You write .card img to round the corners of a card's thumbnail. A month later someone puts a user list inside a card, and every avatar in it gets rounded corners too. You write .dark a for links in a dark panel, then put a light panel inside a dark one, and the links in the light panel are styled by whichever rule came last in the file. These are scoping problems, and for years the answers have been naming conventions like BEM, build tools like CSS Modules, or the full isolation of Shadow DOM.
The @scope at-rule solves them in plain CSS. It lets you say where a set of styles starts, where it stops, and which rule wins when two scopes overlap. This article is for anyone who writes component styles and has fought selectors that reach further than intended. We will cover the syntax, the lower boundary that makes it useful, how it interacts with specificity and the cascade, and when you are better off with something else.
The basic syntax
#An @scope block takes a selector for the scope root and applies its rules only to that root and its descendants.
_10@scope (.card) {_10 img {_10 border-radius: 8px;_10 }_10_10 h2 {_10 font-size: 1.25rem;_10 margin-block: 0 0.5rem;_10 }_10}
So far that is close to writing .card img and .card h2, with one difference worth noticing straight away: the selectors inside do not get any extra specificity from the scope. img inside the block still has the specificity of a single type selector. We will come back to why that matters.
To style the root element itself, use :scope.
_11@scope (.card) {_11 :scope {_11 padding: 1rem;_11 border: 1px solid #ddd;_11 border-radius: 12px;_11 }_11_11 :scope > img {_11 width: 100%;_11 }_11}
Selectors without :scope are treated as descendants of the root, so img means any image inside a card.
Setting a lower boundary
#The part that makes @scope more than a nesting shortcut is the to clause. It sets a scope limit: elements that match the limit, and everything inside them, are excluded.
_10@scope (.card) to (.card__content) {_10 img {_10 border-radius: 8px;_10 }_10}
_10<article class="card">_10 <img src="/thumb.jpg" alt="" /> <!-- rounded -->_10 <div class="card__content">_10 <ul class="users">_10 <li><img src="/avatar.jpg" alt="" /></li> <!-- not affected -->_10 </ul>_10 </div>_10</article>
The styles apply from the card down to the content slot, but not into it. People often call this donut scope, because the styles cover a ring with a hole in the middle. It is exactly what component authors want: style your own markup, and leave whatever gets slotted into you alone.
The limit selector is matched relative to the scope root, so you can also use a broad pattern. This version stops at any nested element that marks itself as a slot:
_10@scope (.card) to ([data-slot]) {_10 p {_10 color: #444;_10 }_10}
Both the root and the limit can be any selector list, so @scope (.card, .panel) to (.content) is valid too.
Proximity: the nearest scope wins
#Here is the theming problem from the introduction.
_10.light a { color: #0645ad; }_10.dark a { color: #9cc3ff; }
_10<section class="dark">_10 <div class="light">_10 <a href="/docs">Docs</a>_10 </div>_10</section>
Both selectors have the same specificity, so the link gets whichever rule appears later in the stylesheet. Here that is the dark colour, which is wrong, because the link is inside the light panel.
@scope adds a step to the cascade called scope proximity. When two declarations come from scoped rules with the same specificity, the one whose scope root is closer to the element, counted in steps up the DOM tree, wins. Order of appearance only decides if proximity is also equal.
_10@scope (.light) {_10 a { color: #0645ad; }_10}_10_10@scope (.dark) {_10 a { color: #9cc3ff; }_10}
Now the link takes the light colour, because the .light root is one level up and the .dark root is two. Swap the nesting and the result swaps with it, no matter what order the rules are written in.
How it fits with specificity and the cascade
#It helps to know where proximity sits. For normal rules, the cascade compares origin and importance, then cascade layers, then specificity, then scope proximity, then order of appearance. Two consequences follow.
- Specificity still beats proximity. If an outer scope uses
a.buttonand an inner scope usesa, the outer rule wins, because it is more specific. Proximity only breaks ties. - Layers still beat both. A rule in a later cascade layer wins over a rule in an earlier one regardless of scope. If you already organise your CSS with
@layer, scopes work inside each layer.
The scope itself adds no specificity to the selectors inside it, as mentioned earlier. :scope counts as a pseudo-class, so :scope img has the specificity of a class plus a type. Keeping specificity low is part of the point: scoped component styles stay easy to override from outside.
Scoping styles to an element without a class
#If you leave out the prelude, an @scope block inside a style element scopes to that element's parent. This is handy for HTML generated by a CMS, for email-style snippets, or for a quick one-off section.
_14<section>_14 <style>_14 @scope {_14 :scope {_14 background: #fff8e1;_14 padding: 1rem;_14 }_14 p {_14 margin: 0;_14 }_14 }_14 </style>_14 <p>These styles only apply inside this section.</p>_14</section>
You do not need to invent a class name or an ID. The styles go wherever the markup goes.
A real component
#Here is a media object component that styles its own parts and stays out of its body slot, written so it works whatever is placed inside it.
_20@scope (.media) to (.media__body > :not(h3)) {_20 :scope {_20 display: flex;_20 gap: 1rem;_20 align-items: flex-start;_20 }_20_20 img {_20 flex: none;_20 width: 64px;_20 height: 64px;_20 border-radius: 50%;_20 object-fit: cover;_20 }_20_20 h3 {_20 margin: 0 0 0.25rem;_20 font-size: 1rem;_20 }_20}
_10<div class="media">_10 <img src="/avatar.jpg" alt="Sam" />_10 <div class="media__body">_10 <h3>Sam</h3>_10 <div class="comment">_10 <img src="/screenshot.png" alt="Screenshot of the bug" />_10 </div>_10 </div>_10</div>
The avatar and the name are styled. The screenshot inside the comment is not, because it sits beyond the limit. The class names are short and plain, and you do not need a naming convention to stop collisions.
A note on & inside @scope
#Inside @scope, & refers to the scope root selector, much as it does in CSS nesting. It behaves slightly differently from :scope: the spec defines it as :where(:scope), so it adds no specificity, but browser handling of & in this position has varied between engines and versions. For styling the root, :scope is the safer and clearer choice.
Browser support
#@scope is supported in all major engines:
- Chrome and Edge from version 118, released in 2023.
- Safari from version 17.4, released in 2024.
- Firefox from version 146, released in December 2025.
With Firefox's release, @scope became Baseline newly available. Users on older browsers, especially older Firefox versions, will not get any of the styles inside the block, because an unsupported at-rule is ignored as a whole. If those users matter to you, keep essential layout outside @scope, or wrap scoped enhancements in a feature query.
_10@supports at-rule(@scope) {_10 /* scoped styles */_10}
Be careful with that example, though: at-rule() inside @supports is newer than @scope itself. It arrived in Chrome and Edge 148 and Firefox 157 and is not yet in stable Safari, so a browser that understands @scope may still skip the block. For most sites today, the simpler approach is to treat @scope as a progressive enhancement and check that the page is still usable without it.
When not to use it
#- When you need real isolation.
@scopelimits where your styles go. It does not stop outside styles from coming in. A globalimgrule still applies inside your scope. If a widget must look the same on any host page, Shadow DOM is the tool. - When a single class does the job. If an element has its own class and nothing nests inside it,
.badgeis clearer than@scope (.badge) { :scope { } }. - When your build already scopes for you. CSS Modules, Vue's scoped styles and similar tools rewrite class names at build time. Adding
@scopeon top is rarely worth the extra layer, though the lower boundary can still help with slotted content. - When you must support browsers from before late 2025 without a fallback, and the scoped styles are essential to the layout.
Wrapping up
#@scope (root)limits rules to an element and its descendants, and:scopetargets the root itself.to (limit)sets a lower boundary, so component styles stop before slotted content.- When specificity ties, the nearest scope root wins, which fixes nested theme problems without relying on source order.
- Scoping adds no specificity, so scoped styles stay easy to override, and layers still take priority.
- It is Baseline newly available since Firefox 146, so treat it as an enhancement if you support older browsers.
Test yourself
Testing yourself is one of the best ways to make what you read stick. Turn over each card and answer the 5 questions to see how much you remember.
quick question
Was this article helpful?
by Danny