Sub topic 13
Debugging layers and avoiding common pitfalls
This chapter assumes you know selector specificity, source order, the @layer block and statement forms, and how to read the Styles pane in browser DevTools. When a layered style does not apply, the cause is nearly always one of a few things, and you can check them in a fixed order.
A debugging checklist
Work through these questions in order.
- Is the losing rule or the winning rule unlayered? Unlayered normal declarations beat all layered normal declarations.
- Is either declaration
!important? If so, the layer order is reversed for those declarations. - Which layer is each rule in, and where does that layer sit in the order?
- If both rules are in the same layer, compare specificity, then source order, as usual.
Pitfall 1: a stray unlayered rule
Adding layers to a project does not help if some rules stay outside them.
@layer base, utilities;
@layer utilities {
.text-red { color: red; }
}
a.nav-link { color: blue; }An element with both nav-link and text-red is blue. The a.nav-link rule is unlayered, so it beats the utilities layer even though utilities is the highest layer. Move the rule into @layer base { ... } and the utility wins.
Pitfall 2: layer order set by accident
A layer's position is fixed by the first time its name appears. If you skip the order statement, the order depends on which file loads first. Declare the order once, at the top of the first stylesheet:
@layer reset, base, components, utilities;Later blocks can then appear in any file order without changing which layer wins.
Pitfall 3: forgetting that `!important` reverses order
Among !important declarations, the earlier layer wins. Unlayered !important declarations lose to layered ones.
@layer base, overrides;
@layer base {
button { color: red !important; }
}
@layer overrides {
button { color: blue !important; }
}These buttons are red. If you add !important to a utility class and it does not win, you have probably put it in the layer that now loses.
Pitfall 4: typos in nested layer names
Dot notation reopens an existing nested layer only if the name matches exactly. A misspelling silently creates a new layer.
@layer framework {
@layer base, theme;
}
@layer framework.thme {
p { color: navy; }
}Here framework.thme is a third layer inside framework, placed after theme. It does not add rules to theme. Copy layer names from the order statement instead of retyping them.
Pitfall 5: `@import` in the wrong place
An @import rule must come before all other rules, except @charset and @layer statements. Put the order statement first, then the imports:
@layer vendor, site;
@import url("vendor.css") layer(vendor);If an @import follows a normal rule, the browser ignores it. The vendor styles never load, and the cause is easy to miss.
Using DevTools
In current browsers, the Styles pane shows the layer name next to each rule. Look for these signs:
- A crossed-out rule with a layer label that sits earlier in the order than the winner.
- A winning rule with no layer label. It is unlayered.
- A layer name you did not expect, such as a misspelled nested layer.
To test a theory, toggle !important off in the pane or edit the order statement in the stylesheet. Then check whether the winner changes.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Utility class loses to a plain rule | The plain rule is unlayered | Put it in a layer |
| Layer order changes between pages | No order statement | Declare the order first |
| `!important` utility loses | Reversed order for important | Move it to an earlier layer |
| Nested rules ignored | Misspelled dot-notation name | Match the declared name |
| Vendor CSS missing | `@import` after other rules | Move `@import` up |
Exercise
A page has @layer base, components;, then @layer components { .btn { background: gray; } }, then an unlayered button { background: green; }. A <button class="btn"> renders green. Why, and what is one fix that keeps the green rule's intent?
Show answer
The unlayered button rule beats any layered normal declaration, regardless of specificity, so it wins over the components layer. One fix is to move the green rule into a layer, such as @layer base { button { background: green; } }. Then components is later in the order, so .btn wins and the button is gray.