Sub topic 10
Nested layers and dot notation
This chapter assumes you know the basic @layer block and statement forms, how layer order is set, and that layer order beats specificity. It also assumes you can read the Styles panel in DevTools. Here you will put layers inside other layers, reopen them from outside with dot notation, and predict the final order.
Layers can contain layers
A layer block can hold other @layer rules. The inner layers are nested layers. They are useful when a stylesheet, often a framework, has its own internal ordering that should stay private to it.
@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 the top-level order statement, but only inside the parent. Here theme beats base.
A nested layer's full name is its parent's name, a dot, then its own name. These two layers are framework.base and framework.theme. Layer names cannot contain a dot themselves. The dot always means "child of".
Reopening a nested layer with dot notation
You can add rules to a nested layer from outside its parent. Write the full dotted name:
@layer framework {
@layer base, theme;
@layer base {
p { margin: 0; }
}
@layer theme {
p { color: #333; }
}
}
@layer framework.theme {
p { color: navy; }
}The last block appends to the existing framework.theme layer. Both p rules are in the same layer and have the same specificity. Source order breaks the tie, so paragraphs are navy.
This is how you adjust one part of a framework without editing the framework's file. You also do not need to know how the framework nests its layers internally, as long as you know the layer's full name.
Dot notation works wherever a layer name is accepted:
- In a block:
@layer framework.theme { ... } - In an order statement:
@layer framework.base, framework.theme; - In an import:
@import url("theme.css") layer(framework.theme);
How the order is computed
Layers form a tree. The browser flattens the tree into one precedence list for normal declarations. It follows three rules:
- Sibling layers are ordered by the order in which they were first declared. Later wins.
- A nested layer is ranked only against its siblings, inside its parent. It never competes directly with layers outside the parent.
- A parent layer's own direct rules, those not inside any nested layer, form an implicit last layer inside the parent. They beat every nested layer of that parent.
Consider this structure:
@layer reset, framework, site;
@layer framework {
@layer base, theme;
}The flattened order, from lowest to highest priority, is:
| Priority | Layer | Notes |
|---|---|---|
| 1 (lowest) | `reset` | Declared first at top level |
| 2 | `framework.base` | First nested layer in `framework` |
| 3 | `framework.theme` | Second nested layer in `framework` |
| 4 | `framework` (direct rules) | Implicit last layer inside the parent |
| 5 | `site` | Declared after `framework` at top level |
| 6 (highest) | Unlayered styles | Beat all layers for normal declarations |
The key consequence is that site beats every framework layer, including framework.theme. The parent's position decides the contest against outside layers. The nested order matters only among the parent's children.
Direct rules in a parent beat its nested layers
Rule 3 is the one people forget. Look at this example:
@layer framework {
@layer base, theme;
@layer theme {
p { color: gray; }
}
p { color: black; }
}The paragraph is black. The bare p rule sits directly in framework, so it ranks above framework.theme. Source order does not matter here. If you moved p { color: black; } above the nested blocks, it would still win.
This is the same principle as unlayered styles beating layered ones, applied one level down. Each layer treats its own direct rules as the highest part of itself.
First mention fixes the order
A layer's position is set the first time its name appears anywhere. That includes statements, blocks, and imports. Reopening it later never changes its position. This matters most with dot notation, because a dotted name can appear before the parent's own block.
@layer framework.theme, framework.base;
@layer framework {
@layer base, theme;
}The first statement creates framework, then creates theme and base inside it, in that order. The later @layer base, theme; does not reorder them. Both names already exist, so the statement does nothing. The result is that framework.base beats framework.theme, which is the opposite of what the second block suggests.
A related case is a dotted name whose parent does not exist yet. The browser creates the parent at that point in the order:
@layer reset, site;
@layer framework.base {
p { margin: 0; }
}framework is created after site, because that is where the name first appears. It therefore beats site. If you meant framework to sit between reset and site, put it in the first statement:
@layer reset, framework, site;Importing into a nested layer
@import accepts a dotted name. This puts third-party CSS inside a specific child of a larger structure:
@layer reset, vendor, site;
@layer vendor.ui, vendor.icons;
@import url("ui-kit.css") layer(vendor.ui);
@import url("icon-font.css") layer(vendor.icons);
@layer site {
.btn { padding: 0.5rem 1rem; }
}The @layer statements may come before @import. Any other rule before an import makes the import invalid. Both vendor files are under vendor, so site beats both, and vendor.icons beats vendor.ui.
If the imported file already contains its own @layer rules, those become nested inside the layer you named. A file with @layer base, theme; imported as layer(vendor.ui) produces vendor.ui.base and vendor.ui.theme. Dot notation is not limited to two levels.
`!important` reverses the order at every level
For !important declarations, the order is reversed. This holds for the nested tree as well. For important declarations the priority runs from lowest to highest as follows:
- Unlayered
!important - Direct rules of the parent
- Nested layers, last declared to first declared
- Earlier top-level layers beat later ones
In practice, framework.base !important beats framework.theme !important, and framework as a whole beats site for important declarations if framework was declared first. Nested layers do not escape the reversal.
Check your understanding of the reversal with this example:
@layer a, b;
@layer a {
@layer x, y;
@layer x { p { color: red !important; } }
@layer y { p { color: blue !important; } }
}
@layer b {
p { color: green !important; }
}The paragraph is red. Layer a is earlier than b, so it wins for important declarations. Inside a, x is earlier than y, so x wins.
Common mistakes
- Expecting a nested layer to compete with outside layers. A nested layer ranks only inside its parent. To raise
framework.themeabovesite, you cannot. Move the rule intosite, or reorderframeworkandsiteas whole layers. - Trying to reorder with a later statement. Order is fixed on first mention. Fix the earliest mention instead.
- Forgetting the parent's direct rules. Rules written directly in
frameworkbeat its nested layers. If a theme override seems ignored, look for a bare rule in the parent. - Typing a slightly different name.
framework.Themeandframework.themeare different layers, because names are case-sensitive. A typo silently creates a new layer at the top of the parent's list. In DevTools, look for an unexpected layer name next to the rule.
Summary
- Nested layers are declared inside a parent layer and ordered by a statement inside it.
- Dot notation reopens or declares a nested layer from outside, in blocks, statements, and imports.
- Nested layers rank only against their siblings. The parent's position decides the contest with outside layers.
- A parent's direct rules beat all its nested layers for normal declarations.
- Order is set on first mention and cannot be changed afterward.
!importantreverses the order at each level.
Exercise
Given the CSS below, what color is a paragraph, and why? @layer ui, site; @layer ui { @layer base, theme; @layer theme { p { color: orange; } } } @layer site { p { color: teal; } } @layer ui.base { p { color: pink; } }
Show answer
Teal. The layer site is declared after ui, so site beats every layer inside ui, including ui.theme. The last block only appends to ui.base, which is already the lowest layer in ui, and it does not change any order.
Exercise
A stylesheet has @layer ui.theme, ui.base; as its first line, followed later by @layer ui { @layer base, theme; }. Which nested layer wins for normal declarations, and how would you get the other order?
Show answer
ui.base wins, because ui.theme was mentioned first and so ranks lower. The later statement does not reorder layers that already exist. To make theme win, change the first line to @layer ui.base, ui.theme;, or declare @layer ui { @layer base, theme; } before any dotted mention.
Exercise
In the CSS below, what color is the link? Give the answer for normal declarations as written, then for the case where both color declarations also end with !important.
@layer lib { @layer a, b; @layer a { a { color: red; } } @layer b { a { color: blue; } } a { color: green; } }
Show answer
As written, green. The direct rule in lib beats its nested layers a and b. With !important on all three declarations, the order reverses. The direct rule loses to the nested layers, and the earlier nested layer wins, so the link is red (from lib.a).