Sub topic 11
The Cascade: Origin, Specificity, and Source Order
This chapter assumes you can write class, ID, and type selectors and can calculate a specificity triple. It covers how the browser chooses one value when several rules set the same property on the same element.
The cascade as a tie-breaker
An element often matches many rules that set the same property. The cascade is the algorithm that picks one winner for each property. It compares competing declarations in a fixed order and stops at the first step that separates them.
The steps that matter here are:
- Origin and importance. Declarations come from the browser's default styles, the user's styles, or your stylesheet (the author origin).
!importantchanges how these origins rank. - Specificity. The selector with the higher (IDs, classes, types) triple wins.
- Source order. If everything else is equal, the declaration that appears later wins.
Step 1: Origin
Browser default styles are why a <h1> is large and bold before you write any CSS. Your author styles override them. For normal declarations the order is: browser defaults lose to user styles, and user styles lose to author styles. You rarely need to think about this beyond knowing your CSS beats the defaults.
Step 2: Specificity
When both declarations come from the author origin, the browser compares specificity. Compare the triple from left to right. Any ID beats any number of classes, and any class beats any number of types.
p { color: black; } /* (0,0,1) */
.note { color: green; } /* (0,1,0) */
p.note { color: blue; } /* (0,1,1) */
#main p.note { color: purple; } /* (1,1,1) */
ul li.active > a[href] { color: red; } /* (0,2,3) */For a <p class="note"> inside #main, all four of the first rules match. The purple rule wins because its ID count is 1. The combinator > adds nothing to the triple.
Step 3: Source order
If specificity is also equal, the later rule wins.
.btn { background: gray; }
.btn { background: teal; }Both selectors are (0,1,0), so the button is teal. This applies across files too. A stylesheet linked later in the HTML counts as appearing later.
Origin beats specificity when `!important` is involved
A declaration marked !important moves into a higher-ranked group, so specificity is not compared with normal declarations.
#box { color: blue; }
.box { color: red !important; }The text is red, even though #box is more specific. Specificity only decides between declarations in the same group.
Common errors
- Raising specificity to fix an ordering bug. If a rule loses only because it appears earlier, move it later instead of adding an ID.
- Assuming the last rule always wins. Source order is the final tie-breaker, so a later rule with lower specificity still loses.
- Comparing triples as a single number. (0,1,11) does not beat (0,2,0). The columns do not carry over.
- Forgetting that a rule must match the element itself. Inherited values lose to any rule that targets the element directly, which a later chapter covers.
Exercise
Given a.link { color: red; } followed later by nav a { color: blue; }, what color is <a class="link"> inside a <nav>? Explain using the cascade steps.
Show answer
Red. Both rules are author-origin and neither is important. a.link is (0,1,1) and nav a is (0,0,2). The first has a higher class count, so it wins on specificity. Source order is never consulted, even though the blue rule comes later.
Exercise
A stylesheet has #menu a { color: green; } and later a { color: orange !important; }. Which color applies to a link in #menu, and why?
Show answer
Orange. The !important declaration is ranked above normal declarations, so the higher specificity of #menu a does not matter.