Sub topic 13
Cascade layers as an alternative
This chapter assumes you know how specificity ranks selectors, how :where() contributes zero specificity, and how the cascade orders declarations. It compares :where() with cascade layers as two ways to keep default styles easy to override.
The problem both tools solve
Earlier chapters used :where() to write defaults that any single-class rule can override. That works because the default has specificity (0,0,0). The weakness is that you must remember to wrap every selector in :where(). One unwrapped selector in a base stylesheet raises specificity, and later rules may lose to it.
Cascade layers solve the same problem at a different level. Instead of lowering the specificity of each selector, you group whole sets of rules into named layers and decide which layer wins.
How a layer works
A layer is declared with @layer. Rules inside it keep their own specificity, but the cascade compares layers before it compares specificity. A rule in a later layer beats a rule in an earlier layer, even if the earlier rule has a more specific selector.
@layer base, components;
@layer base {
ul#nav li a {
color: gray;
}
}
@layer components {
a {
color: teal;
}
}The a rule has specificity (0,0,1), and the ul#nav li a rule has (1,0,3). The link is still teal, because components is declared after base in the first line. Declaring the order up front makes it independent of where the rules appear in the file.
Unlayered styles beat all layered styles in normal declarations. This means page-specific CSS written outside any layer overrides a library you imported into a layer.
Comparing the two approaches
| Question | `:where()` | `@layer` |
|---|---|---|
| What does it change? | Specificity of one selector | Priority of a group of rules |
| Scope of effort | Wrap each selector | Wrap a block or an import |
| Protects against a high-specificity rule in the same group? | No, the rule still has its own specificity | Yes, layer order decides first |
| Source order still matters? | Yes, among equal specificity | Yes, within a single layer |
| Works on part of a selector? | Yes | No |
The last row matters. :where() can lower the weight of just one piece of a compound selector, such as .card :where(h2, h3). A layer can only treat a whole rule as one unit.
Using them together
The two tools combine well. A common pattern puts a reset or defaults in a low layer and uses :where() inside it so rules within that layer are also easy to override.
@layer reset, defaults, theme;
@layer reset {
:where(ul, ol) {
margin: 0;
padding: 0;
}
}
@layer defaults {
:where(a) {
color: royalblue;
}
}
@layer theme {
a {
color: darkgreen;
}
}Here the theme layer wins over defaults because of layer order. Inside defaults, the :where() keeps the default weak, so a later rule in the same layer can override it with any selector.
Choosing between them
- Use
:where()when you control the selectors and want a single default to be weak. - Use
@layerwhen you import third-party CSS or have several groups of styles whose priority should be fixed regardless of selector weight. - Use both when a layer holds many defaults and you also want overrides within the layer to be simple.
Common errors
- Declaring the layer order after the layers are used. The first mention of a layer name sets its position, so list all names in one
@layerstatement at the top. - Expecting a layered rule to beat an unlayered one with higher specificity. Unlayered normal rules always win over layered ones.
- Using layers when only one selector needed to be weaker.
:where()is lighter for that job.
Exercise
A library is imported into @layer vendor, and your own rule a { color: red; } is written outside any layer. The library has nav ul li a.link { color: blue; }. Which color does a link matching both rules get, and why?
Show answer
Red. Normal declarations in unlayered styles beat all layered styles, so the library's higher specificity never gets compared.
Exercise
You declare @layer base, components;. A base rule #app .btn { background: gray; } conflicts with a components rule .btn { background: green; }. Which wins?
Show answer
The components rule wins. The components layer is declared later, and the cascade compares layer order before specificity.