Sub topic 2
Declaring layers with @layer blocks
This chapter covers the block form of @layer. You write a name, then a pair of braces that holds ordinary CSS rules. It assumes you already know the following:
- Basic CSS syntax: selectors, properties, declarations.
- Selector specificity and source order.
- How to link or import a stylesheet.
- How to inspect applied styles in browser DevTools.
The previous chapter placed layers inside the cascade. This chapter shows how to create them and how the browser decides their order when you only use blocks. The statement form, which sets order explicitly, is the next chapter.
What a layer block is
A layer block is an @layer at-rule with a name and a body. The body can hold style rules and nested at-rules such as @media. Every rule inside belongs to that layer.
@layer base {
body {
font-family: system-ui, sans-serif;
line-height: 1.5;
}
a {
color: royalblue;
}
}The two rules above are in a layer named base. Nothing else changes about them. Selectors, properties, and values work as usual. The layer only adds a new step to the cascade: before the browser compares specificity, it compares which layer each competing declaration came from.
The body of a layer block must contain rules, not bare declarations. This is not valid:
@layer base {
color: red; /* ignored: declarations need a selector */
}Layer names
A layer name is an identifier, using the same character rules as a class name without the leading dot. Some points to know:
- Names are case-sensitive.
Baseandbaseare two different layers. - A dot in a name means nesting.
framework.themerefers to a layer calledthemeinsideframework. Later chapters cover nesting. For now, avoid dots in names you choose. - A name is optional. A block with no name creates an anonymous layer, which nobody can add to later. That also has its own chapter.
Pick short, lowercase names that describe a role, such as reset, base, components, or utilities.
How the browser orders layers declared as blocks
If you never write an order statement, the layer order is the order in which each layer name first appears in the CSS. The first layer you mention is the lowest priority. The last one is the highest. For normal declarations, a later layer beats an earlier one.
@layer base {
#nav a.link {
color: blue;
}
}
@layer utilities {
.red {
color: red;
}
}With this markup:
<nav id="nav">
<a class="link red" href="/">Home</a>
</nav>the link is red. #nav a.link has far higher specificity than .red, but utilities came after base, so it wins. Specificity is never compared between the two rules, because the layer decides first.
This diagram covers normal declarations only. Unlayered styles and !important change the result, and both have their own chapters.
Rules inside one layer still use the old rules
Layers do not turn off specificity. They only add a comparison that happens before it. Inside a single layer, the cascade works the same way it always has:
@layer base {
a {
color: green;
}
#nav a {
color: blue;
}
}A link inside #nav is blue, because both rules are in base and the ID selector has higher specificity. Layers help you across groups of rules, not inside a group.
Reopening a layer
You can write more than one block with the same name, in the same file or in different files. The browser merges them into one layer. The layer keeps the position it got when its name first appeared. A later block appends rules to it and does not move it.
@layer base {
body {
margin: 0;
}
}
@layer components {
.btn {
padding: 0.5rem 1rem;
}
}
@layer base {
a {
color: royalblue;
}
}The final order is base, then components. The second base block is still part of base, so the a rule has lower priority than any components rule. Inside base, the a rule counts as coming after the body rule for source-order purposes.
This is useful because you can split a layer across files. It is also the source of the most common bug in this chapter, covered below.
Layer blocks and other at-rules
A layer block can contain @media, @supports, and @container rules. The conditional rules stay in that layer.
@layer components {
.nav {
display: block;
}
@media (min-width: 40rem) {
.nav {
display: flex;
}
}
}You can also place @layer inside a conditional rule. The layer is then only created when the condition is true:
@media print {
@layer print {
nav {
display: none;
}
}
}Be careful with this pattern. Layer order depends on first appearance, and a layer that appears only under a media condition may not take part in ordering the way you expect. Prefer putting the @media inside the layer, as in the earlier example.
Placing blocks relative to @import
@import rules must come before all other rules, with two exceptions: @charset and the statement form of @layer. A layer block counts as a normal rule, so an @import that follows it is invalid and ignored.
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@layer site {
.btn {
padding: 0.5rem 1rem;
}
}This works because the first line is a statement, not a block. Putting @layer site { ... } above the @import would silently break the import. Importing into layers is covered in a later chapter. For now, remember that blocks go after imports.
A worked example across two files
Suppose a project has two stylesheets, loaded in this order:
<link rel="stylesheet" href="components.css">
<link rel="stylesheet" href="base.css">components.css:
@layer components {
.card a {
color: black;
}
}base.css:
@layer base {
a {
color: royalblue;
}
}The order is components then base, because components.css loaded first. Now base has higher priority, and links inside cards turn royal blue instead of black. The developer wrote the rules correctly. Only the file order was wrong.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A rule in a later-loaded file loses to an older one | The older file introduced a layer name that appears later in the cascade order | Check the order in which layer names first appear |
| Two near-identical layers behave as different groups | Case or spelling differs (`Base` vs `base`) | Use one consistent lowercase name |
| An imported stylesheet has no effect | A layer block appears before the `@import` | Move imports to the top, or use a statement before them |
| Declarations in the layer have no effect | Declarations were written directly in the block with no selector | Wrap them in a rule |
| A rule's higher specificity does not help | The rule is in an earlier layer | Move the rule to a later layer, not to a more specific selector |
Checking layers in DevTools
Current Chromium-based browsers and Firefox label the rules in the styles panel with their layer name, and struck-out declarations that lost to a later layer are easy to spot. Open the element, find the crossed-out declaration, and look at the layer label beside the rule. If the winning rule has a later layer, the cause is layer order and not specificity. Also check browser support: layers are part of every major browser engine released since early 2022.
Summary
@layer name { ... }puts the rules in its body into the named layer.- The order of layers is the order in which their names first appear.
- A later layer beats an earlier one for normal declarations, regardless of specificity.
- Inside one layer, specificity and source order work as before.
- Blocks with the same name merge, and the layer keeps its original position.
- Put
@importrules before layer blocks.
Exercise
A page loads a.css and then b.css. a.css contains @layer theme { p { color: blue; } }. b.css contains @layer base { p { color: red; } } followed by @layer theme { p { color: green; } }. What color are paragraphs, and why?
Show answer
Paragraphs are green. Layer order comes from first appearance, so theme (first seen in a.css) comes before base. That makes base the higher-priority layer, which would normally make p red. But b.css later adds a second theme block, and that block merges into the existing theme layer without changing its position. So the winning rule is the red one in base, and paragraphs are red. Green never wins, because it sits in the lower-priority theme layer. The correct answer is red.
Exercise
In one layer named base, you have .btn { color: red; } followed by button { color: blue; }. Both match <button class="btn">. Which color applies, and does moving the rules into separate layers change the answer?
Show answer
The button is red while both rules are in base. The class selector has higher specificity than the type selector, and layers do not change comparison within one layer. If you move them into two different layers, the later layer wins. For example, with button { color: blue; } in a later layer than .btn, the button turns blue.