Sub topic 6
A practical layer structure: reset, base, components, utilities
This chapter assumes you know selector specificity and source order, and that you have seen the @layer block and statement forms. Here you will put them together into a layer structure you can reuse in a real project.
Why a fixed structure helps
Without layers, you win overrides by raising specificity or reordering files. With layers, you decide the priority once and write each rule in the layer where it belongs. A common scheme uses four layers, from lowest to highest priority:
| Layer | Purpose | Typical selectors |
|---|---|---|
| `reset` | Remove browser inconsistencies | `*`, `body`, `img` |
| `base` | Default look of plain elements | `a`, `h1`, `p` |
| `components` | Styles for UI pieces | `.card`, `.btn` |
| `utilities` | Single-purpose overrides | `.text-red`, `.hidden` |
The order runs from most general to most specific in intent. A later layer beats an earlier one, so a utility class can always override a component, no matter how specific the component's selector is.
Declare the order first
Put one @layer statement at the top of your entry stylesheet. It fixes the priority of every layer, so the blocks below can appear in any order or in any file.
@layer reset, base, components, utilities;
@layer reset {
* { margin: 0; box-sizing: border-box; }
}
@layer base {
a { color: royalblue; }
}
@layer components {
.card a { color: black; }
}
@layer utilities {
.text-red { color: red; }
}Consider this markup:
<div class="card">
<a href="/docs" class="text-red">Docs</a>
</div>Three rules match the link. a sets blue, .card a sets black, and .text-red sets red. The .card a rule has higher specificity than .text-red, but the link is red. The utilities layer comes last in the order statement, so it wins before specificity is compared.
How the pieces relate
Where to put unlayered CSS
Unlayered normal declarations beat every layered normal declaration. That makes unlayered CSS a poor fit for general styles in a layered project, because it silently overrides your utilities.
@layer components {
button { background: gray; }
}
button { background: green; }Here the buttons are green. The unlayered rule wins even though button has the same specificity as the layered one, and it would still win if the layered selector were far more specific. For this reason, once you adopt layers, move all your own CSS into them. Leave unlayered only the styles you want to win on purpose, such as a quick one-off patch.
Choosing the right layer
When you add a rule, ask what kind of change it is:
- Does it neutralize a browser default for every element? Use
reset. - Does it set how a bare element such as
aorh1looks everywhere? Usebase. - Does it style a named piece of UI? Use
components. - Does it do one job that should always win, like hiding or coloring? Use
utilities.
Common mistakes
- Writing the order statement after the blocks. A layer's position is set the first time its name appears. If a block like
@layer components { ... }appears before the statement,componentsis ranked before the layers listed later, which may not be what you want. Put the statement first. - Misspelling a layer name.
@layer utilites { ... }creates a new, separate layer at the end of the order, and yourutilitieslayer does not receive the rule. - Leaving some rules unlayered. They override the whole structure.
Exercise
Given @layer base, components, utilities;, a rule #sidebar .btn { color: black; } in components, and .btn-accent { color: orange; } in utilities, what color is <a class="btn btn-accent"> inside #sidebar, and why? What changes if .btn-accent is moved out of any layer?
Show answer
It is orange, because utilities is later in the layer order and layer order is checked before specificity, so the ID selector in components does not help. If .btn-accent is unlayered it still wins, because unlayered normal declarations beat all layered ones, so the link stays orange.
Exercise
A teammate adds .card a { color: black; } to your project, but outside any @layer. A utility class .text-red in the utilities layer no longer colors links inside cards. Why, and what is the fix?
Show answer
The unlayered rule beats every layered normal rule, including utilities. The fix is to move the rule into the components layer, for example @layer components { .card a { color: black; } }.