Sub topic 10
Nested layers and dot notation
This chapter assumes you can write block @layer rules and set top-level order with an @layer statement. It also assumes you know that a later layer beats an earlier one regardless of specificity.
Layers inside layers
A layer can contain other layers. You write an @layer rule inside another @layer block. This is useful when a library ships its own internal structure and you want to keep that structure intact.
@layer framework {
@layer base, theme;
@layer base {
p { margin: 0; }
}
@layer theme {
p { color: #333; }
}
}The statement @layer base, theme; inside framework sets the order of the nested layers. It works like a top-level order statement, but it only applies inside framework. Here theme comes after base, so theme wins when they conflict.
How nested order is resolved
Each layer is ordered among its siblings, and the parent's position decides how it compares with the outside world. A nested layer never moves ahead of a layer that its parent loses to.
@layer framework, app;
@layer framework {
@layer base, theme;
}
@layer app {
p { color: black; }
}
@layer framework {
@layer theme {
p { color: gray; }
}
}Paragraphs are black. framework.theme is the last nested layer, but its parent framework comes before app. Everything in framework therefore loses to everything in app.
Dot notation
You reach a nested layer by joining the parent and child names with a dot. The dotted name works anywhere a layer name works.
@layer framework.theme {
p { color: navy; }
}This reopens the existing framework.theme layer and appends a rule. In the first example above, theme already contains p { color: #333; }. Both rules are in the same layer, so the normal tie-breakers apply: specificity, then source order. The later rule wins, and paragraphs end up navy.
You can also use dotted names in an order statement:
@layer framework.base, framework.theme;If framework does not exist yet, this statement creates it, and then creates the two nested layers inside it. You can also use a dotted name in an import:
@import url("theme.css") layer(framework.theme);Rules directly inside a parent
A parent layer can hold rules that are not inside any nested layer. Those rules behave like an unnamed layer placed after all the nested layers. For normal declarations, they beat every named nested layer in that parent.
@layer framework {
@layer base, theme;
@layer theme {
a { color: teal; }
}
a { color: crimson; }
}Links are crimson. The rule sits directly in framework, so it ranks above framework.theme. This is the same idea as unlayered styles beating layered ones, applied one level down.
Common mistakes
- Writing
framework > themeorframework/theme. Only the dot is valid. - Expecting nested order to affect outside layers. Nesting only orders siblings. The parent's position decides the rest.
- Reordering by reopening. Reopening a layer does not move it. The first mention of a layer name fixes its position, whether that mention is a statement, a block, or an import.
- Forgetting that
!importantreverses order. The reversal applies at each level of nesting, so an important rule inframework.basebeats an important rule inframework.theme.
Exercise
Given the CSS below, what color are paragraphs, and why? @layer a, b; @layer a { @layer x, y; @layer y { p { color: red; } } } @layer b { @layer x { p { color: blue; } } } @layer a.x { p { color: green; } }
Show answer
Blue. Layer b comes after layer a, so everything in b beats everything in a, including a.y. The a.x rule that adds green is in layer a, so it also loses to b.x.