Sub topic 7
Controlling order with @layer
This chapter assumes you know selectors, specificity, and source order, and that you have seen the problems global CSS causes as a codebase grows. Here you will use @layer to decide which group of styles wins, without raising specificity or adding !important.
The problem layers solve
Normally the cascade decides between two conflicting rules by specificity first, then by source order. A utility such as .p-0 has the same specificity as .card, so it only wins if it appears later in the file. Once files are bundled in a different order, or a component uses a longer selector like .page .card, the utility loses. Teams then reach for !important or longer selectors, and specificity keeps climbing.
A cascade layer is a named group of rules. When two rules conflict, the browser compares their layers before it compares specificity. A rule in a later layer beats a rule in an earlier layer, however specific the earlier selector is.
Declaring the order once
Put one @layer statement at the top of your entry stylesheet. It lists the layers from lowest to highest priority.
@layer reset, base, components, utilities;
@layer reset {
* { box-sizing: border-box; margin: 0; }
}
@layer components {
.card { padding: 1rem; }
}
@layer utilities {
.p-0 { padding: 0; }
}The order is fixed by the first statement. The @layer blocks can then appear in any file, in any order, and still land in the right place. Because utilities is declared last, .p-0 overrides .card without !important.
<div class="card p-0">No padding</div>Rules to remember
- Inside one layer, specificity and source order work as usual.
- Styles that are not in any layer form an implicit final layer. They beat every named layer.
- Declaring
!importantreverses the order. An important rule in an earlier layer beats an important rule in a later layer. - Layer names are case-sensitive.
Mapping layers to your architecture
Layers fit the layered structure from the earlier chapter on ITCSS and SMACSS. The layers run from broad and low-specificity to narrow and high-priority.
| Layer | Contents | Example |
|---|---|---|
| `reset` | Browser normalisation | `* { margin: 0; }` |
| `base` | Element defaults | `a { color: inherit; }` |
| `components` | BEM blocks | `.card`, `.button` |
| `utilities` | Single-purpose overrides | `.p-0`, `.hidden` |
Layering third-party CSS
You can import a library into a layer. This stops its selectors from beating yours.
@layer vendor, base, components, utilities;
@import url("library.css") layer(vendor);Your own rules in base or components now win over the library, even if the library uses more specific selectors. Put @import rules before other rules, though a leading @layer order statement is allowed.
Nested layers
A layer can contain sublayers. Use a dot to refer to one.
@layer components {
@layer card, button;
}
@layer components.card {
.card { padding: 1rem; }
}Keep nesting shallow. Deep trees are hard to read and rarely needed.
Common errors
- Forgetting the order statement. Layers are then ordered by first appearance, which depends on file load order.
- Leaving some styles unlayered, so they override everything.
- Using
!importantinside layers and being surprised by the reversed order. - Expecting layers to scope names. They control priority only, so class names can still collide.
Exercise
A page has @layer components, utilities;. The components layer contains #sidebar .card { padding: 2rem; }. The utilities layer contains .p-0 { padding: 0; }. An element matches both selectors. Which padding applies, and why?
Show answer
padding: 0 applies. The two rules are in different layers, and the browser compares layers before specificity. utilities is declared after components, so it wins, even though the ID selector has much higher specificity.
Exercise
You add @layer to an old project, but an old unlayered .button { background: red; } still overrides your components layer. Why, and what is one fix?
Show answer
Unlayered styles form an implicit layer that beats all named layers. Fix it by wrapping the old rules in a named layer, such as @layer legacy { ... }, and listing that layer early in the order statement.