Sub topic 5
Understand the cascade, specificity and inheritance
This chapter assumes you can link a stylesheet to a page and write a basic rule with a selector and declarations. You also need to be able to open your browser's developer tools. When two rules set the same property on the same element, the browser has to pick one. The method it uses is called the cascade.
How the browser picks a winner
For each property on each element, the browser collects every declaration that applies. It then compares them in this order and stops at the first difference:
- Importance and origin. A declaration marked
!importantbeats a normal one. Your styles beat the browser's default styles. - Specificity. A more specific selector beats a less specific one.
- Source order. If everything else ties, the rule that appears later wins.
Specificity
Specificity is a score for a selector, written as three counts. Compare the counts from left to right.
| Selector part | Examples | Counts in column |
|---|---|---|
| IDs | `#about` | First |
| Classes, attributes, pseudo-classes | `.active`, `[href]`, `:focus-visible` | Second |
| Element types, pseudo-elements | `a`, `nav`, `::before` | Third |
The universal selector * adds nothing. Here is a navigation link that matches three rules:
<nav>
<a class="active" href="#about">About</a>
</nav>a {
color: #0b5cad;
}
nav a {
color: #1f2933;
}
.active {
color: #b42318;
}The scores are a = 0-0-1, nav a = 0-0-2 and .active = 0-1-0. The link is red, because one class outranks any number of element selectors. Moving .active to the top of the file would not change this, because specificity is checked before source order.
Inheritance
Some properties pass from a parent element to its children without any rule. Text properties such as color, font-family, font-size and line-height inherit. Box properties such as margin, padding, border and background do not.
This is why the starter stylesheet sets font-family and color once on body. Every paragraph, list and heading picks them up.
An inherited value has the lowest priority. Any rule that targets the element directly overrides it, even a rule with a specificity of 0-0-0. Links are a common surprise here. The browser's default styles give a its own color, so a color set on body never reaches it. You can ask for the parent's value explicitly:
footer a {
color: inherit;
}Common errors
- Reaching for
!important. It wins now but makes later overrides harder. Fix the selector or the order instead. - Using IDs for styling.
#about pscores 1-0-1, and only another ID can beat it. Prefer classes. - Assuming the later rule wins. Order is only the tiebreaker. A later, weaker rule loses to an earlier, stronger one.
- Expecting box properties to inherit. A border on
sectiondoes not appear on the paragraphs inside it. - Guessing instead of looking. In developer tools, select the element and open the Styles pane. Losing declarations appear with a line through them, and the winning rule is listed above them.
Exercise
A paragraph has the class intro and sits inside <main>. The stylesheet contains main p { color: navy; } followed by .intro { color: teal; }. Which color does the paragraph get, and why? Then say what changes if .intro is written before main p.
Show answer
The paragraph is teal. .intro scores 0-1-0 and main p scores 0-0-2, so the class wins on specificity. Moving .intro before main p changes nothing, because source order is only checked when specificity ties.