Cascade Layers (@layer): Ending Specificity Wars Without !important
How CSS cascade layers work: layer order, why unlayered styles win, the !important reversal, layering third-party CSS, revert-layer, and a migration plan.
!important reverses layer order.
A team adopts a UI library. Its buttons are styled with selectors like .btn-group > .btn:not(:last-child):not(.dropdown-toggle). To change a button’s border radius, someone writes .app .page .btn-group .btn. Six months later, a new developer cannot override that rule with a utility class, adds !important, and the next override needs !important with more specificity. Every fix makes the next fix harder.
CSS cascade layers, created with the @layer at-rule, group declarations into named layers whose precedence you set explicitly. In the cascade sort order, layers are compared before specificity. Within one origin, a declaration in a later layer wins over one in an earlier layer regardless of how specific either selector is. Cascade layers have been supported in every major browser since early 2022.
My position: every stylesheet with more than one author or one dependency should start with a layer order statement. It is one line, it costs nothing at runtime, and it turns “who wins?” from an investigation into a lookup.
The core idea in ten lines
@layer base, components;
@layer base {
#main nav a { color: blue; } /* specificity (1, 0, 2) */
}
@layer components {
.nav-link { color: red; } /* specificity (0, 1, 0) */
}
/* Links are red. components is a later layer, so it wins before specificity is checked. */
Specificity still exists, but only as a tie-breaker between rules in the same layer. That is the whole point: you keep specificity small and local, and you express the big decisions (“components override base styles”, “utilities override components”) as layer order.
Declaring and using layers
There are four ways to put styles into a layer.
/* 1. Order statement: declares names and their order, no styles */
@layer reset, vendor, base, components, utilities;
/* 2. Block: adds styles to a named layer (can appear many times) */
@layer components {
.card { padding: var(--space-3); }
}
/* 3. Import a whole file into a layer */
@import url("vendor/datepicker.css") layer(vendor);
/* 4. Anonymous layer: unnamed, cannot be added to later */
@layer {
.legacy-widget { margin: 0; }
}
Layer order is set by the first time each name appears. That is why the order statement belongs at the very top of your entry stylesheet: it fixes the order no matter which file happens to load first later. Without it, the order depends on which @layer block the browser sees first, and code splitting can change that.
@import rules must come before all other rules except @charset and @layer order statements, so this is valid:
@layer reset, vendor, base, components, utilities;
@import url("modern-normalize.css") layer(reset);
@import url("datepicker.css") layer(vendor);
A <link> element cannot assign a layer by itself. To layer a stylesheet loaded with <link>, load it through an @import ... layer() instead, or have your bundler wrap its contents in an @layer block.
Nested layers
Layers can contain layers. Refer to them with dot notation.
@layer components {
@layer primitives, patterns;
}
@layer components.primitives {
.button { border-radius: var(--radius-control); }
}
@layer components.patterns {
.search-bar .button { border-radius: 0; } /* patterns beat primitives */
}
Nesting is useful when one team owns a layer and wants its own internal ordering without touching the global order statement.
The two rules that surprise everyone
Rule 1: unlayered styles beat all layers
Any style not inside a layer belongs to an implicit final layer that outranks every named layer. That design lets you adopt layers gradually: existing CSS keeps winning until you move it. It also means a single forgotten file of unlayered CSS silently overrides your whole system.
@layer utilities {
.hidden { display: none; }
}
/* Some legacy file, not in any layer */
.sidebar div { display: block; }
/* <div class="hidden"> inside .sidebar is VISIBLE: unlayered beats layered */
When a layered rule mysteriously loses, check DevTools for an unlayered rule first. Chromium and Firefox DevTools label each rule with its layer, so unlayered rules are easy to spot.
Rule 2: !important reverses layer order
For normal declarations, later layers win. For !important declarations, the order flips: an important declaration in an earlier layer beats an important declaration in a later layer, and important declarations in any layer beat important unlayered ones.
| Precedence (highest first) | Declarations |
|---|---|
| 1 | !important in the first layer (reset) |
| 2 | !important in later layers, in reverse order |
| 3 | !important in unlayered styles |
| 4 | Normal unlayered styles |
| 5 | Normal styles in the last layer (utilities) |
| 6 | Normal styles in earlier layers, down to the first |
This mirrors how origins work, where user !important beats author !important. It gives low layers a way to protect something essential. A reset layer can mark [hidden] { display: none !important; }, and no component can accidentally show hidden elements. Used that way, !important becomes a deliberate guarantee rather than a weapon.
A layer stack that works
@layer reset, vendor, tokens, base, layouts, components, utilities;
| Layer | Contains | Typical selectors |
|---|---|---|
reset |
Normalize and box-sizing | *, element selectors |
vendor |
Third-party widget CSS | Whatever the library ships |
tokens |
Custom property definitions | :root, [data-theme] |
base |
Element defaults: typography, links, forms | h1, a, :where(.prose) p |
layouts |
Page and region layout | .page, .stack, .content-grid |
components |
Reusable UI | .card, .button |
utilities |
Single-purpose overrides | .visually-hidden, .text-center |
Putting vendor low is the key decision. Your base styles and components now beat the library’s rules with ordinary single-class selectors. Utilities sit at the top, so they override components without !important, which is exactly how Tailwind v4 structures its own output: it emits theme, base, components and utilities layers. If you mix Tailwind with your own CSS, declare your layers around those names so the two systems agree on order. The trade-offs of that mix are covered in vanilla CSS vs Tailwind in 2026.
revert-layer: opting out of a layer’s styles
The revert-layer keyword rolls a property back to the value it would have had from earlier layers, as if the current layer had not set it.
@layer base {
a { color: var(--color-link); text-decoration: underline; }
}
@layer components {
.card a { color: inherit; text-decoration: none; }
.card a.card__more { color: revert-layer; } /* back to the base link color */
}
It is cleaner than restating the base value, because the component no longer needs to know what the base layer chose. If the base link color changes, the reverted link follows.
Layers and @scope
Layers decide priority. They do not limit where a selector reaches. For that, CSS now has @scope, which became available in all major browsers in December 2025 with Firefox 146. @scope (.card) to (.card__content) confines rules to a card’s own subtree and stops them at nested content. Use both together: layers for “which group of styles wins” and @scope for “which elements a component’s rules may touch”. Neither needs naming conventions to work.
Migrating an existing codebase
The “unlayered wins” rule makes migration order matter. If you start writing new code in layers while the old code stays unlayered, the old code beats all of it. Migrate in this order instead:
- Add the order statement with an extra
legacylayer:@layer reset, vendor, base, components, legacy, utilities; - Wrap all existing CSS in
legacy. Usually a single@import url("legacy.css") layer(legacy);. Nothing changes visually, because relative order inside the layer is preserved. - Wrap third-party CSS in
vendor. Now verify: things that legacy CSS was fighting may already look right. - Move code out of
legacyone component at a time, intobaseorcomponents, deleting the specificity hacks as you go. - Delete
!importantwherever layer order now does the job.
The legacy layer sits above components on purpose, so old rules keep winning during the migration and nothing breaks halfway through. When it is empty, remove it from the order statement.
Frequently Asked Questions
What are CSS cascade layers?
Cascade layers group CSS declarations into named layers with an explicit order, created with the @layer at-rule. The cascade compares layer order before specificity, so a rule in a later layer beats a rule in an earlier layer regardless of how specific their selectors are.
Why do unlayered styles override my layers?
Styles outside any layer are treated as an implicit final layer that outranks all named layers for normal declarations. This allows gradual adoption, but it means any unlayered CSS beats your layered system. Move it into a layer, or put legacy CSS in a layer of its own.
How does !important work with cascade layers?
Important declarations reverse layer order. An !important declaration in an earlier layer beats one in a later layer, and important declarations in any layer beat important unlayered ones. This lets foundational layers protect rules such as hidden elements staying hidden.
How do I put Bootstrap or other third-party CSS into a layer?
Import it with a layer assignment, for example @import url(“bootstrap.css”) layer(vendor), after an order statement that places vendor below your own layers. Your components can then override the library with simple class selectors.
Decide priority once, not per selector
Specificity wars happen because priority is decided one selector at a time. Cascade layers move that decision to one line at the top of the stylesheet, where the whole team can see it. Put vendors low, utilities high, everything you own in between, and keep every selector boring. Nesting, which I cover in my guide to native CSS nesting, has its own specificity quirks that layers help keep in check.
Rule of thumb: if you are about to type !important to win, you need a layer, not a heavier selector.
Originally posted on 15 January 2026 and post last modified on 15 January 2026