Sub topic 7
Cascade layers: controlling precedence with @layer
This chapter assumes you can calculate selector specificity, know that later rules win ties, and understand how !important and origins change precedence. Cascade layers add one more step to that process. They let you decide which group of rules wins without raising specificity.
The problem layers solve
Specificity is a poor tool for controlling precedence across a whole project. A library styles #nav a, and your override must beat it. You add a longer selector, then someone adds !important, and the stylesheet becomes a specificity arms race.
A cascade layer is a named group of rules. The browser compares layers before it compares specificity, so a rule's layer can matter more than its selector.
Where layers sit in the cascade
For one property on one element, the browser compares competing declarations in this order and stops at the first difference:
- Origin and importance
- Cascade layer
- Specificity
- Source order
Specificity only decides between declarations that sit in the same layer. A selector's score is never compared across layers.
Declaring layers and their order
List layer names in one @layer statement at the top of your stylesheet. That statement fixes the order. Later layers have higher precedence for normal declarations.
@layer base, components;
@layer base {
#nav a { color: navy; }
}
@layer components {
a { color: teal; }
}Links inside #nav are teal. The #nav a selector has higher specificity, but components comes after base, so it wins before specificity is considered.
The first time the browser sees a layer name, it fixes that layer's position. Later @layer base { ... } blocks add rules to the existing layer but do not move it. This is why declaring the order up front matters.
Unlayered styles win
Rules outside any layer behave as if they sit in a final layer above all named layers. For normal declarations, unlayered styles beat layered ones.
@layer components {
.button { background: steelblue; }
}
.button { background: crimson; }The button is crimson even though both selectors score (0,1,0) and the layered rule is not earlier in a way that matters. Layer position decides it. A common use is to place a third-party stylesheet in a layer so that your own unlayered CSS always overrides it.
@import url("library.css") layer(vendor);Importance reverses the layer order
For !important declarations, the order flips. The earliest layer wins, and layered important declarations beat unlayered important ones.
@layer base, components;
@layer base {
p { color: red !important; }
}
@layer components {
p { color: blue !important; }
}Paragraphs are red. The same rule that makes components win for normal declarations makes base win for important ones. This lets a base layer protect essential rules, such as a reset, from later layers.
Nested and anonymous layers
Layers can nest. Write @layer framework.reset { ... } or put an @layer block inside another one. Nested layers are ordered inside their parent, and the parent layer is compared as a whole against other top-level layers.
A layer without a name, written @layer { ... }, is anonymous. It takes its position where it appears, and nothing else can add rules to it. Prefer named layers so you can extend them.
The keyword revert-layer rolls a property back to the value from the previous layer, as if the current layer had not set it.
Common errors
- Layer order set by accident. If a
@layer components { ... }block appears before your ordering statement,componentsis registered first and the ordering statement cannot reorder it. Put the ordering statement first. - Expecting specificity to beat a layer. An id selector in an early layer loses to a plain element selector in a later layer.
- Layering your own code and the library's inconsistently. Unlayered third-party CSS beats all your layered rules. Layer the library, or layer nothing.
- Forgetting the importance flip. Adding
!importantto a rule in a later layer does not help against an important rule in an earlier layer.
Exercise
Given @layer reset, theme;, a rule #page p { margin: 0; } inside reset, and a rule p { margin: 1rem; } inside theme, what margin does a paragraph in #page get, and why? What changes if the reset rule becomes margin: 0 !important;?
Show answer
The paragraph gets 1rem. Both are normal declarations, so the layer is compared before specificity, and theme is later than reset. If the reset rule is !important, the paragraph gets 0, because for important declarations the earlier layer wins.