Cascade Layers and Scope
How @layer decides which group of styles wins before specificity is compared, and how @scope limits where a rule applies without fighting selector weight.
Cascade Layers and Scope
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `@layer` puts rules into named groups, and a later layer beats an earlier one regardless of selector specificity. Layer order is declared once, e.g. `@layer reset, base, components, utilities;`.
- Styles outside any layer beat styles inside layers (for normal declarations). This lets your own unlayered CSS override a framework you import into a layer.
- `!important` reverses the layer order: for important declarations, an earlier layer wins over a later one.
- `@scope (.card) to (.slot)` applies rules to elements inside `.card` but not inside `.slot`, and when two scopes both match, the closer scoping root wins.
- `@scope` does not stop inheritance and does not make specificity irrelevant: specificity is compared before scope proximity.
The specificity arms race
Specificity works until you add code you do not control. A framework ships .navbar .nav-item a:hover at (0,3,1). Your override .my-link at (0,1,0) loses, so you add nav and an ID, then !important, and soon every new rule has to be heavier than the last. The problem is that selector weight was being used for two jobs: describing which elements to match, and ranking whole groups of styles against each other.
Cascade layers split the second job out. You say, once, which group of styles outranks which, and then selector weight only breaks ties inside a group.
Declaring and filling layers
@layer reset, base, components, utilities;
@layer reset {
* { box-sizing: border-box; margin: 0; }
}
@layer components {
.btn { background: royalblue; color: white; }
}
@layer utilities {
.bg-red { background: crimson; }
}
The first line fixes the order. Layers listed later are stronger. After that, @layer components { ... } blocks can appear in any file, in any order, and they all append to the layer already named. Declaring the order up front is what keeps the ranking in one readable place.
Run this and the weak selector wins, which is the whole point:
@layer base, theme;
@layer base { #a { color: red; } }
@layer theme { p { color: blue; } }
/* <p id="a"> is blue */
I ran this in Chromium and it came out blue. A single-type selector in a later layer beat an ID in an earlier one.
Unlayered styles are the strongest layer
Anything not inside @layer behaves as if it sat in a final layer above all the named ones. So this works as a pattern for third-party CSS:
@import url("framework.css") layer(vendor);
/* your CSS, unlayered: beats vendor no matter its selectors */
.btn { background: tomato; }
I verified an unlayered p { color: blue } beating a layered #a { color: red }. The cost is that a layer cannot override unlayered CSS, so introducing layers to an existing codebase means deciding where the existing styles go.
!important runs backwards
For normal declarations, later layers beat earlier ones. For !important declarations the order flips: the earliest layer wins, and unlayered important rules sit at the bottom. I tested two layers with p { color: ... !important } and the earlier layer won, even against an unlayered important rule. The reasoning is that an early layer such as a reset or a base is where the “must not change” rules live.
Layers can also nest (@layer framework.buttons), and revert-layer rolls a property back to the previous layer’s value.
Scope: limiting where a rule reaches
Layers rank groups. @scope limits which elements a block of rules can reach, without making the selectors longer.
@scope (.card) {
a { color: royalblue; } /* only links inside a .card */
:scope { padding: 1rem; } /* the .card itself */
}
Add to (...) for a lower boundary, a “donut” where the middle is in scope and an inner area is excluded:
@scope (.card) to (.slot) {
a { color: crimson; }
}
Links in the card but outside any .slot are crimson; links inside a .slot are left alone. I checked both cases. That is the situation a component library has: styles for a component that must not leak into whatever content a user drops into its slot.
Proximity, and its limit
If scopes nest, the nearest root wins. With @scope (.light) and @scope (.dark) both styling p, a paragraph in .dark inside .light follows .dark. I tested both nesting directions and each paragraph took the closer ancestor’s rule.
Proximity is a late tiebreaker. It comes after specificity in the cascade ladder, so a more specific selector in a farther scope still wins. And @scope does not isolate inheritance: a property such as color set on an outside element still inherits into a scoped region.
Support and caution
@layer has been in all major browsers since 2022. @scope shipped in Chrome 118 and reached Firefox in 146 and Safari in 26.4 according to MDN’s compatibility data, so it is only now across the three engines; check your audience before depending on it. Layers also deserve restraint: adding many of them recreates the ordering puzzle you were escaping, only at a different level.
What the video covers
The embed is a clip of Kevin Powell’s 43-minute “These CSS features give us more control on the cascade and specificity”, starting at 24:00 and running to the end. By the video’s chapter list that stretch holds cascade layers (introduction, basics, debugging, declaring layers before using them, the effect of !important, mixing layers with unlayered styles) and then scope (introduction, basics, using it with style elements inside an element). I could not read a transcript, so the examples above are the lesson’s own. The earlier part of the video, on source order, specificity and @supports, is worth watching in full. The runner-up is a dedicated 26-minute @scope talk with chapters on proximity and the difference between & and :scope.
Where this fits
Layers and scope finish the cascade ladder from specificity and the cascade. Layers carry real weight in CSS architecture and BEM, where ordering resets, components and utilities is a common design choice. Next, the box model starts the move from “which rule wins” to “how big is the thing”.
Check yourself
Given `@layer base, theme;`, a rule `#a { color: red }` in `base` and `p { color: blue }` in `theme`, both matching `<p id="a">`. What color is it?
Blue. Layers are compared before specificity, and `theme` is declared after `base`, so it wins even though `p` is much weaker than `#a`. I ran this in Chromium.
You import a framework into `@layer vendor` and write your own `.btn { }` rule with no layer. Whose rule wins when both set `background`?
Yours. Unlayered normal declarations beat layered ones, so the framework's selector weight is irrelevant. That is the main reason to wrap third-party CSS in a layer.
Layer `a` is declared before layer `b`. Both contain `p { color: ... !important }`. Which wins?
Layer `a`. For `!important` declarations the order is inverted, so the earlier layer outranks the later one. That is deliberate: a base layer's important rules, like a reset's, are meant to resist later overrides.
What does `@scope (.card) to (.slot) { a { color: red } }` do to a link inside `.slot` inside a `.card`?
Nothing, because `.slot` is the scope's lower boundary and elements inside it are excluded. A link directly inside `.card` outside any `.slot` is styled red. I verified this in Chromium.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.