Sub topic 12
Anonymous layers
An anonymous layer is a cascade layer that has no name. This chapter covers how to create one, where it lands in the layer order, and what you give up by leaving the name out. It also covers when that trade is worth making.
This chapter assumes you know the following:
- Block
@layerrules and the@layer a, b, c;order statement. - The rule that layer order beats specificity.
- Unlayered styles beating layered styles for normal declarations.
- How
@importworks.
What makes a layer anonymous
You create an anonymous layer by writing @layer with a block and no name:
@layer {
.note { color: gray; }
}The rules inside take part in the cascade like any other layer's rules. The layer just has no identifier, so no later rule can refer to it.
There are two ways to get one:
- A block
@layer { ... }with no name. - An import with the
layerkeyword and no parentheses:@import url("legacy.css") layer;.
The statement form needs names. @layer base, , utilities; and a bare @layer; are both invalid, because a statement exists to declare names.
Where an anonymous layer sits in the order
Layer order is set by the first appearance of each layer. An anonymous layer is added to the order at the point where it appears in the source, so it gets a position like any named layer.
Consider this sheet:
@layer base, components;
@layer {
.note { color: gray; }
}
@layer components {
.note { color: black; }
}The order statement fixes base first and components second. The anonymous block appears after the statement, so it is appended last. The final order, from lowest to highest priority, is base, components, anonymous. A <p class="note"> is gray, because the anonymous layer outranks components. The components block comes later in the file, but reopening a layer does not change its position.
This is the most common surprise with anonymous layers. If you write one near the end of a file and never declare the named layers after it, it ends up above all of them.
Unlayered styles still beat every layer, anonymous or not, for normal declarations. The anonymous layer gains nothing there.
Every anonymous block is a separate layer
A named layer can be reopened. If you write @layer utilities { ... } twice, both blocks go into the same layer. Anonymous layers cannot work this way, because there is no name to match. Each @layer { ... } creates a new layer.
@layer {
#page .title { color: red; }
}
@layer {
.title { color: blue; }
}These are two layers, and the second is later in the order. Titles are blue, even though #page .title has far higher specificity. Layer order decides first, and specificity only matters inside a single layer.
Two anonymous blocks side by side therefore do not share a scope. A rule in the first cannot be overridden by a later rule written inside the first block's "layer", because that layer is closed. You can only override it from a later layer, or from unlayered styles.
Anonymous layers inside other layers
Anonymous layers can be nested. The same ordering rule applies within the parent layer.
@layer framework {
@layer {
a { color: red; }
}
@layer theme {
a { color: blue; }
}
}Inside framework, the anonymous child comes first and theme comes second. Links are blue. If you swapped the two blocks, links would be red.
You cannot reach the anonymous child with dot notation. @layer framework.theme works because theme has a name. There is no name you could write to target the anonymous one.
Anonymous imports
The most practical use of an anonymous layer is containing an imported stylesheet when you never need to refer to it again.
@import url("legacy.css") layer;
@layer site {
.btn { padding: 0.5rem 1rem; }
}Everything in legacy.css goes into a new anonymous layer. It is first in the order, so site outranks it. Because the import appears before any other layer, the anonymous layer is also lowest in priority, which is usually what you want for old or third-party CSS.
Imports have strict placement and syntax rules:
@importrules must come before all other rules, except@charsetand@layerstatements.- After the URL, the order is
layer(orlayer(name)), thensupports(...), then any media query. - A name inside the parentheses makes the layer named. Leaving it out makes it anonymous.
This import adds a condition:
@import url("grid-fallback.css") layer supports(not (display: grid)) screen;The file loads into an anonymous layer only when the browser lacks grid support and the medium is a screen.
If you write a layer-order statement first, the import still lands where it appears relative to named layers. The statement fixes the named layers' order, and the anonymous layer is appended after them:
@layer reset, base, utilities;
@import url("legacy.css") layer;Here legacy.css is above utilities. That placement is the opposite of what the previous example gives, and it is easy to do by accident.
What you give up
Leaving the name out removes several abilities.
| Capability | Named layer | Anonymous layer |
|---|---|---|
| Reopen later to add rules | Yes | No, each block is new |
| Appear in an order statement | Yes | No |
| Reference with dot notation | Yes | No |
| Position fixed in advance | Yes | No, set by where it appears |
| Participates in layer order and specificity rules | Yes | Yes |
| Affected by `!important` reversal | Yes | Yes |
| Usable with `revert-layer` | Yes | Yes |
The last three rows matter because an anonymous layer is not a special case. It follows the same cascade rules. Only its addressability differs.
!important and revert-layer still work
For !important declarations, layer order reverses. An anonymous layer that is highest for normal declarations becomes lowest for important ones:
@layer base;
@layer {
button { color: red !important; }
}
@layer base {
button { color: blue !important; }
}base was declared first, so it ranks lower for normal declarations and higher for important ones. Buttons are blue. The anonymous layer's important declaration loses, even though it is later in the file.
revert-layer also works from inside an anonymous layer. It rolls the property back to the value from the previous layer in the order:
@layer base;
@layer base {
button { padding: 1rem; }
}
@layer {
button { padding: 2rem; }
button.plain { padding: revert-layer; }
}For button.plain, the anonymous layer's padding is discarded and the value falls back to 1rem from base.
When to use anonymous layers
Anonymous layers fit a narrow set of cases:
- Throwaway containment. You want a block of CSS to rank below your unlayered styles without designing a layer structure.
- One-off imports. A legacy or third-party file that you will never target by name.
- Generated or embedded CSS. Tooling that wraps output in a layer and does not need anyone to extend it.
Prefer a named layer when any of the following is true:
- Other files will add rules to the same group.
- You want the position to be readable in one place, in the order statement.
- Teammates need to find the layer in DevTools and in the code.
- You may later need to reorder it.
The order statement is the main reason to avoid anonymous layers in a project architecture. It documents priority in one line. An anonymous layer's position is implicit in the file order, so it moves if someone reorders imports or files.
Debugging anonymous layers
Browser DevTools list the layer for each matched rule in the Styles pane. Named layers show their name. An anonymous layer shows a generic label instead, so you cannot tell two anonymous layers apart by name. You have to tell them apart by the source file and line.
Check these causes when a rule behaves unexpectedly:
- Wrong position. The anonymous block appears after your order statement and therefore beats every named layer. Move it, or give it a name.
- Expected shared scope. You wrote two
@layer { }blocks and expected the later one to extend the earlier one. They are separate layers, and the later one wins outright. - Import after statement. An anonymous
@import ... layer;placed after a statement is highest, not lowest. - Unlayered override. An unlayered rule beats the anonymous layer for normal declarations. Check for unlayered rules before blaming layer order.
Exercise
Given the CSS below, what color is <p class="msg" id="top">, and why?
@layer base, theme;
@layer theme {
p.msg { color: green; }
}
@layer {
.msg { color: orange; }
}
@layer base {
#top { color: purple; }
}
Show answer
Orange. The order statement fixes base then theme. The anonymous block first appears after the statement, so it is appended last and outranks both. Specificity is not compared across layers, so the #top rule in base and the p.msg rule in theme both lose. Reopening base later does not move it.
Exercise
You import a vendor stylesheet with @import url("vendor.css") layer; after the statement @layer reset, base, components;. Your own .btn rules are in components. Vendor rules override yours. Give two ways to fix it without changing any selector.
Show answer
Move the @import above the order statement, so the anonymous layer is created first and ranks lowest. Alternatively, name the layer and list it first in the statement: @layer vendor, reset, base, components; with @import url("vendor.css") layer(vendor);. Both put the vendor CSS below components.