This note has an interactive demo. Try it in the original
Put a light card inside a dark card, and a plain .light h3 selector loses to .dark h3 just because .dark comes later in the file. With @scope, the closest theme wins. MDN has listed it as Baseline since March 2026, so you can use it in Chrome, Edge, Firefox and Safari.
The demo above shows this with three nested cards and an embedded widget. Change each card's theme, then turn @scope off. The badges read the computed color of each heading, so they show what the browser actually did.
The problem it solves
Nested themes have always been awkward. Say you have these rules:
.t-light h3 { color: #0B0F1A; } .t-dark h3 { color: #F8FAFC; } Both selectors have the same specificity (0-1-1). An h3 inside a light card that sits inside a dark card matches both, so the last rule in the file wins. The heading turns white on a light background. In the demo, switching @scope off drops the score to 1/4 for exactly this reason.
The usual fixes are more specific selectors, BEM-style class names on every element, or reordering the stylesheet and hoping. @scope fixes it in the cascade itself.
How it works
You give @scope a scope root, and the rules inside only match elements in that subtree:
@scope (.t-light) { :scope { background: #F1F5F9; color: #334155; } h3 { color: #0B0F1A; } .chip { background: #0B0F1A; color: #F8FAFC; } } @scope (.t-dark) { :scope { background: #141B2D; color: #94A3B8; } h3 { color: #F8FAFC; } .chip { background: rgba(0,212,170,.16); color: #00D4AA; } } There are three things to notice:
-
:scopematches the scope root itself, which here is the card. That's where the background goes. - Bare selectors like
h3behave as if:where(:scope)were prepended, so they only match inside the root and add no specificity.h3inside@scopestill weighs 0-0-1. - Scoping proximity is the new cascade step. When two scoped rules conflict, the one whose root is the fewest hops up the DOM tree wins, even if it comes earlier in the file. That's why the demo scores 4/4 in
@scopemode while.t-darkis still the last block.
Donut scope: stop at a boundary
You can also set a lower limit with to:
@scope (.t-light) to (.slot) { h3 { color: #0B0F1A; } } The root is inclusive and the limit is exclusive, so .slot and everything inside it are left alone. MDN calls this a "donut scope". It's useful when a component has a hole for content it doesn't own, such as an embedded widget or user-generated markup. Turn on the donut in the demo and the widget goes back to neutral, while the three cards keep their themes.
Scope to the parent with an inline <style>
If you leave out the prelude, a <style> element scopes its rules to its parent element:
<section class="article-body"> <style> @scope { img { border: 5px solid black; } } </style> <img src="photo.jpg" alt=""> </section> That only affects images in this section, and you don't need a class name.
The gotcha I hit while building the demo
Proximity comes after specificity. MDN puts it plainly: scoping proximity "overrules source order but is itself overridden by other, higher-priority criteria such as importance, layers, and specificity."
My first version of the demo had a neutral base style written as .card h3 { color: #94A3B8; }. That has specificity 0-1-1. The scoped h3 has 0-0-1, so the base rule beat every theme and every heading came out gray. The fix was to lower the base rule with :where():
:where(.card) h3 { color: #94A3B8; } /* 0-0-1: scoped rules now win */ Unscoped rules count as infinitely far for proximity, so with equal specificity, a scoped rule beats the base style.
Other things to know
- It isn't style isolation.
@scopelimits which elements selectors can match. It doesn't block inheritance.colororfont-familyset on a root still flows past the donut's lower limit. If you need a hard boundary, use the shadow DOM. - You can't escape the subtree. A selector like
:scope + pis invalid inside@scope. -
:scopeadds weight and&doesn't. An explicit:scope imghas specificity 0-1-1. MDN warns that the specificity of&inside@scopehas differed between engines and versions, so I'd avoid relying on it. - Support. Chrome shipped
@scopein version 118. Firefox turned it on by default in version 146 (December 9, 2025). MDN's compatibility data lists Safari 26.4, and the Safari 26.4 release notes include a fix for@scoperules not applying to<input>and<textarea>. For older browsers, the demo checkstypeof CSSScopeRule !== 'undefined'and shows a notice. In real CSS, a reasonable plan B is to write your base styles so the page still works when the scoped rules are ignored.
What to keep an eye on
- Design systems. Nested themes and light/dark islands are the obvious first use. I expect component libraries to start shipping
@scopeblocks instead of long class prefixes. -
&specificity. It's worth checking again whether engines now agree on how&behaves inside@scope. - Older Safari and Firefox. "Newly available" means the latest versions. If your analytics show many older iOS devices, keep the fallback.
Sources
- @scope — MDN
- Limit the reach of your selectors with the CSS @scope at-rule — Chrome for Developers
- Firefox 146 release notes for developers — MDN
- WebKit Features for Safari 26.4 — WebKit blog
- CSS Cascading and Inheritance Level 6: scoped styles — CSSWG Editor’s Draft
Originally published at richi.codes.