Sub topic 13
Cascade layers as an alternative
Both :where() and cascade layers keep specificity from deciding a conflict. This chapter compares them and shows when to use each. It assumes you know the previous chapters: how :is() and :where() compute specificity, and why a :where() default is easy to override. It also assumes you can read a specificity triple such as (1,0,1).
The problem both tools solve
Specificity is hard to manage at scale. A reset like #app a { color: inherit; } beats every later .link rule, however many you write. The usual defenses are to lower the specificity of the base rules or to stop comparing specificity across groups of rules.
:where() takes the first approach. It zeroes the specificity of the selector inside it. Cascade layers take the second. A layer is a named group of rules, and the cascade compares layers before it compares specificity.
How a layer works
You create a layer with @layer. Rules inside the block belong to that layer.
@layer base {
a {
color: navy;
}
}The order in which layers are first declared sets their priority. Later layers beat earlier layers for normal declarations. The usual pattern is to declare the whole order in one statement at the top of the stylesheet:
@layer reset, base, components, utilities;Here utilities has the highest priority and reset the lowest. Rules can appear later in @layer blocks in any source order. The statement above has already fixed the ranking.
Where layers sit in the cascade
The cascade compares these things in order and stops at the first difference:
- Origin and importance (browser, user, author, and
!important). - Context (such as shadow DOM).
- Element-attached styles (the
styleattribute). - Cascade layers.
- Specificity.
- Order of appearance.
Layers come before specificity. If two declarations are in different layers, the one in the higher-priority layer wins and specificity is never checked. Specificity only decides conflicts between rules in the same layer.
The next example shows this.
@layer defaults, components;
@layer defaults {
/* Specificity (1,0,1) */
#main a {
color: gray;
}
}
@layer components {
/* Specificity (0,0,1), but this layer comes later */
a {
color: teal;
}
}A link inside #main is teal. The defaults rule has far higher specificity, but the components layer outranks it, so the cascade never compares the two selectors.
Unlayered styles beat layered styles
Styles that are not inside any layer act as if they were in a final layer with the highest priority. For normal declarations, any unlayered rule beats any layered rule, whatever the specificity.
@layer components {
#main .button {
background: teal;
}
}
.button {
background: crimson;
}The button is crimson. This is useful because a page author can always override layered library code with plain CSS. It also causes the most common layer bug. If you put your own styles in layers but an old stylesheet is unlayered, the old stylesheet wins every conflict.
To bring third-party CSS under your control, import it into a layer:
@layer vendor, base, components;
@import url("vendor.css") layer(vendor);@import rules must come before other rules. The only exceptions are @charset and @layer statements, so the order statement can sit above the import.
Comparing `:where()` and layers
Both tools make some rules easy to override. They work at different scopes, and their failure modes differ.
| Aspect | `:where()` | Cascade layers |
|---|---|---|
| Unit of control | One selector, or part of one | A whole group of rules |
| What it changes | The specificity of the selector inside it | Which group the cascade ranks first |
| Priority order | Not explicit. Zero-specificity rules fall back to source order | Explicit, set by the `@layer` order statement |
| Effect on `!important` | None. The declaration stays important | Reverses the layer order for important declarations |
| Applies to third-party CSS | Only if you rewrite its selectors | Yes, through `@import ... layer(name)` |
| Can lose to unlayered CSS | No | Yes, always |
Take the default list style from earlier in this tutorial:
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}Any single-class rule beats it, in any source order. That is the :where() guarantee. Now compare the layered version:
@layer defaults, components;
@layer defaults {
ul,
ol {
margin: 0;
padding-left: 1.5rem;
}
}
@layer components {
.menu {
padding-left: 0;
}
}This also works, and the selector inside defaults can stay a plain ul, ol. The layer does the job of :where() for every rule inside it. The cost is that the order statement must exist and be correct.
The `!important` reversal
Layers reverse their order for !important declarations. For normal declarations the later layer wins. For important declarations the earlier layer wins, and important declarations in layers beat important unlayered declarations.
@layer base, overrides;
@layer base {
p {
color: black !important;
}
}
@layer overrides {
p {
color: red !important;
}
}The paragraph is black. The base layer is earlier, so its important declaration takes priority. The design intent is that a foundation layer, such as a reset or a security-related style, can protect its important rules from later layers. If you use layers, use !important sparingly, because its behavior is less intuitive than in a flat stylesheet.
:where() does not change !important at all. Important declarations in a :where() rule still beat normal ones, and specificity still orders important declarations against each other.
Combining the two
The tools do not exclude each other. Inside a layer, :where() still lowers specificity within that layer. A common pattern is a base layer whose rules use :where(), so that even other rules in the same layer can override them. Layers manage priority between groups, and :where() manages priority inside a group.
@layer base, components;
@layer base {
:where(h1, h2, h3) {
margin-block: 0.5em;
}
h2 {
margin-block: 1em;
}
}The h2 rule wins inside base because the :where() rule has specificity (0,0,0) and h2 has (0,0,1). Without :where(), h1, h2, h3 would score (0,0,1) for each item. h2 would then tie with it, and the later source position would decide.
Choosing between them
Use these criteria:
- Choose
:where()when you want a few specific rules to be easy to override and you control the selectors. It needs no setup and does not depend on a layer order. - Choose layers when you want to rank whole groups of CSS, such as reset, base, components and utilities. Layers also suit CSS you cannot edit, because
@import ... layer()assigns it to a layer without changing its selectors. - Use both when you need group ranking and want some rules inside a group to be even weaker.
Also check browser support. :where() and @layer are both supported in all current major browsers. Cascade layers arrived in 2022, and :where() in 2021. Older browsers that do not understand @layer ignore the whole block, so the layered rules do not apply at all. A :where() rule in an old browser that lacks the pseudo-class is dropped too, because an unknown pseudo-class invalidates the rule.
Common mistakes
- Forgetting that unlayered CSS beats layered CSS, then wondering why a layered utility class has no effect.
- Declaring layers in the wrong order. The first mention of a layer name sets its rank, so a stray
@layer components { ... }before the order statement can fix the wrong order. - Expecting
!importantto behave the same way inside layers. The order is reversed. - Writing high-specificity selectors in a low layer and expecting them to win. They do not, because layers are compared first.
Exercise
A stylesheet declares @layer base, theme;. The base layer contains #page p { color: black; }. The theme layer contains p { color: green; }. A separate unlayered rule says .intro { color: purple; }. What color is a paragraph with class intro inside #page? What color is a paragraph without that class?
Show answer
The paragraph with class intro is purple, because unlayered rules beat layered rules for normal declarations. The paragraph without the class is green. The theme layer is declared after base, so it has higher priority, and the ID in the base rule does not matter because layers are compared before specificity.
Exercise
You must override a third-party stylesheet that uses selectors such as .widget .title a and that you cannot edit. Name two approaches that use layers and :where() ideas from this chapter, and say which one fits better.
Show answer
You can import the vendor CSS into a low-priority layer with @import url("vendor.css") layer(vendor); and declare your own layers after it, or you can write your own selectors with higher specificity. The layer approach fits better. :where() only helps for selectors you write, and you cannot change the vendor selectors, while the layer lets your simple rules win without raising specificity.