HTML & CSS Masterclass Tutorial

Comprehensive guide to modern HTML and CSS development.

Selectors & Specificity

Selectors & Specificity

CSS selectors allow you to target specific HTML elements to style them.

  • Element Selector: Targets tags (e.g., p { color: red; })
  • Class Selector: Targets the class attribute, prefixed with a dot (e.g., .button { background: blue; })
  • ID Selector: Targets the id attribute, prefixed with a hash. IDs must be unique per page! (e.g., #header { height: 100px; })

Specificity

If there are two or more conflicting CSS rules that point to the same element, the browser follows rules of specificity to determine which one wins.

Hierarchy (from lowest to highest power): Element < Class < ID < Inline Style < !important.

Try It Yourself

Edit the code below and click Run to see the result live.

Source Code
Result
Topics