Sub topic 1
Where cascade layers fit in the cascade
Prerequisites
This chapter assumes you already know:
- Basic CSS syntax: selectors, properties, and declarations.
- How selector specificity and source order decide between two rules.
- How to link or import a stylesheet.
- How to inspect applied styles in browser DevTools.
The problem layers solve
When two rules set the same property on the same element, the browser runs the cascade to pick one. Most teams know only two tie-breakers: specificity and source order. That leads to specificity fights. A reset or framework rule such as #nav a.link beats your later .red utility, so you add !important or a longer selector to win. Cascade layers add a new step to the cascade, so you can state which group of rules should win and stop escalating selectors.
The order of the cascade
For conflicting declarations, the browser compares them step by step. It stops at the first step where one declaration wins.
| Step | Criterion | Winner |
|---|---|---|
| 1 | Origin and importance | Normal declarations: author beats user beats browser defaults. `!important` flips that order. |
| 2 | Context | For `!important` declarations, the outer context beats the inner one (shadow DOM). |
| 3 | Element-attached styles | Inline `style` attributes beat selector-based rules. |
| 4 | Layers | The winning layer, by layer order. |
| 5 | Specificity | The higher specificity. |
| 6 | Scoping proximity | The scope root closest to the element, for `@scope` rules. |
| 7 | Order of appearance | The declaration that comes last. |
Layers are step 4. They sit above specificity and source order, so a layer decision is final before specificity is compared. Layers sit below origin and importance and below inline styles, so they cannot make an author rule beat !important from a higher origin or an inline style.
A first example
Without layers, the ID selector wins here and the link is blue:
#nav a.link { color: blue; }
.red { color: red; }Now put each rule in a layer. The @layer statement on the first line sets the order, and later layers win:
@layer base, utilities;
@layer base {
#nav a.link { color: blue; }
}
@layer utilities {
.red { color: red; }
}For <a class="link red"> inside #nav, the link is now red. The cascade reaches step 4 and finds that utilities is declared after base. It never compares the specificity of #nav a.link with that of .red.
What layers do not change
Specificity still works inside a single layer. If two rules in base conflict, the cascade skips step 4, because both rules are in the same layer. It then falls through to specificity and source order as usual.
Layers also affect only the rules assigned to them. Rules outside any layer take part in the layer comparison too, and they have their own place in the order. Later chapters cover that, along with how !important changes the direction of layer order.
Exercise
Exercise
A page has @layer theme, overrides;. The theme layer contains #app .btn { background: navy; }. The overrides layer contains .btn { background: teal; }. Both rules match the same button, and the button has no inline style. Which background does it get, and at which cascade step is that decided?
Show answer
The button is teal. The decision is made at the layers step (step 4), because overrides is declared after theme and so wins. The higher specificity of #app .btn is never compared, because specificity is the next step and the cascade has already stopped.
Exercise
Name two things that a layer cannot override, because they are checked before layers in the cascade.
Show answer
Inline style attributes (element-attached styles) and declarations that win on origin and importance, such as an !important declaration from a higher-priority origin. Both are decided before the layers step.