Sub topic 7
Resolving conflicts with scope proximity
This chapter covers how the cascade decides between two @scope rules that match the same element with equal specificity. It assumes you know CSS selectors, the cascade (origin, specificity, order of appearance), and the basics of @scope from earlier chapters: scope roots, :scope, and scope limits.
The problem proximity solves
Components nest. A light card can contain a dark card, and a dark card can contain a light one. Each theme styles links with its own @scope rule. When a link sits inside both, two rules match it. Which one wins?
Before @scope, the answer came from specificity and then source order. Source order is the wrong tiebreaker for nested themes. The rule that appears later in the stylesheet would win everywhere, even when the link sits inside the other theme's component.
Scope proximity fixes this. When two competing declarations come from @scope rules, the cascade prefers the one whose scope root is closer to the element in the DOM tree.
Where proximity sits in the cascade
The cascade compares declarations in a fixed order. For normal author rules, the steps that matter here are:
- Origin and importance.
- Context (shadow DOM tree rules).
- Element-attached styles (the
styleattribute). - Layers.
- Specificity.
- Scoping proximity.
- Order of appearance.
Proximity is checked after specificity and before order of appearance. Two consequences follow:
- Proximity never overrides specificity. A more specific selector wins even if its scope root is farther away.
- Proximity overrides source order. A rule that comes earlier can beat a rule that comes later.
A worked example
Here is the markup and CSS from the topic. The link is inside a .dark element, which is inside a .light element.
<div class="light">
<div class="dark">
<a href="#">Link</a>
</div>
</div>@scope (.dark) {
a { color: plum; }
}
@scope (.light) {
a { color: darkmagenta; }
}Both rules match the link. Both inner selectors are a lone a, and the scope root adds nothing to specificity (covered in the previous chapter), so specificity is equal. Without proximity, the later rule would win and the link would be darkmagenta.
With proximity, the cascade measures the distance from each scope root to the link:
| Rule | Scope root | Steps from root to the link |
|---|---|---|
| `@scope (.dark)` | the `.dark` div | 1 |
| `@scope (.light)` | the `.light` div | 2 |
The .dark root is closer, so the link is plum. The order of the two rules does not matter. Swap them and the result stays the same.
How distance is measured
Proximity counts the number of generational hops (parent to child) between the scope root and the element. It does not count siblings. A smaller number wins. If an element is the scope root itself, the distance is 0.
Two details matter in practice:
- The scope root is the one that matched for that rule. If a selector like
@scope (.card)matches several ancestors of the element, the nearest matching ancestor is used for that rule. - Each rule is measured on its own. Proximity compares the root of one rule against the root of the other. It does not compare how deeply the inner selector matched.
Consider an element that is a descendant of two nested .card elements. A single @scope (.card) rule applies to it through both roots. The closest root gives the distance used when that rule competes against another.
Proximity beats order, but not specificity
You can break the tie in the other direction by giving the outer rule more specificity. Add a class to the inner selector of the .light rule.
@scope (.dark) {
a { color: plum; }
}
@scope (.light) {
a.nav { color: darkmagenta; }
}<div class="light">
<div class="dark">
<a class="nav" href="#">Link</a>
</div>
</div>The selector a.nav has higher specificity than a. The cascade settles the conflict at the specificity step and never reaches proximity. The link is darkmagenta, even though the .light root is farther away.
Proximity applies only between scoped rules
Proximity compares scope roots. A rule outside any @scope has no scope root. How does it compare?
The specification says that a declaration from a scoped rule has proximity, and a declaration from an unscoped rule is treated as having infinite distance. So when specificity ties, any scoped rule beats an unscoped one, regardless of order.
Here is a case that surprises people:
a { color: red; }
@scope (.card) {
a { color: blue; }
}Both selectors have the specificity of one type selector. The scoped rule has a finite proximity, so links inside .card are blue. Reverse the order and nothing changes: the scoped rule still wins.
Compare this with the donut-scope and specificity example from the earlier chapter, where .link { color: red; } has a class selector. A class is more specific than the lone a inside @scope, so specificity decides and the class rule wins. Proximity never gets a chance.
Proximity with a scope limit
A scope limit removes a subtree from the scope. An element inside the limit's subtree is not in scope for that rule, so the rule does not apply and proximity does not enter. This affects nested themes in a useful way.
Suppose you want the outer theme to stop at any nested component of the same kind:
@scope (.light) to (.dark) {
a { color: darkmagenta; }
}
@scope (.dark) to (.light) {
a { color: plum; }
}Each rule styles links only in its own theme and stops where the other theme begins. The two rules never match the same link, so no conflict exists and no tiebreaker is needed. This is often easier to reason about than relying on proximity.
The trade-off is verbosity. Each theme must name every other theme it could contain. Proximity needs no such list, so it scales better when themes are added later.
| Approach | Conflict resolved by | Cost |
|---|---|---|
| Proximity only | Closer scope root wins | Fragile if specificity differs between rules |
| Scope limits | Rules never overlap | Each rule must name the other roots |
Common errors
Expecting proximity to override a more specific selector
This was shown above. Check specificity before proximity.
Assuming proximity counts siblings or document position
Proximity measures ancestor distance only. Two roots that are not ancestors of the element cannot both apply.
Mixing unscoped rules and scoped rules and relying on order
Scoped rules beat unscoped rules of equal specificity regardless of order. If you move a legacy global rule below a scoped rule and expect it to win, it will not.
Forgetting that `:scope` rules compete too
A rule such as :scope { color: navy; } inside @scope (.card) also has a root, and the element is the root itself, so its distance is 0. In a nested pair of cards, the inner card's :scope rule beats the outer card's rules on that inner card element. Remember that :scope has the specificity of a pseudo-class, so it also carries more weight than a bare type selector.
Debugging
Open the element in browser developer tools and inspect the Styles pane. Overridden declarations appear struck through. If two scoped rules tie on specificity, look at which scope root is nearer in the DOM tree. Also check for a rule you forgot about that has higher specificity.
Putting it together
A practical pattern for nested themes uses proximity for the default behavior and keeps inner selectors simple:
@scope (.light) {
:scope { background: white; color: #222; }
a { color: darkmagenta; }
}
@scope (.dark) {
:scope { background: #222; color: white; }
a { color: plum; }
}<div class="light">
<p><a href="#">Light link</a></p>
<div class="dark">
<p><a href="#">Dark link</a></p>
<div class="light">
<p><a href="#">Light link again</a></p>
</div>
</div>
</div>The first link is darkmagenta. The second is plum, because the .dark root is nearer. The third is darkmagenta again, because the innermost .light is the nearest root, at distance 2, against the .dark root at distance 3. Nesting works to any depth with no extra rules, and the :scope rules also resolve correctly because each themed container is the root of its own rule.
Exercise
A page contains <div class="a"><div class="b"><p>Text</p></div></div> and this CSS: @scope (.b) { p { color: green; } } followed by @scope (.a) { p.note { color: orange; } }. The p element has no class. What color is the text? Then add class="note" to the p. What color is it now, and which cascade step decides it?
Show answer
Without the class, only the .b rule matches (p.note does not match), so the text is green. With class="note", both rules match. p.note has higher specificity than p, so the specificity step decides and the text is orange. Proximity is never checked, even though .b is closer.
Exercise
Given p { color: red; } placed after @scope (.card) { p { color: blue; } }, what color is a paragraph inside .card, and why?
Show answer
Blue. Both selectors have equal specificity. The scoped rule has a finite proximity and the unscoped rule has none, so the scoped rule wins at the proximity step. Order of appearance is never reached.
Exercise
Two rules, @scope (.x) { a { color: red; } } and @scope (.y) { a { color: blue; } }, apply to a link. The link is a child of .x, and .x is a child of .y. How far is each root from the link, and what color is the link? What if the rules are written in the opposite order?
Show answer
The .x root is 1 step away and the .y root is 2 steps away. The nearer root wins, so the link is red. Swapping the order of the rules does not change this, because proximity is checked before order of appearance.