Sub topic 12
Anonymous layers
This chapter assumes you know how to declare named layers with @layer name { ... }, how an @layer a, b; statement sets layer order, and that a later layer beats an earlier one regardless of specificity. It covers layers that have no name.
What an anonymous layer is
If you leave out the name, the browser still creates a layer. It is called an anonymous layer.
@layer {
p { color: crimson; }
}An anonymous layer follows the same cascade rules as a named one. The only difference is that you cannot refer to it again. You can't add rules to it later, list it in an order statement, or target it with dot notation.
Where it lands in the order
A layer's position is fixed by the first time the browser sees it. An anonymous layer is always "first seen" where it is written, so its position depends only on its place in the source.
@layer base, theme;
@layer base {
p { color: black; }
}
@layer theme {
p { color: teal; }
}
@layer {
p { color: crimson; }
}The order statement creates base and then theme. The anonymous layer appears after them, so it is the highest layer. Paragraphs are crimson. An unlayered p rule would still beat all three layers, because unlayered normal declarations win over layered ones.
Every anonymous block is a new layer
Named blocks with the same name are merged into one layer. Anonymous blocks never merge. Each @layer { ... } creates its own layer, placed after the layers that came before it.
@layer {
p.lead { color: red; }
}
@layer {
p { color: blue; }
}For <p class="lead">, the text is blue. The second block is a later layer, and layer order beats specificity. Within one layer, p.lead would have won.
Anonymous layers from @import
You can import a stylesheet into an anonymous layer by writing layer with no parentheses.
@layer reset, base;
@import url("widgets.css") layer;
@layer base {
.btn { padding: 0.5rem 1rem; }
}Here widgets.css becomes an anonymous layer. It is first seen after reset and base were created by the order statement, so it ranks above both. Compare this with layer(vendor), which gives the imported file a name you can place in an order statement. Remember that @import rules must come before other rules, except @layer statements and @charset.
Nested anonymous layers
Anonymous layers can sit inside named layers and the other way round. The anonymous layer is a child of its parent, and the parent's position still controls how it ranks against other top-level layers.
@layer components {
@layer {
.card { border: 1px solid gray; }
}
.card { border-color: black; }
}Inside components, the unlayered .card rule beats the anonymous child layer. The same "unlayered wins" rule applies at each level of nesting.
When to use them
Anonymous layers fit a narrow set of cases:
- Wrapping a one-off block, such as a generated stylesheet, so that it ranks below your unlayered CSS.
- Importing a file you will never need to reorder.
- Quick experiments.
For anything shared across a project, prefer named layers. A name lets you list the layer in your order statement, reopen it from another file, and find it in DevTools.
Summary table
| Feature | Named layer | Anonymous layer |
|---|---|---|
| Can be reopened later | Yes | No |
| Appears in an order statement | Yes | No |
| Position decided by | First appearance or order statement | First appearance only |
| Same block written twice | Merged into one layer | Two separate layers |
| Import syntax | `layer(name)` | `layer` |
Exercise
Given the code below, what color is <p class="note">, and why?
@layer base, theme;
@layer theme { p { color: green; } }
@layer { p.note { color: orange; } }
@layer base { p.note { color: purple; } }
Show answer
Orange. The order statement creates base first and theme second. The anonymous layer first appears after both, so it is the highest layer, and layer order beats specificity. The later @layer base block only adds to the existing base layer. It does not move it. No unlayered rule exists, so the anonymous layer wins.