Sub topic 6
Source order: the last rule wins a tie
This chapter assumes you can read CSS selectors and compute specificity as (ids, classes, elements), as covered in the earlier chapters. Source order is the last criterion the cascade checks. It only matters when every earlier criterion is equal.
Where source order sits
The cascade compares two competing declarations in a fixed sequence: origin and importance, then cascade layers, then specificity, then order of appearance. The comparison stops at the first criterion that separates them. If two declarations tie on all of the earlier criteria, the one that appears later wins.
The basic case
Two rules with identical selectors have identical specificity. The later one wins.
.button { background: gray; }
.button { background: steelblue; }The button is steelblue. Swap the two lines and it becomes gray. Nothing else about the rules changed, only their position.
Source order never overrides specificity. In the next example the id selector wins even though it comes first.
#intro { color: green; }
p { color: black; }A <p id="intro"> is green. The specificity of (1,0,0) beats (0,0,1) before source order is consulted.
Order across files
"Later" refers to the order in which the browser processes the rules, not to a single file. All stylesheets on a page are treated as one sequence, in the order the <link> and <style> elements appear in the document.
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="theme.css">If both files contain .button rules, those in theme.css win ties because the file is linked second. A common bug is a component stylesheet that stops working after someone reorders the <link> tags or a build tool changes the bundle order.
An @import rule inserts the imported file at the point where the import appears. Imports must come before other rules, so imported styles are earlier in the sequence than the rules of the importing file.
Order inside one rule
Source order also applies to declarations within a single rule. The last declaration of a property wins.
.panel {
width: 100%;
width: calc(100% - 2rem);
}Browsers drop declarations they cannot parse. A browser that does not understand calc() discards the second line and keeps 100%. A browser that does understand it uses the second line. This is the basis of the fallback pattern: put the safe value first and the newer value after it.
Order-dependent patterns
Several familiar patterns work only because of source order.
Link states: :link, :visited, :hover and :active all have the same specificity, (0,1,1) when combined with a. The order decides which state wins when more than one applies.
a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: red; }
a:active { color: orange; }A hovered, unvisited link is red because the :hover rule comes after :link. If you moved a:link to the end, it would override the hover and active colors for every unvisited link.
Media queries: an @media block adds no specificity. A responsive override must therefore come after the rule it overrides.
.card { padding: 1rem; }
@media (min-width: 40rem) {
.card { padding: 2rem; }
}If you wrote the @media block first, the plain .card rule would win at every width, and the media query would appear to do nothing.
Order within layers
Source order also works inside cascade layers. Within one layer, later rules win ties. Between layers, the layer order set by @layer decides, whatever the position of the rules in the file. Source order is the tie-breaker only among rules in the same layer, or among rules outside any layer.
Common mistakes
- Expecting a later rule to win against a more specific selector. Check specificity first.
- Adding an override earlier in the file than the rule it targets.
- Forgetting that a third-party stylesheet linked after yours wins ties with it.
- Raising specificity or adding
!importantto fix an ordering problem. Moving the rule is usually the simpler fix.
Exercise
A page links a.css first and b.css second. a.css contains .title { color: red; } and b.css contains h1 { color: blue; }. For <h1 class="title">, what color is the heading, and which criterion decides it?
Show answer
The heading is red. The .title selector has specificity (0,1,0) and h1 has (0,0,1), so specificity decides the result before source order is checked. The later file does not matter here.
Exercise
You write .alert { margin: 0; } in your stylesheet, followed by @media (min-width: 40rem) { .alert { margin: 2rem; } }. A teammate moves the @media block to the top of the file. What happens at wide screens, and why?
Show answer
The margin stays 0. Both rules have the specificity (0,1,0), and the media query adds none. The plain .alert rule is now later in the file, so it wins the tie even when the media query matches.