Sub topic 5
Unlayered styles win over layered styles
This chapter assumes you know selector specificity, source order, and how to read the Styles pane in browser DevTools. You should also know how @layer blocks and the layer order statement work.
The rule
Cascade layers add one step to the cascade. For normal (non-!important) declarations, the browser compares origin first, then layer, then specificity, then source order. Styles that are not in any layer are treated as if they sit in one implicit layer that comes after every named layer. That implicit layer has the highest priority for normal declarations.
So an unlayered rule beats a layered rule, even when the layered rule has far higher specificity and appears later in the file.
@layer components {
button { background: gray; }
}
button { background: green; }The button is green. Both selectors have the same specificity here, but that is not the reason. Suppose the layered rule were #app button.primary { background: gray; }. The button would still be green, because layer comparison happens before specificity.
Where unlayered styles rank
The diagram shows the order for normal declarations, from lowest to highest priority.
Layers declared first have the lowest priority. Unlayered styles sit at the far right.
A common surprise
Teams often adopt layers for new code but leave older CSS outside any layer. That older CSS then overrides everything in the layers.
@layer base, components, utilities;
@layer utilities {
.hidden { display: none; }
}
/* legacy.css, not layered */
.card { display: block; }Given <div class="card hidden">, the element is displayed as a block. The .hidden utility lives in a layer, so the unlayered .card rule wins. The utility appears to do nothing, and DevTools shows it crossed out.
Choosing how to respond
You have three practical options.
| Option | When to use it |
|---|---|
| Put the legacy CSS in a layer | You want one consistent system. Use `@layer legacy { ... }` or `@import url("legacy.css") layer(legacy);`. |
| Leave it unlayered on purpose | You want page-specific or one-off overrides to beat every layer. |
| Raise specificity inside layers | Avoid this. It does not help against unlayered CSS. |
The second option is a legitimate design. Many projects put framework and base styles in layers and keep a small unlayered stylesheet for final overrides.
@layer reset, base, components, utilities;
@layer base {
a { color: royalblue; }
}
/* Unlayered: beats every layer above */
a.brand { color: crimson; }Reading it in DevTools
When an expected style is crossed out, check the Styles pane. Chrome and Firefox label rules with their layer name, and unlayered rules show no layer label. If a rule you wrote in a layer loses to a rule with no label, the cause is this chapter's rule, not specificity.
Summary of the working model
- Compare origin and importance.
- For normal declarations, a rule outside any layer beats a rule inside any layer.
- Among layered rules, the later layer wins.
- Only when two rules are in the same layer (or both unlayered) does specificity matter, then source order.
Exercise
Given the CSS below, what color is <p id="intro" class="note">, and why?
@layer theme {
#intro.note { color: blue; }
}
p { color: orange; }
Show answer
Orange. The p rule is unlayered, and unlayered normal declarations beat layered ones. The higher specificity of #intro.note is not considered because the layer comparison comes first.
Exercise
Your .hidden { display: none; } utility is in @layer utilities, but an old unlayered .card { display: block; } overrides it. Name two ways to fix this without using !important.
Show answer
Put the legacy stylesheet in a layer declared before utilities, for example @import url("legacy.css") layer(legacy); with the order legacy, utilities. Alternatively, move the .hidden rule out of the layer so it is also unlayered, and place it after .card (or give it equal or higher specificity).