Sub topic 11
What @scope Does Not Isolate
@scope limits which elements a rule can match. It does not wall off a region of the page. Developers who expect a wall write CSS that leaks or is overridden in surprising ways. This chapter covers where the boundary stops and how to design around it.
This chapter assumes you know:
- selectors and combinators
- the cascade order: origin, layers, specificity, then order of appearance
- the basics of
@scoperoots, limits, and:scope - CSS nesting
What the boundary restricts
An @scope rule changes one thing: which elements its selectors can match. Inside @scope (.card), the selector a matches only anchors that are descendants of a .card element. The browser treats that as a relative selector, roughly :where(:scope) a.
Nothing else changes. The scoped rules are still ordinary style rules in the page's cascade. They compete with every other rule, and inheritance still follows the DOM tree.
So @scope stops your rules from matching elements outside the region. It does not stop three other things:
- Inherited values flowing into the region.
- Outside rules matching elements inside the region.
- Scripts and other tools reaching into the region.
The diagram shows both directions.
Inheritance crosses the boundary
Inheritance works on the DOM tree, not on selector matching. A scope limit stops selectors from matching the hole. It does not stop inherited properties from reaching it.
<div class="card">
<p>Card text</p>
<div class="card-content">
<p>Hole text</p>
</div>
</div>@scope (.card) to (.card-content) {
:scope {
color: crimson;
}
p {
font-weight: 700;
}
}The first paragraph is crimson and bold. The second paragraph is outside the scope, so it is not bold. It is still crimson, because color is inherited from .card through .card-content.
To stop the leak, reset the property on the limit element with a rule outside the scope:
.card-content {
color: CanvasText;
}The limit element is excluded from the scope, so a rule inside @scope cannot reset it.
Inherited properties include color, font-family, font-size, line-height, text-align, visibility, and all custom properties. The same applies in the other direction. If an ancestor of the root sets font-family, every element in your scope inherits it unless you override it.
Outside rules still match inside elements
A scope does not shield its contents from other stylesheets. Consider this page:
<div class="page">
<section class="card">
<a href="#">Read more</a>
</section>
</div>.page a {
color: blue;
}
@scope (.card) {
a {
color: green;
}
}The link is blue. The @scope rule comes later, but specificity is compared before order of appearance. .page a has specificity (0,1,1). Inside @scope, the root adds nothing, so a has (0,0,1). The global rule wins.
Fixes, from least to most intrusive:
- Lower the specificity of the outside rule, for example
:where(.page) a. - Raise the specificity of the scoped rule, for example
a:any-link, or:scope aif you accept that:scopecounts as a pseudo-class (0,1,0). - Put both rules in cascade layers and order the layers on purpose.
Specificity is not the only way an outside rule can beat a scoped one. Declarations marked !important outside the scope beat normal scoped declarations. Rules in a later or earlier layer can too, as the next section shows.
Layers and importance sit above the scope
Cascade layers are compared before specificity, so they interact with @scope in ways that surprise people.
@layer base {
.card a {
color: blue;
}
}
@scope (.card) {
a {
color: green;
}
}The link is green. Unlayered normal declarations beat layered ones, whatever their specificity. If you move the scoped rule into a layer, it competes by layer order instead:
@layer base, components;
@layer base {
.card a {
color: blue;
}
}
@layer components {
@scope (.card) {
a {
color: green;
}
}
}The link is green here too, because components is declared after base. Reverse the order in the first line and the link turns blue. Scope does not change this ranking.
Proximity only ranks scoped rules
Scope proximity breaks ties between scoped rules whose roots differ. It counts the generations (parent-child steps) between a scope root and the styled element, and the closer root wins. It sits after specificity and before order of appearance:
| Cascade step | Applies to scoped rules? |
|---|---|
| Origin and importance | Yes, unchanged |
| Layers | Yes, unchanged |
| Specificity | Yes, the root adds none |
| Scope proximity | Only between scoped rules |
| Order of appearance | Yes, last tiebreaker |
Proximity never rescues a scoped rule from a global rule with higher specificity. It also does not help when two scopes target the same element at different specificities:
@scope (.dark) {
a.nav {
color: plum;
}
}
@scope (.light) {
a {
color: darkmagenta;
}
}For <div class="light"><div class="dark"><a class="nav" href="#">Link</a></div></div>, the link is plum. The first rule has higher specificity (0,1,1 against 0,0,1), so proximity is never consulted. Proximity decides only when specificity is equal.
Nested roots overlap
If the root selector can match elements at several depths, scopes overlap. A .card inside a .card puts the inner contents in both scopes.
<div class="card">
<a href="#">Outer link</a>
<div class="card">
<a href="#">Inner link</a>
</div>
</div>@scope (.card) {
a {
color: teal;
}
}Both links are teal. The rule applies once for each matching root, and the declarations are identical. If you want the outer card's styles to skip the inner card, use the inner card as the limit:
@scope (.card) to (.card) {
a {
color: teal;
}
}Only the outer link is teal now. The limit .card must be a descendant of the root, so the inner card starts the hole. This works because the limit is evaluated relative to the root. The inner card then needs its own styling, and it can get it from a separate rule with different values.
Not an encapsulation boundary
@scope is a selector feature. It does not create a separate tree, so these things stay global:
- JavaScript.
document.querySelectorAll('a')returns anchors inside and outside the scope. Scripts can add classes or inline styles to scoped elements. - IDs and names. Duplicate
idvalues still collide, scoped or not. - Other scopes. A scoped rule from another component can target the same elements if its root matches an ancestor.
- Third-party CSS. A reset like
* { margin: 0 }applies inside the scope. - The scope root itself. The root is the subject of
:scope, so a rule scoped to.cardcan style that root. Outside rules can style it too.
Shadow DOM is different. It creates a separate tree, so outside selectors do not match inside, and inside selectors do not match outside. Inheritance still crosses a shadow boundary, which is why custom properties are the standard theming channel for web components.
Here is how the approaches compare on what they isolate:
| Concern | `@scope` | BEM | CSS Modules | Shadow DOM |
|---|---|---|---|---|
| Your rules leak out | No | No, by convention | No, names are hashed | No |
| Outside rules reach in | Yes | Yes | Yes | No, except inheritance |
| Inheritance reaches in | Yes | Yes | Yes | Yes |
| Needs a build step | No | No | Yes | No |
| Needs naming discipline | No | Yes | No | No |
Pick @scope when you want to stop your own rules from leaking and avoid naming conventions. Pick Shadow DOM when you need protection from outside styles as well.
Common errors
- Expecting a limit to stop inheritance. The hole inherits from the scope. Reset the properties on the limit element from outside.
- Expecting a late scoped rule to win. Order of appearance comes after specificity and layers. Check both before reordering.
- Expecting proximity to beat specificity. It only breaks ties.
- Repeating the root selector inside the scope. This requires a second matching element and usually matches nothing.
- Treating the root as private. An ancestor's styles and outside rules still reach it.
Exercise
A page has .page a { color: blue; } followed by @scope (.card) { a { color: green; } }. A link inside .card inside .page renders blue. Name the cascade step that decides this, and give one change that makes the link green without touching the .page a rule.
Show answer
Specificity decides it. .page a has (0,1,1), and the a inside @scope has (0,0,1) because the scope root adds no specificity. The global rule wins even though the scoped rule comes later. One fix is to write :scope a in the scoped rule, which adds a pseudo-class and gives (0,1,0) plus one type, so (0,1,1). That then ties with .page a, and the later scoped rule wins on order of appearance. Another fix is a:any-link.
Exercise
Given @scope (.card) to (.card-content) { :scope { color: crimson; } }, why is text inside .card-content still crimson, and how do you stop it?
Show answer
color is an inherited property. The scope limit stops selectors from matching inside .card-content, but inheritance follows the DOM tree, so the text inherits crimson from .card. Add a rule outside the scope, such as .card-content { color: CanvasText; }, to reset it. A rule inside @scope cannot do this because the limit element is excluded from the scope.