Sub topic 4
Why layer order beats specificity
This chapter assumes you know selector specificity and source order, and that you can read the Styles panel in browser DevTools. You should also know the @layer block syntax and how an @layer statement sets layer order, both covered earlier.
Where layers sit in the cascade
When two declarations target the same property on the same element, the browser compares them in a fixed sequence of steps. It stops at the first step that finds a difference.
- Origin and importance (user agent, user, author, and whether the declaration uses
!important). - Context (for example, shadow DOM).
- Style attribute, if present.
- Cascade layer order.
- Specificity.
- Source order.
Layer order is checked before specificity. If two declarations sit in different layers, the one in the higher-priority layer wins, and the browser never compares their selectors. Specificity only matters between declarations that are in the same layer.
For normal (non-!important) declarations, layers declared later have higher priority.
A worked example
@layer base, utilities;
@layer base {
#nav a.link { color: blue; }
}
@layer utilities {
.red { color: red; }
}Take this markup:
<nav id="nav"><a class="link red" href="/">Home</a></nav>The selector #nav a.link has specificity (1,1,1). The selector .red has specificity (0,1,0). Without layers, the blue rule would win. With layers, the link is red, because utilities is declared after base.
The order statement on the first line is what fixes this. Even if the base block appeared later in the file, utilities would still win.
Specificity still works inside a layer
Layers do not turn specificity off. Within one layer, the usual rules apply.
@layer components {
.card a { color: black; }
a.link { color: purple; }
}Both selectors have specificity (0,1,1) and (0,1,1), so the later one wins. If one were higher, it would win. Layers only change the outcome when the competing declarations come from different layers.
What this changes in practice
Before layers, overriding a rule often meant raising specificity: adding an ID, chaining classes, or using !important. Each fix made the next override harder. With layers, you decide priority once, in the order statement, and write low-specificity selectors inside each layer.
A common arrangement puts broad, low-priority rules first and narrow, high-priority rules last:
@layer reset, base, components, utilities;A single-class utility in utilities then beats a three-class selector in components, with no extra specificity.
Common errors
- Wrong order of declaration. Layer order comes from the first time each layer name appears. If a
@layer components { ... }block appears before your order statement,componentsis placed first, and the later statement cannot move it. Put the order statement at the top of your first stylesheet. - Expecting specificity to rescue a lower layer. Adding an ID to a selector in
basewill not make it beat a bare element selector inutilities. Move the rule to a higher layer or change the order. - Forgetting unlayered styles. Normal declarations outside any layer beat layered ones. This is covered in the next chapter.
Exercise
Given @layer base, utilities;, a rule #app .btn.primary { background: blue; } in base, and a rule button { background: red; } in utilities, what background does <button class="btn primary"> inside #app get, and why?
Show answer
Red. The two declarations are in different layers, and utilities is declared after base, so it has higher priority. The browser decides at the layer step and never compares the specificity of the two selectors.
Exercise
Two rules sit in the same layer. One is .nav a { color: blue; } and the other is a { color: green; }, written after it. Which color wins, and does layering change that?
Show answer
Blue. They are in the same layer, so specificity decides, and .nav a (0,1,1) is higher than a (0,0,1). Source order is only a tie-breaker. Layering does not change the result.