Sub topic 3
Setting layer order with an @layer statement
This chapter assumes you know the block form of @layer, selector specificity, and source order. Here you will learn how to fix the priority of every layer in one line.
Why order needs its own statement
Layer order is set by the first time the browser sees each layer name. With block rules alone, the order depends on which file or block happens to load first. That is fragile: moving a stylesheet can change which layer wins.
The statement form removes the guesswork. It lists layer names and has no rules inside.
@layer reset, base, components, utilities;This line creates four layers and sets their order. Later layers have higher priority for normal declarations. So utilities beats components, which beats base, which beats reset.
A complete example
Put the statement first. The blocks below can then appear in any order.
@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; }
}For <a class="text-red"> inside a .card, the link is red. Both .card a and .text-red match. .card a has higher specificity (0,1,1 against 0,1,0), but utilities is declared after components, so the layer wins first. Specificity only matters between rules in the same layer.
How the browser builds the order
The browser reads the stylesheet from top to bottom and records each layer name the first time it appears. Three rules follow from this:
- A name listed twice keeps its first position. Later mentions are ignored for ordering.
- Reopening a layer with
@layer base { ... }adds rules to the existing layer. It does not move it. - A layer first mentioned in a block, with no earlier statement, goes to the end of the list so far.
@layer components {
.btn { padding: 1rem; }
}
@layer base, components;
@layer base {
.btn { padding: 2rem; }
}The first block creates components first. The statement then adds base after it. The final order is components, base, so base wins and .btn gets 2rem of padding. The statement came too late to put base first.
Statements can come before @import
@import rules must come near the top of a stylesheet, but an @layer statement is allowed before them. This lets you fix the order before any imported file loads. The @import ... layer() syntax is covered in a later chapter.
@layer vendor, site;
@import url("vendor.css") layer(vendor);Common errors
- Statement placed too late. Any block that names a layer earlier decides its position. Keep the statement at the very top of the entry stylesheet.
- Several statements that disagree. Only the first mention counts, so a second statement with a different order silently does nothing for names already seen.
- Typos in names.
@layer componentand@layer componentsare two different layers. The typo creates a new layer at the end, where it overrides everything. Copy names from the statement. - Expecting source order to still matter between layers. It does not. Source order only breaks ties inside one layer.
Check your understanding
Exercise
Given the CSS below, what is the final order of layers from lowest to highest priority, and what color is a paragraph? @layer theme { p { color: green; } } @layer base, theme; @layer base { p { color: orange; } }
Show answer
The order is theme, then base, so base has the higher priority. The first block mentions theme first, and the later statement only adds base after it. The paragraph is orange because the base layer comes later and wins. Specificity is the same here, but it would not matter between layers anyway.