Sub topic 6
How @scope affects specificity
This chapter assumes you know CSS selectors, the cascade (specificity, then order of appearance), and the basic @scope (root) { ... } syntax from earlier chapters. You will learn how to calculate the specificity of a rule inside @scope, where the scope root counts and where it doesn't, and what mistakes to avoid.
The scope root adds no specificity
A selector inside @scope is matched relative to the scope root. The browser behaves as if the selector had :scope in front of it, joined by a descendant combinator. That implied part adds zero specificity.
So the specificity of a rule inside @scope is the specificity of the selector you wrote, and nothing else.
@scope (#sidebar) {
a { color: green; }
}Even though the root is an ID selector, the rule a has specificity (0,0,1), the same as a bare a outside any scope. The ID in the prelude only decides which elements the rule can match. It does not make the rule stronger.
Comparing against rules outside the scope
Consider this stylesheet and markup.
.link { color: red; }
@scope (#sidebar) {
a { color: green; }
}<aside id="sidebar">
<a class="link" href="#">Link</a>
</aside>Both rules match the anchor. .link has specificity (0,1,0) and the scoped a has (0,0,1), so the class wins and the link is red. This holds even if the @scope block comes later in the file, because specificity is compared before order of appearance.
Now add an ID selector rule:
#sidebar a { color: blue; }
.link { color: red; }
@scope (#sidebar) {
a { color: green; }
}The same anchor is now blue. The rule #sidebar a has specificity (1,0,1), which beats both .link and the scoped a. This is the main difference between the two forms. Writing #sidebar a puts the ID's weight into the rule, and writing @scope (#sidebar) { a { ... } } does not.
| Rule | Specificity |
|---|---|
| `#sidebar a` | (1,0,1) |
| `.link` | (0,1,0) |
| `a` inside `@scope (#sidebar)` | (0,0,1) |
Why this is useful
Low specificity makes scoped rules easy to override. A component's styles stay close to the weight of plain element selectors, so a utility class or a theme rule can replace them without !important or longer selectors. Moving a rule from .card a into @scope (.card) lowers its specificity by one class.
The trade-off is that you can no longer rely on the scope root to win against other rules. If an existing global rule such as #sidebar a already styles the same elements, your new scoped rule will lose to it.
Writing :scope explicitly
If you write :scope yourself, it counts. :scope is a pseudo-class, so it has the specificity of a class, (0,1,0).
@scope (.card) {
:scope { border: 1px solid #ccc; }
:scope a { color: rebeccapurple; }
}Here :scope alone has specificity (0,1,0), and :scope a has (0,1,1). The implied :scope is free, and a typed :scope is not. This lets you choose. Leave it out for low-weight rules, and add it when you want a scoped rule to beat a plain class selector.
The diagram shows that specificity is checked first. Scope proximity only matters when specificity ties. The next chapter covers proximity.
Common errors
- Expecting the root to add weight. A scoped
adoes not beat.link. Check the specificity of the selector you wrote. - Forgetting that older rules still apply. An existing
#sidebar arule outranks anything scoped under#sidebarthat uses only element selectors. - Miscounting
:scope. An explicit:scopeadds (0,1,0). Do not treat it as free. - Fixing conflicts with
!important. Adjust the selector, or write:scopeexplicitly, instead.
Exercise
Given #nav a { color: blue; } and @scope (#nav) { a { color: green; } } placed after it, what color is an <a> inside #nav, and why?
Show answer
Blue. #nav a has specificity (1,0,1). The scoped a has (0,0,1) because the scope root adds nothing. Specificity is compared before source order, so the later @scope rule does not win.
Exercise
What is the specificity of :scope a inside @scope (.card)?
Show answer
(0,1,1). The explicit :scope pseudo-class counts as one class-level item, and a adds one type selector. The .card in the prelude adds nothing.