FRONTENDO
Emberhold Forge
0
0
CSS · Selectors & Specificity
New technique

Three basic selectors

A selector is your aim. Strike the right elements and nothing else.

The selector is the part of a rule before the {. It decides which elements the rule touches. Most of the time you aim with one of three things HTML already gives you: the tag name, a class, or an id.

Classes are the everyday workhorse: you can put one on any element you like and reuse it. Ids are unique, so an id selector can only ever hit one element.

p { … }        /* every <p> */
.hot { … }     /* every element with class="hot" */
#forge { … }   /* the element with id="forge" */
* { … }        /* every element */
Result in the browser
  • Type selector: just the tag name, like p or h2.
  • Class selector: a dot and the class name, like .hot. In the HTML you write class="hot" without the dot.
  • Id selector: a hash and the id, like #forge.
  • The universal selector * matches everything.
  • Common mistake: forgetting the dot. primary { … } looks for a <primary> tag, which doesn't exist.