1<p id="intro"2 class="note">3 Hello!</p>index.htmlHello!Hello!color: ? (not decided yet)1p { color: gray; }2p { color: navy; }3#intro { color: red; }4.note { color: teal; }0-0-10-0-11-0-00-1-0line–IDs–classes–tags–color–pp#intro.notep 0-0-1p 0-0-1#intro 1-0-0.note 0-1-0
Left: the HTML (one paragraph) and the CSS (four competing rules, with their scores)Right: the browser drawing the paragraph, and the board showing the leading ruleTokens: selectors on their way to what they match, and rules carrying their score to the boardLeft: the HTML file (one paragraph with id="intro" and class="note") and the stylesheet (four rules, all setting color). Right: the browser and the board showing the rule leading so far. All four selectors match. Each gets a specificity score (IDs, classes, tag names). Equal scores: the later rule wins. Higher score: it wins wherever it is written. #intro (1-0-0) wins, so the paragraph is red; without the id, .note (0-1-0) wins and it turns teal. A real browser does not run a contest rule by rule. It collects every matching declaration and sorts them all at once; the figure takes them one at a time so you can watch each comparison. Both ways give the same winner.

The conflict

  1. The page has one paragraph with id="intro" and class="note". The stylesheet has four rules, and each one sets color. Four values for one property is a conflict. The cascade is how the browser picks one winner.

Specificity

Picking the winner

What happens

;; step 1/8 · conflict
index.html
  <p id="intro" class="note">Hello!</p>
style.css
  1  p { color: gray; }
  2  p { color: navy; }
  3  #intro { color: red; }
  4  .note { color: teal; }
one element, one property (color), four values

A real browser does not run a contest rule by rule. It collects every matching declaration and sorts them all at once; the figure takes them one at a time so you can watch each comparison. Both ways give the same winner. Before specificity, the cascade checks other things: where a style comes from (your stylesheet beats the browser's built-in one), !important, cascade layers and the style attribute. In this lesson all four rules are ordinary rules in one stylesheet, so only specificity and order decide.