Sub topic 6
How @scope affects specificity
This chapter assumes you know CSS selectors and combinators, how the cascade orders declarations (importance, specificity, order of appearance), and the basics of CSS nesting. Earlier chapters showed how @scope limits where a rule applies. This chapter covers a side effect that surprises many people: a scope changes where a rule applies, but it does not make the rule stronger.
The short version
When a browser compares two competing declarations, it calculates each selector's specificity as three counts: IDs, then classes, attributes and pseudo-classes, then type selectors and pseudo-elements. For example, #sidebar a is (1,0,1).
Inside an @scope block, the selector in the scope's prelude does not count. A rule written as a inside @scope (#sidebar) has the specificity of a alone, which is (0,0,1). It does not become (1,0,1).
Two details follow from this:
- The scope root selector in the prelude, such as
#sidebaror.card, adds nothing to the rules inside. - The scope limit, the part after
to, adds nothing either.
The root still determines which elements the rule can match. It just has no weight in the cascade.
Why the root adds no weight
Inside @scope, a relative selector like a is treated as if it were written :where(:scope) a. The :where() pseudo-class always has zero specificity, no matter what is inside it. The scope root therefore acts as a zero-weight ancestor condition.
Compare this with CSS nesting, which you may already know:
/* Nesting: the parent selector contributes its specificity */
#sidebar {
a { color: green; } /* behaves like :is(#sidebar) a, specificity (1,0,1) */
}
/* Scope: the root contributes nothing */
@scope (#sidebar) {
a { color: green; } /* behaves like :where(:scope) a, specificity (0,0,1) */
}Both rules style links inside #sidebar. They differ only in how hard they win. A nested rule inherits the weight of its parent, so it is a poor choice if you want low specificity. A scoped rule stays light, so ordinary rules can override it easily.
A worked example
Here is the specificity example from the tutorial plan, with markup added so you can see each result:
<div id="sidebar">
<a href="#">Plain link</a>
<a class="link" href="#">Classed link</a>
</div>
<a class="link" href="#">Link outside the sidebar</a>#sidebar a { color: blue; }
@scope (#sidebar) {
a { color: green; }
}
.link { color: red; }First, list the specificity of each rule:
| Rule | Specificity | Matches |
|---|---|---|
| `#sidebar a` | (1,0,1) | Links inside `#sidebar` |
| `a` inside `@scope (#sidebar)` | (0,0,1) | Links inside `#sidebar` |
| `.link` | (0,1,0) | Any element with class `link` |
Now the result for each link:
| Link | Matching rules | Winner | Why |
|---|---|---|---|
| Plain link in `#sidebar` | blue (1,0,1), green (0,0,1) | blue | Higher specificity beats the later `@scope` rule |
| Classed link in `#sidebar` | blue (1,0,1), green (0,0,1), red (0,1,0) | blue | (1,0,1) beats (0,1,0) |
| Classed link outside `#sidebar` | red (0,1,0) | red | Only `.link` matches |
The green rule never wins in this stylesheet. Against .link, it loses because (0,1,0) is larger than (0,0,1). Against #sidebar a, it loses because the ID counts there. Order of appearance does not help it, even though it comes after the blue rule. Order is only consulted when specificity is equal.
If you delete the #sidebar a rule, the plain link in the sidebar turns green, because the scoped rule is now the only one that matches it. The classed link in the sidebar turns red, because .link (0,1,0) beats the scoped a (0,0,1).
This is the main practical lesson. A scoped element selector is weak. If you write @scope (.card) { a { ... } } and the page has a global .link rule, the global rule wins on any link that has that class.
Where the cascade checks specificity
Specificity is one step in a fixed order. For normal declarations, the browser asks these questions in order and stops at the first one that picks a winner:
Two consequences matter for @scope:
- Scoping alone does not defeat any rule that has higher specificity. It only helps when specificity is equal.
- Scope proximity is a tie-breaker that runs after specificity. The next chapter covers it. For now, remember that it never overrides a specificity difference.
Specificity of `:scope`
The :scope pseudo-class is a normal pseudo-class, so it counts as (0,1,0). That makes these two rules different in weight:
@scope (.card) {
img { width: 100%; } /* (0,0,1) */
:scope img { width: 50%; } /* (0,1,1) */
}For an img inside .card, the second rule wins. You write the same elements, but the second selector names :scope explicitly, so it counts a class-level piece. The implicit form uses :where(:scope) and counts nothing.
The same applies when the rule targets the root. In this stylesheet, the root rule has specificity (0,1,0) because of :scope:
@scope (.card) {
:scope {
border: 1px solid #ccc;
padding: 1rem;
}
}A plain .card { padding: 2rem; } elsewhere has the same specificity, (0,1,0). Specificity is then tied, so the cascade moves on to the later steps. If you need the scoped version to win reliably, you cannot rely on :scope alone. You would have to raise specificity some other way, or place the global rule so it does not compete.
Practical effects
Scoped rules are easy to override
This is usually good. A component's default styles should lose to a more specific page-level adjustment. If a theme author writes .theme-dark a { color: white; }, that rule (0,1,1) beats a scoped a (0,0,1) without needing !important.
You cannot rely on the scope to beat global styles
Suppose a global stylesheet has nav a { color: gray; } at (0,0,2). Then this rule loses, even though it is more local:
@scope (.promo) {
a { color: crimson; } /* (0,0,1), loses to nav a if the link is inside nav */
}Common ways to deal with it:
- Write a more specific selector inside the scope, such as
a.cta, at (0,1,1). - Use
:scope aif you want to add one class-level unit to the weight. - Put global styles in a cascade layer (
@layer) so they lose by layer order, not by specificity. This is usually the cleanest option when you control both stylesheets.
The root selector can be as plain or as strong as you like
Because the root contributes nothing, you can pick it for clarity and reuse. Writing @scope ([data-component="card"]) costs nothing in weight, while the same attribute selector in a normal rule would add (0,1,0). Using an ID as a scope root, such as @scope (#sidebar), does not carry the usual "IDs are hard to override" penalty for the rules inside.
Switching from BEM-style selectors changes weight
Suppose you migrate .card .title { ... } (0,2,0) into @scope (.card) { .title { ... } } (0,1,0). The rule is now lighter. Any other rule that used to lose to it, such as .title { ... } elsewhere, may now tie or win. Check for conflicts when moving existing CSS into scopes.
A runnable test
You can verify all of this in a single file. Save it as specificity.html and open it in a browser that supports @scope:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>@scope specificity</title>
<style>
#sidebar a { color: blue; }
@scope (#sidebar) {
a { color: green; }
}
.link { color: red; }
</style>
</head>
<body>
<div id="sidebar">
<p><a href="#">Plain link in the sidebar (blue)</a></p>
<p><a class="link" href="#">Classed link in the sidebar (blue)</a></p>
</div>
<p><a class="link" href="#">Classed link outside the sidebar (red)</a></p>
</body>
</html>Then try these edits one at a time and predict the colors before you reload:
- Delete the
#sidebar arule. - Change the scoped rule to
:scope a { color: green; }. - Restore the original and wrap
#sidebar ain@layer base { ... }. Layers change the outcome, because unlayered rules beat layered ones regardless of specificity.
Common mistakes
- Assuming the root adds weight. A scoped
adoes not beat a global.linkjust because it is "more specific" in the human sense. - Using
!importantto compensate. Importance sits above specificity in the cascade and makes later overrides harder. Prefer:scope, a class on the target, or layers. - Expecting order to rescue a weak scoped rule. A later
@scopeblock does not beat an earlier rule with higher specificity. - Forgetting that
:scopeis a pseudo-class. Writing it explicitly raises weight. Leaving it out keeps weight low. Both are valid, so choose deliberately.
Summary
- The prelude selector and the scope limit do not contribute specificity.
- A relative selector inside
@scopebehaves like:where(:scope) selector, so only the part you write counts. - Writing
:scopeexplicitly adds (0,1,0), because it is a pseudo-class. - Specificity is checked before scope proximity and order of appearance, so a lighter scoped rule still loses to a heavier rule that matches the same element.
Exercise
Given this CSS and the markup <div class="box"><a class="cta" href="#">Go</a></div>, what color is the link, and why?
.box a { color: orange; }
@scope (.box) {
a.cta { color: teal; }
}
Show answer
The link is orange. .box a has specificity (0,1,1). The scoped selector a.cta has specificity (0,1,1) as well, because the .box root in the prelude adds nothing but the class .cta adds (0,1,0) and the type adds (0,0,1)... Recount: a.cta is one class plus one type, so (0,1,1). The two rules tie on specificity, and the cascade moves on. Scope proximity does not separate them here, because the unscoped .box a rule is not compared in a way that favors it, so order of appearance decides. The @scope rule comes later, so the link is teal.
Exercise
A page has nav a { color: gray; } globally. A component uses @scope (.promo) { a { color: crimson; } } and its links sit inside both nav and .promo. The links stay gray. Give two ways to fix this without !important, and name the specificity of each scoped selector you propose.
Show answer
The global rule has specificity (0,0,2) and the scoped a has (0,0,1), so the global rule wins. One fix is to write :scope a inside the scope, which is (0,1,1) because :scope counts as a pseudo-class. Another is to write a.cta or a[href], which is (0,1,1), and add the class or attribute to the markup if needed. A third option is to move the global rule into an @layer so that the unlayered scoped rule beats it regardless of specificity.