CSS @layer — Cascade Layers
Before @layer, the only tools to control which CSS rule "wins" were specificity and source order — leading to escalating battles of extra classes, ID selectors, and !important just to override a framework or reset. Cascade layers add an explicit, named priority system on top of the existing cascade: you decide the layer order once, and from then on, layer order beats specificity entirely. A reset can use * and still lose to a single class in a later layer.
The Specificity Wars Problem
Without cascade layers, overriding a framework or reset style often means fighting the cascade rather than working with it:
button { padding: 0; border: none; }
You want padding back on your button.
A plain .btn { padding: 10px; }
might lose if the reset uses a more
specific selector, or simply comes
LATER in source order.
Common "fix": add more specificity
.btn.btn { padding: 10px !important; }
— fragile, and now YOUR styles are
hard for anyone else to override too.
@layer reset {
button { padding: 0; }
}
@layer components {
.btn { padding: 10px; }
}
.btn ALWAYS wins — components is
declared after reset. No specificity
tricks, no !important, no fragility.
@layer Syntax
Declare layer names and their order up front, then write rules into each named layer anywhere in the stylesheet — even across separate files. The declaration order is what sets the priority, not where the rules physically appear:
declares layer order upfront
lowest priority — applied first
highest priority — wins last
/* ── Declare order upfront (recommended — do this once, at the top) ── */
@layer reset, base, components, utilities, overrides;
/* ── Add rules to a layer — can appear ANYWHERE, even in other files ── */
@layer reset {
* { margin: 0; box-sizing: border-box; }
button { border: none; background: none; }
}
@layer components {
.btn { padding: 10px 20px; border-radius: 8px; }
}
/* ── .btn wins over button, even though "button" + "*" combined
could theoretically have equal or higher specificity — layer
order decides BEFORE specificity is even considered ── */
/* ── Adding to an EXISTING layer later — same name, more rules ── */
@layer components {
.card { border-radius: 12px; } /* appended to the same layer, not a new one */
}
/* ── @import directly into a layer ── */
@import url(reset.css) layer(reset);
@import url(framework.css) layer(components);
Layer Order Beats Specificity
This is the core rule to internalise: a later layer always wins over an earlier layer, regardless of selector specificity. An ID selector in an earlier layer loses to a single class in a later layer. Specificity only matters for resolving conflicts within the same layer:
↑ The orange button represents what actually renders: the LAST declared layer wins, full stop.
@layer reset, components, overrides;
@layer reset {
/* Universal selector — LOWEST possible specificity (0,0,0) */
* { color: grey; }
}
@layer components {
/* ID selector — VERY HIGH specificity (1,0,0) */
#button { color: blue; }
}
@layer overrides {
/* Class selector — LOW specificity (0,1,0) */
.btn { color: orange; }
}
/* Result on
/* ── Specificity DOES still matter WITHIN one layer ── */
@layer components {
.btn { color: blue; }
#button { color: red; } /* wins — same layer, higher specificity */
}
Unlayered Styles & Nesting
Any CSS rule written outside an @layer block is "unlayered." Unlayered styles always beat every layered style — they form an implicit highest-priority layer above anything you declare explicitly. This is important for inline styles, quick fixes, or third-party widget overrides that must always win:
@layer reset, components, overrides;
@layer overrides {
.btn { color: orange; } /* highest DECLARED layer */
}
/* Unlayered rule — written OUTSIDE any @layer block */
.btn { color: green; } /* GREEN wins — unlayered beats ALL layers */
/* Practical use: a "patch" stylesheet that must always win
over the entire layered system, without being added as a layer */
/* ── Nested layers — sub-priority inside a layer ── */
@layer components {
@layer buttons, cards; /* nested layer order */
@layer buttons {
.btn { padding: 10px; }
}
@layer cards {
.card { padding: 16px; } /* wins within components, since cards declared after buttons */
}
}
/* ── Anonymous layer — no name, still gets its own priority slot ── */
@layer {
.quick-fix { color: red; } /* can't be re-opened or referenced by name later */
}
/* ── Referencing nested layer by dotted path from outside ── */
@layer components.buttons {
.btn-lg { padding: 14px 24px; }
}
Real-World Layer Architecture
A common, battle-tested layer order for production projects — each layer has one clear job, and conflicts resolve predictably by design:
/* ── Declare the entire architecture in ONE place, top of main CSS file ── */
@layer reset, base, components, utilities, overrides;
/* ── Import third-party CSS directly into a named layer ── */
@import url("normalize.css") layer(reset);
@import url("bootstrap.css") layer(components); /* tame a framework's specificity */
@layer base {
body { font-family: system-ui; line-height: 1.6; }
h1, h2, h3 { font-weight: 700; }
}
@layer components {
.btn { padding: 10px 20px; border-radius: 8px; }
.card { border-radius: 12px; box-shadow: 0 4px 12px #0001; }
}
@layer utilities {
/* Even though .mt-4 has the SAME specificity as .card,
utilities is declared AFTER components, so it always wins */
.mt-4 { margin-top: 1rem; }
.hidden{ display: none; }
}
@layer overrides {
/* Reserved for rare, must-always-win patches */
.force-visible { display: block !important; }
}
/* Why this works: you NEVER need !important in components or
utilities anymore — the layer system already guarantees the
intended priority order, regardless of how specific any
individual selector happens to be. */
Cascade Layers Reference
| Concept | Syntax | Behaviour |
|---|---|---|
| Declare order | @layer a, b, c; | Sets priority upfront — c beats b beats a, regardless of specificity |
| Add rules | @layer a { selector {} } | Appends to layer "a" — can appear anywhere, even multiple times |
| Anonymous layer | @layer { selector {} } | Gets its own priority slot but can't be referenced by name again |
| Nested layer | @layer a { @layer b {} } | "b" is scoped inside "a" — referenced as a.b from outside |
| Import into layer | @import url(x.css) layer(name); | Loads external CSS directly into a named layer |
| Unlayered styles | selector {} (no @layer) | Always wins over ANY layered style, regardless of order |
| Within-layer conflicts | — | Resolved by normal specificity + source order rules |
Try It Yourself
A complete layered stylesheet — reset, base, components, utilities and overrides, all competing for the same elements:
Quick Check — Chapter 54
.btn.btn.btn), an ID hack, or !important — all of which are fragile and create a never-ending escalation, since the next override needs to be even more specific.Cascade layers add an explicit priority dimension that sits ABOVE specificity in the resolution order. You declare "reset, then base, then components, then utilities, then overrides" once — and from that point on, anything in a later layer always wins over anything in an earlier layer, no matter how specific the earlier selector is. This eliminates the arms race entirely: you organize by intent (reset vs component vs utility) instead of by selector cleverness.
@layer order, then #button in components wins over * in reset — which seems expected since IDs are far more specific than universal selectors.But the surprising part is what happens if you reverse it: if a THIRD layer declared even later — say "overrides" — contains
.btn { color: orange }, then orange wins over the ID selector in components, even though a class selector has dramatically lower specificity than an ID selector.This is the core mental shift cascade layers require: specificity comparisons only happen within the same layer. Across different layers, the comparison never even reaches specificity — the layer declared later always wins outright, full stop, regardless of what selector type was used inside it.
@layer block — plain, ordinary CSS exactly as you've always written it. They form an implicit "highest priority layer" that sits above every named layer you've declared, regardless of the order you specified.This design exists so introducing cascade layers into an existing codebase doesn't silently change the behaviour of all your existing CSS. Any styles you haven't moved into a layer continue to behave with their original (very high, since they're "last" in cascade terms) priority. It also provides an escape hatch: a small, deliberately unlayered "patch" stylesheet can always override the entire layered system without needing to be the highest-declared layer or use
!important.In practice, this means migrating a large codebase to cascade layers can be done incrementally — move one category of styles (say, a reset) into a layer at a time, while everything else remains unlayered and still wins by default.
@layer components { } multiple times throughout a stylesheet — or across multiple imported files — doesn't create separate layers. CSS recognizes the layer by name and appends each new block of rules to that same named layer's running collection.This is what makes layers practical for real projects: a CSS framework can ship rules already wrapped in
@layer components, your own project file can also add to @layer components later, and both contribute to the same priority slot in the cascade — they don't compete with each other based on which file loaded first.Within a single named layer, normal specificity and source order rules still apply to resolve conflicts between rules contributed from different places — the layer system only changes how DIFFERENT layers relate to each other, not how rules within the same layer are resolved.
@import url("bootstrap.css") layer(components); loads the entire framework's CSS directly into a named layer — without modifying the framework's source files at all.This solves a long-standing pain point: large frameworks often use moderately specific selectors (compound classes, sometimes IDs in older code) that are difficult to override without resorting to
!important or selector escalation in your own CSS. By importing the entire framework into an early layer (like "components"), and writing your own customisations in a later layer (like "overrides" or "utilities"), your simpler selectors automatically win — regardless of how specific Bootstrap's internal selectors are.This effectively "demotes" an entire third-party library's specificity to "whatever layer you put it in," letting you control its priority as a single unit instead of fighting individual selectors one at a time.
- @layer adds an explicit priority dimension above specificity, ending "specificity wars" between resets, frameworks, components, and overrides.
- Declare order upfront:
@layer reset, base, components, utilities, overrides;— later names have higher priority. - Layer order beats specificity completely: a class selector in a later layer beats an ID selector in an earlier layer. Specificity only resolves conflicts WITHIN the same layer.
- Rules can be added to a layer from multiple locations: writing
@layer components { }twice appends to the same layer, doesn't create duplicates. - Unlayered styles (plain CSS outside any @layer block) always beat ALL layered styles — useful as an escape hatch and for incremental migration.
- Nested layers:
@layer components { @layer buttons, cards; }— referenced from outside ascomponents.buttons. - Anonymous layers (
@layer { }) get a priority slot but can't be reopened by name. - @import into a layer:
@import url(x.css) layer(name);— tame an entire third-party framework's specificity by demoting it to one named layer. - Recommended architecture: reset → base → components → utilities → overrides, with !important reserved only for the final overrides layer if ever needed at all.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.