Sub topic 7
Resolving conflicts with scope proximity
This chapter assumes you know the cascade (specificity and order of appearance), basic selectors and combinators, and how @scope roots work. It covers what happens when two @scope rules match the same element and set the same property.
The problem: nested scopes
Components often nest. A light-themed panel can contain a dark-themed panel, and each theme styles links with its own @scope rule. A link inside the dark panel is also inside the light panel, so both rules match it.
Without @scope, the usual tie-breaker is order of appearance, and the last rule wins. That is the wrong answer here, because you want the innermost theme to apply no matter how the stylesheet is ordered.
Where proximity sits in the cascade
Scope proximity is a step in the cascade. The browser only uses it when the earlier steps leave a tie.
- Importance and origin, then layers.
- Specificity.
- Scope proximity.
- Order of appearance.
Proximity sits above order of appearance and below specificity. It never overrides a rule that has higher specificity.
Measuring proximity
Proximity is the number of generational hops in the DOM tree between the element and the scope root of the rule that matched. A smaller number means the rule's root is closer, and the closer root wins.
Here is the markup from the example below:
<div class="light">
<div class="dark">
<a href="#">Link</a>
</div>
</div>@scope (.dark) {
a { color: plum; }
}
@scope (.light) {
a { color: darkmagenta; }
}Both a rules have the same specificity, a single type selector. The .light rule appears later, so order of appearance alone would make the link dark magenta. Proximity is checked first, though. The .dark root is one hop from the link and the .light root is two hops away. The .dark rule wins, and the link is plum.
Why this works without specificity tricks
The scope root adds nothing to the specificity of the selectors inside @scope. Both rules above stay at the specificity of a. That is why a tie occurs and proximity gets to decide. You would otherwise need selectors like .dark a and .light a, which also tie in specificity. Proximity is what makes the nesting behave predictably.
You can reorder the two @scope blocks and the link stays plum.
When proximity does not apply
Proximity only breaks ties. If the outer rule has higher specificity, it wins even though its root is farther away:
@scope (.dark) {
a { color: plum; }
}
@scope (.light) {
a[href] { color: darkmagenta; }
}Here a[href] has specificity (0,1,1) and plain a has (0,0,1). The cascade settles the conflict at the specificity step, and the link is dark magenta. Proximity is never consulted.
Debugging checklist
When the wrong @scope rule wins, check in this order:
- Do both rules set the same property with the same importance?
- Are the specificities of the selectors inside
@scopeequal? Compare them as if the@scopewrapper were not there. - Which scope root is fewer hops from the element? That rule wins.
- If the distances are also equal, the later rule wins.
The distances can be equal when two different scope selectors match the same element. For example, @scope (.a) and @scope (.b) both match a single <div class="a b">. In that case order of appearance decides.
Summary table
| Cascade step | Question it answers | Example outcome |
|---|---|---|
| Specificity | Which selector is more specific? | `a[href]` beats `a` |
| Scope proximity | Which scope root is closer to the element? | `.dark` beats `.light` |
| Order of appearance | Which rule comes later? | Last `@scope` block wins a full tie |
Exercise
Given <div class="light"><div class="dark"><p><a href="#">Link</a></p></div></div>, with @scope (.dark) { a { color: plum; } } followed by @scope (.light) { a { color: darkmagenta; } }, what color is the link, and how many hops separate the link from each root?
Show answer
The link is plum. The .dark root is 2 hops away (a to p to .dark) and the .light root is 3 hops away. Specificity ties, so the closer root, .dark, wins despite the .light rule coming later.
Exercise
You change the .light rule to a:not(.muted) { color: darkmagenta; } and leave the .dark rule as a { color: plum; }. Which rule wins for a plain link inside .dark, and why?
Show answer
The .light rule wins. a:not(.muted) has the specificity of its argument, so it is (0,1,1) against (0,0,1) for a. Specificity is checked before scope proximity, so distance to the root is never compared.