Sub topic 4
Why Layer Order Beats Specificity
This chapter assumes you can read CSS selectors and compute specificity (for example, that #nav a.link outranks .red). It also assumes you know that later rules win ties, and that you have declared a layer with @layer name { ... }. You don't need !important, @import, or nesting here. Later chapters cover those.
The problem layers solve
Without layers, the only way to beat a high-specificity selector is to write a selector that is at least as specific, or to reach for !important. Over time this leads to selectors like #app .sidebar .nav a.link:not(.disabled) that exist only to win a fight with another selector.
Cascade layers change how the fight is decided. You group rules into named layers and rank the layers. When two rules conflict, the browser compares their layers first. It looks at specificity only when both rules are in the same layer.
Where layers sit in the cascade
When several declarations target the same property on the same element, the browser picks a winner by checking these criteria in order. The first criterion that separates the candidates decides the result.
| Step | Criterion | What it compares |
|---|---|---|
| 1 | Origin and importance | Browser defaults, user styles, author styles, and whether each is `!important` |
| 2 | Context | Shadow DOM encapsulation |
| 3 | Element-attached styles | The `style` attribute |
| 4 | Layer order | Which cascade layer the rule is in |
| 5 | Specificity | The selector's weight |
| 6 | Order of appearance | Which rule comes later in the source |
Layers sit above specificity and source order. Everything about specificity you already know still holds, but it only applies after the layer comparison has produced a tie.
Two consequences follow from this table.
- A rule in a higher-priority layer beats a rule in a lower-priority layer even if the lower one has an ID selector.
- A
styleattribute still beats normal layered rules, because element-attached styles are checked at step 3.
A first example
Here is the example from the plan, with markup so you can run it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Layer order beats specificity</title>
<style>
@layer base, utilities;
@layer base {
#nav a.link { color: blue; }
}
@layer utilities {
.red { color: red; }
}
</style>
</head>
<body>
<nav id="nav">
<a class="link red" href="#">Home</a>
</nav>
</body>
</html>Both rules match the link and both set color. The numbers are:
#nav a.linkhas specificity (1, 1, 1): one ID, one class, one type..redhas specificity (0, 1, 0): one class.
Specificity alone would make the link blue. The link is red because utilities is declared after base in the order statement, and later layers have higher priority. The browser decides at step 4 and never reaches step 5.
How the comparison works
For a conflict between two normal (not !important) declarations, the browser follows this procedure:
- Find the layer each rule belongs to.
- If the layers differ, the rule in the later layer in the declared order wins. Stop.
- If the layers are the same, compare specificity.
- If specificity ties, the rule that appears later in the source wins.
Specificity does not disappear. It becomes a local tiebreaker inside each layer. A layer is like a small, self-contained stylesheet with its own ordinary cascade, and the layers are ranked against each other.
The next example puts two rules in the same layer, so specificity decides.
@layer base, utilities;
@layer base {
#nav a.link { color: blue; }
a { color: green; }
}Inside base, #nav a.link has higher specificity than a, so it wins over a even though a comes later in the source. A link matching both rules is blue. If you added a utilities rule that sets color, that rule would beat both, whatever its selector.
The order statement fixes priority
The line @layer base, utilities; is a statement with no block. It creates both layers and sets their order: layers listed later have higher priority.
This means file order of the blocks no longer matters. Take the same CSS with the blocks swapped:
@layer base, utilities;
@layer utilities {
.red { color: red; }
}
@layer base {
#nav a.link { color: blue; }
}The link is still red. The priority was fixed on the first line, so the order of the blocks below it has no effect.
To see how much the order statement controls, reverse it:
@layer utilities, base;
@layer base {
#nav a.link { color: blue; }
}
@layer utilities {
.red { color: red; }
}Now base is later and therefore stronger, so the link is blue. The rules are the same as before. Only the declared order changed.
Layers lower the cost of low-specificity selectors
Because the layer is checked first, you can write simple selectors in the layer that should win. A utility class with one class selector can override a component rule with several classes, as long as the utility layer comes later:
@layer components, utilities;
@layer components {
.card .card-body a.card-link:hover { text-decoration: underline; }
}
@layer utilities {
.no-underline { text-decoration: none; }
}For <a class="card-link no-underline"> inside a card body, hovering does not show an underline, because .no-underline is in the later layer. Without layers you would have to match or beat a four-part selector, or use !important.
The reverse also holds, and it is the part people forget. A rule in a lower layer cannot beat a rule in a higher layer by being more specific. If the utilities layer sets color on an element, no selector you add to base can override it, however many IDs you chain. You must either move the rule into a layer that is at least as high, or change the layer order.
Specificity comparisons across layers do not exist
It helps to think of layer comparison as a lookup, not a calculation. The browser does not add or weigh a layer's rank against a selector's specificity. Layer rank is compared first, and if it differs, the specificity values are never consulted. This has a practical effect on debugging: two rules from different layers that look "obviously" ranked by specificity in your head may be ranked the other way, and no amount of specificity inspection will explain it.
Common mistakes
- Expecting a high-specificity rule to rescue a low layer. Adding more IDs to a selector in
basedoes nothing against a conflicting declaration inutilities. - Assuming file order sets layer order. The order comes from the first mention of each layer name. If a stray
@layer components { ... }block appears before your order statement,componentsis registered first. Put the order statement at the top of the first stylesheet that loads. - Declaring the same layer name twice and expecting two layers. A repeated name refers to the same layer. Rules in the second block are appended to the existing layer and rank where the layer was first placed, not where the second block appears.
- Forgetting the same-layer case. Inside one layer, the ordinary rules still apply. If a utility rule loses, check whether another rule in the same layer has higher specificity.
The next example shows the third mistake:
@layer base, utilities;
@layer utilities {
.tight { margin: 0; }
}
@layer base {
p { margin: 1rem; }
}
@layer utilities {
.loose { margin: 2rem; }
}There are two layers here, not three. Both utilities blocks form one layer that ranks above base. A paragraph with class="loose tight" gets margin: 2rem, because both rules are in the same layer, they have equal specificity, and .loose comes later in the source.
Checking a result in DevTools
Current browsers show the layer name next to each rule in the Styles pane. When a property is not applying as expected:
- Inspect the element and find the property in the Styles pane.
- Look at the crossed-out declarations. Note which layer each rule belongs to.
- Compare the layer order from your order statement. The rule in the later layer should be the winner.
- If both rules show the same layer, compare specificity and source order as usual.
If the winning rule has no layer label, it is unlayered. That case is covered in the next chapter.
Summary of the model
- The cascade checks origin and importance, context, and element-attached styles first, then layer order, then specificity, then source order.
- Between different layers, the later layer in the declared order wins for normal declarations, whatever the selectors.
- Within one layer, specificity and source order work as they always have.
- An order statement such as
@layer reset, base, components, utilities;sets priority in one place, independent of where the blocks appear. - Reusing a layer name reopens the same layer. It does not create a new one.
Exercise
Given this CSS, what color is <p id="intro" class="note">, and why?
@layer base, theme;
@layer theme {
.note { color: green; }
}
@layer base {
p#intro.note { color: purple; }
}
Show answer
Green. The order statement puts theme after base, so theme has higher priority. The browser compares layers before specificity, so the higher specificity of p#intro.note (1, 1, 1) never matters. The rule order in the file also does not matter, because the layer order was fixed by the first line.
Exercise
A button inside the components layer is styled by .toolbar .actions button.primary { background: navy; }. You want a one-class rule .flat { background: transparent; } to override it. In which layer must .flat be placed, given @layer reset, base, components, utilities;, and what happens if you put it in base?
Show answer
Put .flat in utilities, which ranks above components, or in components itself, where it would lose on specificity to the three-class selector. In utilities it wins despite its low specificity. In base it always loses, because base ranks below components and specificity is never consulted for rules in different layers.