Selectors and the cascade · Frontend: HTML, CSS and JavaScript
When two CSS rules disagree, which one wins?
Course contentsWhy you need it
CSS decides how everything looks. On a real site, many rules touch the same element: a base style, a component style, a special case. When the colour comes out wrong, the reason is almost always the cascade, and knowing its rules turns guessing into a quick fix.
The intuition
A CSS rule picks elements with a selector and sets properties:
h1 { color: tomato; }| Selector | Picks |
|---|---|
p | every <p> |
.note | every element with class="note" |
#intro | the element with id="intro" |
nav a | every a somewhere inside a nav |
ul > li | li that are direct children of a ul |
When several rules set the same property on one element, the browser decides in this order:
- Specificity: count ids, then classes, then tags.
#intro(1,0,0) beats.note(0,1,0) beatsp(0,0,1).p.noteis (0,1,1), which beats.note. - Order: on a tie, the rule written later wins.
- An inline
style="…"beats both, and!importantbeats everything. Avoid them: they make the next change harder.
Some properties are inherited: color and font-family set on a parent flow down to the text inside. Box properties like border and padding are not.
Definition
Selector
The part before { } that says which elements a rule applies to.
Specificity
A score (ids, classes, tags) that decides between rules aimed at the same element. Higher wins; on a tie the later rule wins.
Inheritance
Text properties such as color and font-family pass from a parent element to the elements inside it, unless they set their own.
Examples
Three rules, one paragraph
Run it in the lessonAll three rules match the paragraph. Specificity decides.
<style>
p { color: grey; }
#tip { color: green; }
.note { color: navy; }
</style>
<p id="tip" class="note">Drink water.</p>- L2Matches: score (0,0,1).
- L3Matches: score (1,0,0), the highest.
- L4Matches: score (0,1,0). It's later than #tip, but order only matters on a tie.
- L6The paragraph is green.
Inherited and not inherited
Run it in the lessonThe colour flows down to the span; the border doesn't.
<style>
.box { color: purple; border: 2px solid black; }
</style>
<div class="box">
Outer text and <span>a span inside</span>
</div>- L2Both properties are set on the div only.
- L5The span has no rules of its own: its text is purple (inherited), but it gets no border of its own.
Common mistakes
Fighting with !important
It works once, then the next fix needs another !important. Make the selector more specific instead, or simplify the rules.
Thinking order always wins
A later rule only wins when specificity is equal. .note written last still loses to #tip written first.
Styling with ids
An id can be used once per page and its high specificity is hard to override. Prefer classes for styling.
Quick quiz
1. Which selector has the highest specificity?
Show the answer
Answer: A
One id (1,0,0) beats any number of classes and tags.
2. `.note { color: red }` comes first and `p.note { color: blue }` second. What colour is `<p class="note">`?
Show the answer
Answer: A
p.note scores (0,1,1), higher than .note at (0,1,0).
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWho wins?
What colour is the heading?
- ModifyMake the warning red
The warning should be red but shows grey. Change the CSS so it's red and the first note stays grey. Don't use
!importantor astyle=attribute. - WriteStyle a price list
Write CSS in the
<style>block so that:- the whole list uses the font
Georgia - every
.priceisbold - prices of items on sale (
li.sale) arered; other prices keep their default colour
- the whole list uses the font
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| CSS | CSS | Cascading Style Sheets: the rules that decide how HTML looks. |
| Selector | 选择器 | The pattern that picks which elements a CSS rule applies to. |
| Specificity | 优先级(特异性) | The (ids, classes, tags) score that decides between conflicting rules. |
| Cascade | 层叠 | The rules a browser uses to pick one value when several CSS rules conflict. |