Lesson 2 · Unit 02
The cascade
When rules disagree, one wins by fixed steps
What you practise
- Specificity computed, source order, inheritance,
!importantand why not, custom properties
Try a question from this lesson
Pick one
Two rules with the same selector disagree. What colour is "Tea"?
<style>
p { color: blue; }
p { color: green; }
</style>
<p>Tea</p>Correct answer
green
Why
A property has exactly one value, so one rule must win. When the selectors are equally strong, the one that comes later in the stylesheet wins.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Specificity: three counters
Each selector is scored with three counters: ids, then classes (pseudo-classes and attribute selectors count here), then element types.
Compare the counters left to right. The first difference decides. One id beats any number of classes, and one class beats any number of types.
selector ids classes types
#menu .item a 1 1 1
.item a:hover 0 2 1
ul li a 0 0 3Concept
Who wins
For each property the browser keeps one declaration:
1. !important beats everything that is not. 2. An inline style="..." beats any selector. 3. Higher specificity beats lower. 4. On a tie, the later rule wins.
Inheritance is not on this list. An inherited value is only a fallback, used when no rule at all sets the property on the element.
10 more exercises in the app
- 5× multiple choice
- 2× type the answer
- 1× match the pairs
- 1× order the lines
- 1× fill the blanks
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.