Sub topic 6
Source order: the last rule wins a tie
This chapter assumes you can read selectors and calculate specificity, and that you know the cascade compares origin and importance, then layers, then specificity. Source order is the last of those comparisons. It matters less often than the others, but when it applies it decides the result completely.
Where source order sits
The browser compares competing declarations for one property on one element, one criterion at a time. It moves to the next criterion only if the previous one produced a tie.
- Origin and importance (browser, user, author, and
!important). - Cascade layers.
- Specificity.
- Order of appearance.
Order of appearance means position in the source. If two declarations are equal on every earlier criterion, the one that appears later wins. There is no further tiebreaker. The cascade always ends with a single winner.
Source order never overrides a higher criterion. A rule earlier in the file still beats a later rule if it has higher specificity, and that is the most common reason a "later" rule seems to be ignored.
The basic case
Both selectors below have specificity (0,1,0), so only position separates them.
.button { background: gray; }
.button { background: steelblue; }The button is steelblue. The gray declaration is not deleted. It loses and stays visible, crossed out, in the Styles panel of developer tools.
Two things do not count as order:
- The order of class names in the HTML attribute.
class="a b"andclass="b a"behave identically. Only the order of the rules in the stylesheet matters. - The order of elements in the document. Source order compares the CSS, not the HTML.
Order across stylesheets
Real pages have many stylesheets, and they are treated as one long list in document order. The browser reads <link rel="stylesheet"> and <style> elements in the order they appear in the HTML, and later ones win ties.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Order demo</title>
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="theme.css">
<style>
.card { border-color: tomato; }
</style>
</head>
<body>
<div class="card">Hello</div>
</body>
</html>If base.css, theme.css and the <style> block all set .card { border-color: ... }, the <style> block wins, then theme.css, then base.css. Swapping the two <link> lines swaps the winner between those two files.
Imports
An @import rule places the imported file's rules at the point of the import. Because @import must come before other rules in a stylesheet, the imported rules always come before the rules in the importing file.
@import url("reset.css");
a { color: teal; }Here a { color: teal; } wins any tie with reset.css, because it comes later.
Styles added by script
A stylesheet inserted by JavaScript takes its place in the document like any other. A <style> element appended to the end of <head> comes after the existing stylesheets, so it wins ties with all of them. A script that adds a stylesheet and finds that it "doesn't work" has usually lost on specificity, not order.
Order inside a single rule
Source order also applies within one declaration block. If a property appears twice, the last one wins.
.panel {
width: 300px;
width: 50%;
}The width is 50%. Writing the same property twice on purpose is a common way to provide a fallback.
Fallbacks and unsupported values
When the browser parses a declaration it cannot understand, it discards that declaration. It never reaches the cascade. That lets you put a safe value first and a newer value after it.
.panel {
width: 90%;
width: min(90%, 40rem);
}A browser that does not understand min() throws away the second line and keeps 90%. A browser that does understand it uses the second line, because it is later.
The order matters. If you wrote the modern value first and the safe value second, every browser would use the safe value, because both are valid and the later one wins.
Shorthands and longhands
A shorthand property such as margin, border or background sets several longhand properties at once. This makes order matter in a way that is easy to miss.
.box {
border-color: red;
border: 1px solid;
}The border shorthand comes second, and it sets every border longhand, including border-color. Since no color was given, the color resets to its initial value, currentcolor. The red is gone. Reversing the two lines fixes it:
.box {
border: 1px solid;
border-color: red;
}The rule of thumb is to write shorthands first and the specific longhands after them. The same applies across rules. If .box { margin-left: 0; } appears before .box { margin: 1rem; }, the left margin ends up as 1rem.
Link states and the LVHA order
Link pseudo-classes show source order at work. In these selectors the pseudo-class adds (0,1,0) to the a element's (0,0,1):
| Selector | Specificity |
|---|---|
| `a:link` | (0,1,1) |
| `a:visited` | (0,1,1) |
| `a:hover` | (0,1,1) |
| `a:active` | (0,1,1) |
All four tie, so the last matching rule wins. A link the user is pressing can match :link, :hover and :active at once. To make the stronger states win, write the rules from weakest to strongest: link, visited, hover, active (often remembered as LVHA).
a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: darkorange; }
a:active { color: red; }If you put a:hover before a:visited, a visited link can never show the hover color, because :visited comes later and also matches.
Media queries do not add weight
A rule inside @media has exactly the specificity it would have outside. The media condition decides whether the rule applies at all, but it gives no priority when it does. Order still decides.
.card { padding: 1rem; }
@media (min-width: 40rem) {
.card { padding: 2rem; }
}This works as intended. On wide screens both rules apply, tie on specificity, and the later one wins. If the @media block came first, the plain .card rule would come later and win everywhere, and the media query would appear to do nothing.
This is why "mobile-first" CSS puts the base styles first and the min-width overrides after them.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A later rule has no effect | The earlier rule has higher specificity | Compare specificity first, then raise or lower one side |
| A media query override is ignored | The `@media` block sits above the base rule | Move the base rule above it |
| Hover style never shows on a visited link | `:visited` placed after `:hover` | Use link, visited, hover, active order |
| A color or border vanishes | A shorthand written after its longhand | Write the shorthand first |
| A fallback value is never used | The modern value was written first | Write the fallback first |
| Overrides in a new file lose to old ones | The new `<link>` comes before the old one | Reorder the `<link>` elements |
Working with source order on purpose
Relying on order is fine for rules of equal specificity, such as a base style and its variant:
.btn { padding: 0.5rem 1rem; background: #eee; }
.btn-primary { background: steelblue; color: white; }Both selectors are single classes, so .btn-primary wins because it is written after .btn. This is legitimate, but it makes the stylesheet fragile. Someone who sorts the file alphabetically, concatenates files in a different order, or moves a block can change the output without changing a single selector.
Three habits reduce that risk:
- Keep related rules together, general ones before specific ones.
- Keep specificity flat, mostly single classes, so order is the one thing deciding ties.
- When a project needs a guaranteed ordering between groups of styles, use cascade layers. They give you an explicit order that does not depend on where each file is loaded.
Exercises
Exercise
Given the CSS below and the element <p class="intro lead">Text</p>, what color is the text, and does changing the HTML to class="lead intro" change the result?
.lead { color: green; }
.intro { color: orange; }
Show answer
The text is orange. Both selectors have specificity (0,1,0), so the later rule, .intro, wins. Changing the order of the class names in the HTML changes nothing, because only the order of the rules in the stylesheet counts.
Exercise
A page has this CSS, and on a narrow screen the card still has 2rem of padding instead of 1rem. Explain why and fix it without changing any selector. @media (max-width: 40rem) { .card { padding: 1rem; } } .card { padding: 2rem; }
Show answer
The media query does not add specificity, so both rules have specificity (0,1,0) and both match on a narrow screen. The plain .card rule appears later, so it wins. Move the .card { padding: 2rem; } rule above the @media block, so the override comes after the base rule.
Exercise
What color does a link show while the pointer is over it, given this CSS, and how do you fix the order so hover works for visited links? a:hover { color: darkorange; } a:visited { color: purple; }
Show answer
An unvisited link shows dark orange, but a visited link shows purple. Both selectors have specificity (0,1,1), and a:visited comes later, so it wins whenever both match. Put a:visited before a:hover, and keep a:active last, so the order is link, visited, hover, active.