Sub topic 11
What @scope does not isolate
This chapter assumes you know the cascade, specificity, inheritance, and the basic @scope syntax with scope roots, scope limits and :scope. It covers what @scope leaves unchanged, so you don't mistake it for a style sandbox.
The core idea: @scope restricts rules, not elements
An @scope block limits where its own rules can match. It does not shield the elements in that region from anything else. Three things still reach those elements:
- Global rules written outside the block.
- Inherited values from ancestors.
- Rules from other
@scopeblocks that cover the same elements.
Outside rules still apply
A global rule matches an element whether or not that element is in a scope.
a {
text-decoration: underline;
font-size: 1.25rem;
}
@scope (.card) {
a {
color: rebeccapurple;
}
}Links inside .card are purple, and they are also underlined and 1.25rem. The global a rule leaks in. @scope only stops the purple from leaking out to links elsewhere. This is the main difference from Shadow DOM, which blocks most outside selectors from matching inside a shadow tree.
Higher specificity outside still wins
The scope root adds no specificity to the selectors inside the block. A scoped rule can therefore lose to a more specific outside rule, whatever the source order.
.card a {
color: blue; /* specificity 0,1,1 */
}
@scope (.card) {
a {
color: green; /* specificity 0,0,1 */
}
}Links in .card stay blue. Scope proximity is checked only after specificity, so it can't rescue the green rule here. If a scoped rule keeps losing, compare specificities first.
Inheritance crosses scope limits
A scope limit stops selectors from matching. It does not stop inheritance. Inherited properties such as color, font-family and line-height pass through the excluded subtree like any other part of the DOM.
<div class="card">
<p>Card text</p>
<div class="card-content">
<p>Content text</p>
</div>
</div>@scope (.card) to (.card-content) {
:scope {
color: green;
}
p {
font-weight: 700;
}
}The first paragraph is green and bold. The second paragraph is not bold, because the limit excludes it from the p rule. It is still green, because color inherits from .card through .card-content. If you need the donut hole to look unstyled, set the property explicitly there, for example with color: initial or a rule that targets that subtree.
The root and its context are not protected
The scope root can be styled from outside as well. A global .card { margin: 2rem; } still applies, and so does any other rule that matches .card. A scope root also doesn't prevent the same element from being the root of several scopes, or from sitting inside someone else's scope.
Not DOM or script isolation
@scope is purely a CSS selector feature. It does not change the DOM, so:
document.querySelector("a")still finds links inside the scope.- Scripts can read and write the same elements as before.
- There is no separate tree, and events behave as usual.
Shadow DOM changes the DOM structure itself. @scope doesn't.
Summary of isolation
| Concern | Isolated by @scope? |
|---|---|
| Scoped rules matching elements outside the root | Yes |
| Scoped rules matching inside a scope limit | Yes |
| Global rules matching elements inside the scope | No |
| Higher-specificity outside rules overriding scoped ones | No |
| Inherited properties and custom properties | No |
| JavaScript selectors and DOM structure | No |
Practical consequences
- Treat
@scopeas a way to avoid writing long prefixes and naming conventions. It does not replace a reset or a design system. - If you need strong separation from unknown outside CSS, such as an embedded widget, use Shadow DOM. If you only need to keep your own rules from spreading,
@scopeis enough. - When a style behaves unexpectedly, open the browser's styles panel. It lists every matching rule, including global ones.
Exercise
A page has .card p { color: blue; } and later @scope (.card) { p { color: green; } }. Text in a p inside .card is blue. Why, and what is one fix that keeps the scoped rule?
Show answer
The outside rule .card p has specificity 0,1,1. The scoped p has 0,0,1 because the scope root adds nothing. Specificity is compared before scope proximity or order, so blue wins. Fixes: raise the scoped selector's specificity, for example p.lead with a matching class on the element, or remove or lower the outside rule.
Exercise
In @scope (.card) to (.card-content) { :scope { color: green; } }, will text inside .card-content be green? Explain.
Show answer
Yes. The limit stops selectors from matching in that subtree, but color is inherited. The text inherits green from .card through the excluded subtree, unless another rule sets its color.