Sub topic 2
Declaring layers with @layer blocks
This chapter assumes you know basic CSS syntax, how selector specificity and source order decide conflicts, and how to inspect applied styles in browser DevTools. It covers the block form of @layer, which is the basic way to create a cascade layer.
The block form
A layer block is an @layer at-rule followed by a name and a set of braces. Every rule inside the braces belongs to that layer.
@layer base {
a { color: royalblue; }
p { line-height: 1.5; }
}The layer name is an identifier, like a class name without the dot. Layer names are only labels for the cascade. They don't match elements and have no effect on selectors.
Any rule that can appear at the top level of a stylesheet can go inside a layer block. That includes style rules, @media, @supports, and @font-face.
A layer decides before specificity does
Layers matter because the cascade compares layer position before it compares specificity. Here is a short example.
@layer base, utilities;
@layer base {
#nav a.link { color: blue; }
}
@layer utilities {
.red { color: red; }
}For <a class="link red"> inside #nav, the link is red. The utilities layer is declared after base, so it wins, even though #nav a.link has much higher specificity than .red. The next chapters cover the ordering rules in detail. For now, remember that a later layer beats an earlier one for normal declarations.
Rules inside a layer behave normally
Inside a single layer, nothing changes. Specificity and source order work as you already know them. A layer only adds a new comparison step between layers.
@layer components {
.card a { color: black; }
a.cta { color: green; }
}Both selectors above sit in the same layer, so .card a.cta is resolved by specificity first. Both selectors have specificity (0,1,1), so the later a.cta rule wins on source order.
Reopening a layer
You can write several blocks with the same name. The browser merges them into one layer, and later blocks append rules to it.
@layer base {
a { color: royalblue; }
}
@layer components {
.card a { color: black; }
}
@layer base {
p { margin-block: 1em; }
}The second base block adds to the first. It does not create a new layer after components. The position of a layer is fixed by where its name first appears in the stylesheet, so base stays before components. Rules in the reopened block are ordered by source order inside base, as usual.
Common mistakes
- Expecting a block to raise priority by itself. Wrapping rules in
@layerdoesn't make them win. Only the layer's position relative to other layers matters. - Mixing layered and unlayered rules by accident. Rules outside any
@layerform their own group, which wins over layered rules for normal declarations. A forgotten layer wrapper can therefore override rules you meant to beat. A later chapter covers this. - Misspelling a layer name.
@layer componets { ... }quietly creates a new layer in a new position. It doesn't throw an error, so check the layer names in DevTools when something looks wrong.
Exercise
In the stylesheet below, what color is <a class="link">, and why?
@layer one {
a.link { color: red; }
}
@layer two {
a { color: blue; }
}
@layer one {
a.link { color: green; }
}
Show answer
The link is blue. The layer one is first declared before two, so two is later in the layer order. The second @layer one block only adds rules to the existing one layer, so it doesn't change the order. Inside one, the green rule beats the red rule on source order. But the layer two beats all of one for normal declarations, so a { color: blue; } wins even though its specificity is lower.