@scope: Limiting Where Styles Apply, Scope Limits and Proximity
How @scope limits rules to a part of the document: the scope root and :scope, scope limits for donut scoping, implicit scopes in a style element, why the root adds no specificity, scope proximity in the cascade, and how @scope compares with BEM, nesting and shadow DOM.
kvi5hnuCode Sprout team member8 min readOn this page
Class naming conventions such as BEM keep component styles apart by making every class unique: .media__image, .media__body. That works, but only through discipline. @scope lets the browser enforce the boundaries instead: rules inside an @scope block apply only within a chosen part of the document, and can stop at an inner boundary where another component begins.
The scope root
@scope (selector) { ... } makes every element matching the selector a scope root. Rules inside the block only match the root's descendants:
@scope (.book-card) {
img { width: 100%; aspect-ratio: 2 / 3; object-fit: cover; }
h3 { font-family: Georgia, serif; margin: 0.5rem 0 0; }
.price { font-weight: 700; color: #2f7d4f; }
}
The .price rule styles prices inside book cards only. Another component can use a .price class with completely different styles, without any naming scheme.
Inside the block:
:scopematches the scope root itself::scope { padding: 1rem; }styles the card.- Plain selectors match descendants of the root, as if written
:scope img. A selector repeating the root, such as.book-cardinside@scope (.book-card), matches a nested card, not the root. - Relative selectors start from the root:
> h3is a child of the card,+ .notethe element after it. &stands for the scope root, like:scope.
Scope limits: "donut" scoping
A scope can also have a lower boundary, written with to:
@scope (.book-card) to (.card-slot) {
p { color: #555; }
}
Rules match elements inside .book-card until they reach a .card-slot: the limit element and everything inside it are excluded. The part of the tree in scope has a hole in it, which is why this is called donut scoping.

That's what components that contain other content need. A card's rules shouldn't reach into whatever is placed in its content slot, which may be another component entirely. The limit can be relative to the root: to (:scope > .content) stops only at a .content that is a direct child of the root. A selector list sets several limits: to (.slot, .nested-widget).
@scope (.promo) to (.promo-content) {
:scope { background: #fff8e6; border: 2px solid #f4c95d; border-radius: 10px; padding: 0.75rem 1rem; font-family: system-ui, sans-serif; }
h3 { color: #8a3b12; margin: 0 0 0.5rem; }
p { font-weight: 700; }
}
.promo-content { border-top: 1px dashed #c9b37e; padding-top: 0.5rem; }
The limit stops selectors, not inheritance. Inherited properties set on the scope root, such as the font family here, still flow into the excluded content. Set them on elements inside the scope if they shouldn't reach the slot.
Implicit scope in a <style> element
An @scope rule without a selector inside a <style> element scopes its rules to the parent of that <style> element:
<div class="newsletter-signup">
<style>
@scope {
:scope { display: grid; gap: 0.5rem; }
input { font: inherit; }
}
</style>
<input type="email" placeholder="you@example.com">
</div>
That's convenient for self-contained snippets in content management systems and server-rendered components, where styles can travel with their markup.
Specificity inside @scope
The scope root's selector adds no specificity to the rules inside. img in @scope (.book-card) has the specificity of img, 0,0,1, while .book-card img would be 0,1,1. :scope counts as a pseudo-class (0,1,0), and & takes the specificity of the root selector, as in nesting.
That keeps scoped components easy to override. It also means a scoped rule can lose to a more specific unscoped one: .sidebar img (0,1,1) beats img inside a card's scope. If a component rule must win, give it a class, or use :scope in the selector.
Scope proximity
Scopes add a new step to the cascade, after specificity and before source order (article 17): scope proximity. When two scoped declarations have the same specificity, the one whose scope root is closer to the element, in generations up the tree, wins, whatever their order in the stylesheet:
@scope (.light) { a { color: #1d4ed8; } }
@scope (.dark) { a { color: #f4c95d; } }
<section class="dark">
<div class="light"><a href="#">Blue: .light is closer</a></div>
</section>
Without scopes, .dark a and .light a have equal specificity, and the later rule would always win, so a light panel inside a dark one would get dark-panel links. With scopes, the nearest theme wins, which matches what the markup says.

The first two panels have the same rules in the same order: only the @scope wrappers changed the result. The last panel shows proximity is only a tie-breaker.
Proximity only decides between declarations that are otherwise tied: higher specificity still wins first. Unscoped declarations count as infinitely far away, so when the specificity is equal, a scoped declaration beats an unscoped one even if the unscoped one comes later.
Nesting and @scope
@scope can be nested inside a style rule, and other rules can be nested inside @scope:
.book-card {
@scope (.blurb) to (.spoiler) {
p { line-height: 1.6; }
}
}
To scope to the parent rule's elements, write & as the root: .toolbar { @scope (&) { button { ... } } }. Leaving the prelude out doesn't do that, even when nested: a prelude-less @scope always scopes to the parent of the <style> element, so in Chrome .toolbar { @scope { button { width: 66px } } } also sized a button outside the toolbar.
@scope compared with other approaches
| Approach | Stops styles leaking out | Stops page styles coming in | Needs naming discipline |
|---|---|---|---|
| BEM or similar naming | yes, by convention | no | yes |
| Nesting | no, it only writes selectors | no | yes |
@scope | yes, enforced by the browser, with limits | no | no |
| Shadow DOM | yes | yes | no, but needs web components |
| CSS Modules (build tool) | yes, by generated class names | no | no |
@scope is the lightest way to give components real boundaries in ordinary HTML. It doesn't isolate a component from page styles, though: global rules such as p { } still apply inside the scope. For full isolation, shadow DOM is still the tool (article 14).
What's current
@scopebecame Baseline newly available in March 2026, when Safari 26.4 fixed the last bugs. Chrome has had it since version 118 (2023), Safari since 17.4 (2024), and Firefox since version 146 (December 2025).- Safari versions 26.0 to 26.3 had a bug that skipped scoped rules for
<input>and<textarea>. Test forms if you support those versions. - For older browsers, rules inside
@scopeare ignored entirely, so put essential layout outside it or provide a fallback.
Common questions
What's the difference between @scope (.card) { img { } } and .card img?
Three things. The scoped rule has lower specificity (that of img), it can have a scope limit, and it takes part in proximity, so the nearest scope wins between nested components.
Why doesn't .card { } inside @scope (.card) style the card?
Selectors inside a scope match descendants of the root. Use :scope or & for the root itself.
Does a scope limit stop inheritance? No. Selectors stop at the limit, but inherited properties set above it still pass into the excluded elements.
Should I use @scope instead of BEM?
It can replace much of what BEM's long names are for. Many teams combine them: short class names inside @scope blocks, with the component name only on the root.
Can I use @scope in a <link>ed stylesheet without a selector?
It's valid, but rarely what you want. A prelude-less @scope scopes to the parent of the element that loaded the stylesheet. For a <link> in the <head>, that's the <head> itself, so the rules match nothing on the page; tested in Chrome, @scope { p { color: red } } loaded that way left every paragraph black. In a stylesheet file, give a selector.
Summary
@scope (root) { ... }limits rules to descendants of each scope root.:scopeand&match the root itself.to (limit)excludes the limit elements and their contents, which gives donut scoping for components with slots.- A prelude-less
@scopein a<style>element scopes to that element's parent. - The root selector adds no specificity, which keeps scoped rules easy to override.
- Scope proximity, after specificity and before source order, lets the nearest scope root win.
- Limits stop selectors, not inheritance. Page styles still reach into scopes.
@scopeis Baseline since March 2026.
Practice questions
Write a scoped rule that gives links in .site-footer a muted color, without affecting links inside a .newsletter form embedded in the footer.
Answer
@scope (.site-footer) to (.newsletter) {
a { color: #666; }
}
What's the specificity of .price in this scoped rule, and why does it matter?
@scope (#featured .book-card) {
.price { color: #8a3b12; }
}
Answer
0,1,0, just the class. The root selector, with its ID, adds nothing. That keeps the rule easy to override, but it can also lose to an unscoped rule such as .sidebar .price (0,2,0).
Two themes are scoped as below. A paragraph is inside .theme-light, which is inside .theme-dark. What color is it?
@scope (.theme-light) { p { color: black; } }
@scope (.theme-dark) { p { color: white; } }
Answer
Black. Both declarations have the same specificity, so scope proximity decides before source order: .theme-light is the closer scope root, so its rule wins although it comes earlier.
A card has the scoped rule below. Why does the text in .content still use Georgia?
@scope (.card) to (.content) {
:scope { font-family: Georgia, serif; }
}
Answer
The scope limit only stops the scoped selectors from matching elements in .content. font-family is inherited, so the value set on the card itself still passes down to all its descendants, including those past the limit.
Review quiz
Test yourself on Part 3 with the CSS Part 3 review quiz.
Previous: CSS Nesting: The & Selector, Nested At-Rules and Their Subtleties
Next: CSS Value Types: Keywords, Strings, URLs, Numbers and Percentages

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.