1<h1 id="top">Menu</h1>2<p>Coffee</p>3<p class="new">Tea</p>4<p class="new">Cake</p>index.htmlMenuCoffeeTeaCake1p { font-size: 20px; }2.new { color: tomato; }3#top { color: teal; }<h1>id="top"<p><p>class="new"<p>class="new"<h1><p><p><p>ppp.new.new#top20pxtomatoteal
Left: the two files, index.html (content) and style.css (three rules)Right: the browser window and the four elements; matched elements liftTokens: one copy of a selector per element it matches, then the rule's valueLeft: the HTML file (four elements, one with an id, two with a class) and the CSS file (three rules). Right: the browser window and the four elements it made. Copies of each selector hop to every element it matches: p reaches all three paragraphs, .new the two with that class, #top only the heading. Then each rule's value restyles just those elements. A real browser works out every element's style in one pass. The figure tests one rule at a time so you can see what each selector reaches.

Elements and patterns

  1. The browser makes an element from each tag. Every element has a tag name, like h1 or p. Some carry labels you add: a class, here class="new", or an id, here id="top".

Three kinds of selector

Their reach

What happens

;; step 1/9 · elements
index.html   4 elements
  <h1>Menu     id top
  <p>Coffee    no labels
  <p>Tea       class new
  <p>Cake      class new
a class can go on many elements
an id names one element: unique in the page

A real browser works out every element's style in one pass. The figure tests one rule at a time so you can see what each selector reaches. No two rules here set the same property on the same element, so they never compete. When two matching rules do set the same property, the cascade and specificity decide which one wins.