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'.
What students will learn in this module
Every question in Module 40
Quick questions
- Which selector type has the highest specificity score?AClass selector (.class)BID selector (#id)
- 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
- Does !important override an inline style?AYes — !important beats inline stylesBNo — inline styles always win
- Which rule wins: 'p { color: blue; }' or '.text { color: red; }'?Ap { color: blue; }B.text { color: red; }
- 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 doescolor: blue !important;Match it to what it doesCascade source order tiebreakerMatch it to what it doesClass selector specificity beats element selectorMatch it to what it doesp { color: black }Match it to what it does.box { color: red !importent; }Match it to what it doesFill in the blanks
p { _____ red; }Hint: Type the CSS property name for text colour
____ { color: blue; }Hint: Type an ID selector for an element with id='nav'
______ { color: green; }Hint: Type a class selector for elements with class='intro'
h1, p { _________ 16px; }Hint: Type the CSS property that sets text size
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; }
