Cascade Layers: @layer, Layer Order and Taming Specificity
How @layer groups styles into ordered layers that beat specificity: declaring the order, anonymous and nested layers, importing third-party CSS into a layer, unlayered styles, the reversed order for !important, revert-layer and a practical structure.
kvi5hnuCode Sprout team member8 min readOn this page
Specificity was designed to make precise selectors win, and in small stylesheets it does. In large ones, it turns into a contest: a component rule loses to a broad rule with an ID in it, so someone adds a class, then an !important. Cascade layers solve this by letting you decide the priority of whole groups of styles before specificity is considered. A rule in a later layer beats a rule in an earlier layer, however specific the earlier one is.
Defining layers
The @layer at-rule puts rules into a named layer:
@layer base {
a:any-link { color: #8a3b12; text-decoration: underline; } /* 0,1,1 */
}
@layer components {
.button { color: white; text-decoration: none; } /* 0,1,0: wins anyway */
}
The components layer comes later, so <a class="button"> gets white text without an underline, although a:any-link has higher specificity. Within one layer, specificity and source order work as usual. Between layers, layer order decides first: in the cascade, layers come after origin, importance and inline styles, and before specificity (article 17).
@layer base, components;
@layer base {
#main a:any-link { color: #8a3b12; text-decoration: underline; } /* 1,1,1 */
}
@layer components {
.button { color: white; background: #2f7d4f; text-decoration: none;
padding: 0.4rem 0.9rem; border-radius: 6px; font: 600 1rem system-ui, sans-serif; }
}

The selectors never change between panels. Only where the rules live, and the order of the layers, decides which color wins.
Layer order
A layer's priority is set by the first time its name appears. Layers that appear later have higher priority. Adding more rules to a layer later doesn't change its position:
@layer base { ... }
@layer components { ... }
@layer base { ... } /* still the base layer, lower than components */
To make the order explicit, declare all layers in one statement at the top of the first stylesheet, with no block:
@layer reset, base, layout, components, utilities;
Then add rules to them anywhere, in any order, in any file. This statement is the single place that documents the priority of the whole project. It's one of the few rules allowed before @import.
Layer names are identifiers: letters, digits, hyphens and underscores, not starting with a digit.
Unlayered styles win
Styles that aren't in any layer belong to an implicit final layer, so they beat all layered styles for normal declarations:
@layer components { .button { background: #2f7d4f; } }
.button { background: #b3261e; } /* unlayered: wins */
That's convenient while migrating, or for quick experiments. In a project that uses layers, put everything in a layer, so that nothing wins by accident.
Importing CSS into a layer
@import can put a whole stylesheet into a layer, which is the cleanest way to handle third-party CSS:
@layer reset, vendor, base, components;
@import url("normalize.css") layer(reset);
@import url("datepicker.css") layer(vendor);
Now your base and components rules override the date picker's styles without needing more specific selectors. If the imported file defines its own layers, they become nested layers inside vendor, so they can't interfere with your order.
There's no layer attribute on <link> elements yet. To layer a stylesheet loaded from HTML, use a <style> element with @import ... layer(...) in it. Frameworks and build tools can also wrap files in @layer blocks for you.
Anonymous and nested layers
Anonymous layers have no name: @layer { ... } or @import url(x.css) layer;. Each one is a new, separate layer at the position where it appears, and you can't add rules to it later. They're useful mainly for imported files.
Nested layers subdivide a layer:
@layer components {
@layer layout { .card { display: grid; } }
@layer theme { .card { background: #f4efe6; } }
.card { padding: 1rem; } /* in components, outside the nested layers */
}
@layer components.theme { .card { border-radius: 8px; } } /* dot notation adds to it */
Inside components, theme beats layout, and rules directly in components beat both, like unlayered rules at the top level. The whole components layer, with everything nested in it, keeps its single position among the top-level layers.
!important reverses the order
Important declarations in layers follow the same rule as important declarations in origins (article 17): the order is reversed. An important declaration in an earlier layer beats an important declaration in a later one, and important unlayered declarations are the weakest of the important ones:

That's intended. A low layer, such as a reset, can use !important to protect something that must never be overridden, and higher layers can't defeat it even with their own !important. A common example is turning off motion for people who ask for it:
@layer reset {
@media (prefers-reduced-motion: reduce) {
:root { scroll-behavior: auto !important; }
*, ::before, ::after {
animation-delay: 0s !important;
animation-duration: 1ms !important; /* end at once, but still fire animation events */
transition-delay: 0s !important;
transition-duration: 1ms !important;
}
}
}
Every important declaration still beats every normal one. The reversal only decides between important declarations.
revert-layer
The revert-layer keyword (article 18) rolls a property back to the value from earlier layers, as if the current layer didn't set it:
@layer base, components;
@layer base { a:any-link { color: #8a3b12; } }
@layer components {
.button { color: white; }
.prose .button { all: revert-layer; } /* buttons inside articles look like base links again */
}
A practical layer structure
There's no single correct structure, but most projects settle on something close to this, from lowest to highest priority:
| Layer | Contains |
|---|---|
reset | fixes to browser defaults: box-sizing, form control fonts, image sizing, reduced motion |
theme | design tokens: custom properties for colors, fonts and spacing, color-scheme, accent-color |
base | element defaults: body text, headings, links, lists, tables |
layout | page structure: header, main, sidebars, grids, containers |
components | reusable parts: cards, buttons, menus, dialogs, each with its states |
utilities | single-purpose classes that should win: .visually-hidden, .text-center, .stack |
Third-party CSS usually goes into a low layer such as vendor, after reset and before base. Not every project needs every layer, and you can split a large layer with nested layers. Article 117 covers architecture in more depth.
Layers in older browsers
Cascade layers have worked in every browser since early 2022, so support is no longer a concern for most sites. A browser without support ignores every rule inside @layer blocks, which is why there's no gentle fallback. If you must support very old browsers, a build tool can compile layers into plain CSS with adjusted specificity.
Layers in scripts and DevTools
- DevTools show the layer of each rule in the Styles pane, and Chrome's Styles pane has a button that shows the whole layer order.
- In the CSSOM,
@layerblocks areCSSLayerBlockRuleobjects and statements areCSSLayerStatementRuleobjects, with the layer names in.nameand.nameList. - Each shadow tree has its own layer order. Layers in a component don't mix with the page's layers.
What's current
@layer,@import ... layer()andrevert-layerare Baseline since March 2022, and widely available since September 2024.- The
@layerstatement is the recommended first line of a project's CSS. Many frameworks, including recent versions of utility-class frameworks, now ship their CSS in layers. - A
layerattribute for<link>has been proposed but isn't in any browser yet.
Common questions
Do layers replace specificity? No. Specificity still decides between rules in the same layer. Layers decide first, between rules in different layers, so you only have to think about specificity inside a layer.
Why do my unlayered styles beat everything in my layers?
Because unlayered styles form the last, strongest layer for normal declarations. Put them in a layer, or make the layered rule !important only if it really should win.
How do I make a third-party library easy to override?
Import it into a low layer: @import url(lib.css) layer(vendor);, after declaring @layer reset, vendor, base, components;. Then any of your layered or unlayered rules beats it.
Why does an !important in my reset layer beat an !important in my utilities layer?
For important declarations, the layer order is reversed, so earlier layers win. That protects foundational rules, just as user !important styles beat author ones.
Can I change a layer's position later? No. Its position is fixed by its first appearance. That's why the order statement should come first.
Summary
@layergroups rules into layers. Later layers beat earlier ones before specificity is compared.- A layer's position is set by its first appearance. Declare the order with one
@layer a, b, c;statement. - Unlayered styles beat all layers for normal declarations.
@import url(...) layer(name)puts third-party CSS into a layer, and its own layers become nested.- Nested layers subdivide a layer. Rules directly in the parent layer beat its nested layers.
- For
!important, the order reverses: earlier layers win, and unlayered important styles are weakest. revert-layerrolls back to earlier layers.
Practice questions
What color is the link?
@layer base, components;
@layer components { a { color: green; } }
@layer base { #nav a { color: red; } }
Answer
Green. The order statement makes components the later, stronger layer, and layer order is compared before specificity, so #nav a loses although it contains an ID.
A team adds @layer blocks to an old stylesheet, but leaves many old rules outside any layer. Their new component styles don't apply. Why?
Answer
Unlayered rules are in the implicit last layer, which beats every named layer for normal declarations. The old rules win regardless of specificity. Move the old rules into a low layer, such as @layer legacy, declared first.
Write the CSS to load vendor.css so that all your own styles override it, with layers reset, vendor, base and components.
Answer
@layer reset, vendor, base, components;
@import url("vendor.css") layer(vendor);
The order statement can come before @import, and every other rule must come after it.
An element has both the hidden attribute and class="flex", with the layers below. Is it shown?
@layer reset, utilities;
@layer reset {
[hidden] { display: none !important; }
}
@layer utilities {
.flex { display: flex !important; }
}
Answer
No. Both declarations are important, and for important declarations earlier layers win, so the reset layer's display: none beats the utilities' display: flex.
Previous: Shorthand Properties: How They Reset Values and the Order of Their Parts
Next: CSS Nesting: The & Selector, Nested At-Rules and Their Subtleties

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.