Sub topic 3
Setting Layer Order with an @layer Statement
This chapter assumes you know basic CSS syntax, selector specificity, and source order. It also assumes you can read the Styles panel in browser DevTools. The earlier chapters introduced @layer blocks. Here you will learn how to control which layer wins by declaring the order in one place.
What the statement form does
An @layer statement lists layer names and has no rules inside it. It ends with a semicolon.
@layer reset, base, components, utilities;The statement creates four empty layers and fixes their priority. Layers listed later have higher priority for normal declarations. Here utilities beats components, which beats base, which beats reset.
You can then fill the layers with block rules in any order and in any file. The block rules do not change the priority.
@layer reset, base, components, utilities;
@layer utilities {
.text-red { color: red; }
}
@layer base {
a { color: royalblue; }
}
@layer components {
.card a { color: black; }
}
@layer reset {
* { margin: 0; box-sizing: border-box; }
}The blocks above appear in scrambled order. The result is the same as if they were written in the order reset, base, components, utilities. For an element <a class="text-red"> inside .card, the link is red. The utilities layer outranks components, even though .card a has higher specificity than .text-red.
A statement can name a single layer, such as @layer base;. A comma-separated list is the usual form because it shows the whole order at a glance.
How the browser decides the order
The rule is simple: a layer's position is set the first time its name appears. The first appearance can be in a statement or in a block. Whichever comes first in the stylesheet wins the position.
After that, every later mention of the same name refers to the same layer. A later block with that name adds rules to the layer, and it never moves the layer.
Take this example:
@layer components {
.card a { color: black; }
}
@layer reset, base, components, utilities;The block for components appears first. That creates components as the first layer. The statement then adds reset, base, and utilities after it, and it ignores components because that name already exists. The final order is:
componentsresetbaseutilities
Now reset and base outrank components. That is almost certainly not what the author wanted. The statement looks correct, but a block above it already fixed the position.
Repeated names in one statement
A name can appear twice in the same statement. The second mention is ignored, and the first one sets the position.
@layer base, components, base, utilities;The order is base, components, utilities. This is legal but confusing, so avoid it in real code.
Layer order across several stylesheets
The "first appearance" rule works across the whole page. The browser reads stylesheets in document order: <link> and <style> elements, plus anything pulled in by @import. It records each layer name as it first meets it.
Suppose a page has two stylesheets:
<link rel="stylesheet" href="layers.css">
<link rel="stylesheet" href="app.css">If layers.css contains only this line, every later file can use those layers without declaring the order again:
@layer reset, base, components, utilities;app.css can then write:
@layer components {
.btn { padding: 0.5rem 1rem; }
}
@layer utilities {
.p-0 { padding: 0; }
}The order is already set, so the block order in app.css does not matter. This is the main benefit of the statement form. One short file or one first line owns the priority, and the rest of the codebase just fills the layers.
The statement and @import
@import rules must come before other rules in a stylesheet. The exceptions are @charset and @layer statements. Because of this exception, you can declare the order first and then import files into those layers:
@layer vendor, site;
@import url("vendor.css") layer(vendor);
@layer site {
.btn { padding: 0.5rem 1rem; }
}The @layer vendor, site; line runs first, so vendor is lower priority than site. A later chapter covers @import ... layer() in detail. For now, note that the statement is the one @layer form allowed above an @import. A block @layer placed above an @import would make the import invalid.
A diagram of the order
The diagram shows how priority rises from the first layer in the statement to the last, with unlayered styles on top.
Unlayered styles sit above every layer for normal declarations. A later chapter covers them fully. The fact matters here because the order statement only ranks the layers against each other.
Why the statement beats ordering blocks by hand
You can set layer order by arranging blocks in the file. The statement is better for these reasons:
- It is explicit. A reader sees the whole order in one line and does not need to scan a long file.
- It survives refactoring. If someone moves a block or splits a file, the priority does not change.
- It handles build tools. Bundlers and CSS concatenation can reorder files. A statement at the top of the first file keeps the order stable.
- It is easy to change. To make
utilitiesweaker thancomponents, you swap two names on one line.
The cost is that the statement must truly come first. If a build step puts another stylesheet ahead of it, that stylesheet may fix the order by accident.
Common errors
Typos create new layers
Layer names are case-sensitive identifiers. A mismatch does not raise an error. It silently creates a new layer.
@layer reset, base, components, utilities;
@layer Components {
.card a { color: black; }
}Components is not components. The browser creates a fifth layer called Components after utilities, so it outranks every other layer. The rules in it will override your utilities, which is the opposite of the intent. If your component styles seem too strong, check the spelling in DevTools and in your source.
A late statement does nothing
As shown above, a statement placed after blocks cannot reorder layers that already exist. The visible symptom is that a rule you expected to lose wins. The fix is to move the statement to the very top of the first stylesheet.
Forgetting that order is not about specificity
Inside one layer, specificity and source order still decide conflicts as usual. Layer order only decides between rules in different layers. If two rules in components conflict, the higher specificity wins, as it always did.
Mixing statement and nested layers
A statement can also name nested layers with dot notation, for example @layer framework.base, framework.theme;. This creates framework if it does not yet exist, then adds base and theme inside it in that order. Nesting is covered in a later chapter. For now, treat the dotted names as a way to set order inside a parent layer.
Checking the order in DevTools
Current Chrome, Edge, Firefox, and Safari show layer names in the Styles panel next to each rule. A rule from a layer shows a label such as @layer components. Use the labels to confirm that your rules landed in the layers you expected.
When something wins unexpectedly, work through these checks:
- Find the winning rule and read its layer label. No label means the rule is unlayered.
- Find the rule you expected to win and read its label.
- Check where each layer name first appears in the loaded CSS, including imports.
- Look for a misspelled or differently cased name.
Summary of the rules
| Situation | Result |
|---|---|
| Name first appears in a statement | The layer is created at that position |
| Name first appears in a block | The layer is created at that position, and a later statement cannot move it |
| Name appears again later | Rules are added to the existing layer, and the position stays the same |
| Name is spelled differently | A new layer is created at the end |
| Statement placed before `@import` | Allowed, and it fixes the order before imports run |
Exercise
A stylesheet contains the following code. Which layer has the highest priority for normal declarations, and why? @layer theme { p { color: navy; } } @layer reset, theme, utilities; @layer reset { p { color: black; } }
Show answer
The order is theme, reset, utilities, so utilities has the highest priority. The theme block appears first, so theme is created first. The statement then adds reset and utilities after it and ignores the existing name theme. Because reset comes after theme, the black color from reset beats navy from theme for paragraphs, unless a utilities rule overrides both. To get the intended order, move the statement above the theme block.
Exercise
You write @layer base, components, utilities; at the top of your CSS, then add @layer Utilities { .hidden { display: none; } } further down. The .hidden rule seems to override everything. What happened, and how do you fix it?
Show answer
Layer names are case-sensitive, so Utilities is a new layer, not utilities. It was created after the three declared layers, so it has the highest priority among them. Rename it to utilities so the rule joins the declared layer and sits in the intended position.