Specificity and the cascade, online

Six activities your students do on their own devices. See who's finished, and their scores.

Specificity and the cascade in one minute

What students need to know

When two rules have identical specificity, the one declared later in the stylesheet wins — here 'color: red' overrides 'color: black'. A class selector scores 0,0,1,0 while an element selector scores 0,0,0,1, so .note beats a plain 'p' rule.

ID selectors score 0,1,0,0 while class selectors score 0,0,1,0 — IDs always win over classes. Source order is the tiebreaker — the later rule overrides the earlier one.

Mistakes students make

  • The '.box' rule is missing a semicolon after 'green' — it should be 'color: green;'.
  • 'colour' is not a valid CSS property — the correct spelling is 'color'.
  • The colon is missing after 'font-size' in the '#hero' rule — it should be 'font-size: 32px;'.
  • '!importnt' is a typo — the correct keyword is '!important'.
PREVIOUS · MODULE 39Pseudo-classes
YOU ARE HERE · MODULE 40Specificity and the cascade
NEXT · MODULE 41CSS color values

Discover other journeys with this module

What students will learn in this module

Every question in Module 40

Quick questions

  1. Which selector type has the highest specificity score?AClass selector (.class)BID selector (#id)
  2. Two rules target the same element with equal specificity. Which rule applies?AThe rule written last in the stylesheetBThe rule written first in the stylesheet
  3. Does !important override an inline style?AYes — !important beats inline stylesBNo — inline styles always win
  4. Which rule wins: 'p { color: blue; }' or '.text { color: red; }'?Ap { color: blue; }B.text { color: red; }
  5. An element selector and a class selector both target the same element. Which wins?AClass selectorBElement selector

Key terms

Students type the meaning of:

  • specificity

Match the pairs

#hero { color: red; }Match it to what it does
color: blue !important;Match it to what it does
Cascade source order tiebreakerMatch it to what it does
Class selector specificity beats element selectorMatch it to what it does
p { color: black }Match it to what it does
.box { color: red !importent; }Match it to what it does

Fill in the blanks

  1. p { _____ red; }

    Hint: Type the CSS property name for text colour

  2. ____ { color: blue; }

    Hint: Type an ID selector for an element with id='nav'

  3. ______ { color: green; }

    Hint: Type a class selector for elements with class='intro'

  4. h1, p { _________ 16px; }

    Hint: Type the CSS property that sets text size

  5. body { color: black __________; }

    Hint: Type the keyword that forces this rule to override all others

Spot the error

Question 1

A.box { color: green }B.note { color: blue; }C#main { color: red; }

Question 2

Ap { color: black; }B.intro { colour: blue; }C#hero { color: red; }

Question 3

Ah2 { font-size: 24px; }B.title { font-size: 20px; }C#hero { font-size 32px; }

Question 4

Abody { background: white; }Bp { color: gray; }C.box { color: green; }Dh1 { color: black !importnt; }

Question 5

A#header { color: navy; }Bp { color: black; }C.nav { color: teal; }Dh2 { font-size: 20px; }Espan  color: gray; }