Sub topic 4
Styling the root with :scope
This chapter assumes you can write an @scope (root) { ... } rule and know what a scope root is. It also assumes you know the cascade basics: specificity, then order of appearance. Here you will learn how the :scope pseudo-class lets a scoped rule target the scope root itself, which a plain selector inside @scope can't do.
The root is not matched by plain selectors
Consider this markup and rule:
<article class="card">
<h2 class="title">Trail map</h2>
<p>Updated weekly.</p>
</article>@scope (.card) {
.card {
border: 1px solid #ccc;
}
}The .card rule never applies. Selectors inside @scope match elements inside the scope. A selector is relative to the scope root, so .card means "a .card descendant of the root". No .card descendant exists here.
To style the root, use :scope. Inside an @scope rule, :scope matches the scope root element.
@scope (.card) {
:scope {
border: 1px solid #ccc;
padding: 1rem;
}
.title {
margin-block: 0 0.5rem;
}
}The :scope rule styles the <article> itself. The .title rule styles only titles within it.
What a selector means inside @scope
A selector in a scoped rule is handled in one of two ways:
- If it contains no
:scopeand no&, the browser treats it as if:where(:scope)were written in front of it..titlebehaves like:where(:scope) .title. - If it contains
:scope(or&), it is used as written. You control where the root appears in the selector.
The implicit prefix is wrapped in :where(), so it adds no specificity. An explicit :scope is a pseudo-class and adds the specificity of one class, (0,1,0).
@scope (.card) {
.title { color: navy; } /* same as :where(:scope) .title */
:scope .title { color: navy; } /* same match, but one class higher in specificity */
}Both rules match the same elements. The second wins any specificity contest against the first because of the extra class weight. The next chapter on specificity covers this in more detail.
Combining :scope with other selectors
:scope is a normal pseudo-class, so you can attach compound selectors to it. This is the usual way to style variants of the root.
@scope (.card) {
:scope {
display: grid;
gap: 0.5rem;
}
:scope.featured {
border-color: gold;
}
:scope:hover {
box-shadow: 0 2px 8px rgb(0 0 0 / 0.2);
}
:scope:has(img) {
grid-template-columns: 8rem 1fr;
}
}:scope.featuredmatches the root only when it also has the classfeatured. Write it without a space.:scope .featuredwould look for a descendant.:scope:hoverand:scope:has(img)filter the root by state or content.:scope > .titleselects direct children of the root only.
Matching the context around the root
A scoped rule may mention elements outside the scope, as long as the subject of the selector (the element the rule styles) is the root or something in scope. With :scope in the selector, you can write conditions on the root's surroundings:
@scope (.card) {
.dark :scope {
background: #222;
color: #eee;
}
}Here the subject is the card. The rule applies to a .card that sits inside a .dark ancestor. The .dark element is outside the scope, and that is allowed.
Custom properties on the root
A common use of :scope is to define custom properties that the rest of the component reads. This keeps the component's settings in one place.
@scope (.card) {
:scope {
--card-accent: rebeccapurple;
--card-gap: 1rem;
border-top: 4px solid var(--card-accent);
padding: var(--card-gap);
}
.title {
color: var(--card-accent);
}
}Custom properties are inherited, so they also reach descendants outside the scope, including content inside a scope limit. This follows from one rule: @scope limits which elements a selector can match. It doesn't block inheritance. A later chapter covers this in detail.
A page can override the values from outside:
.card.warning {
--card-accent: crimson;
}This works because .card.warning and :scope both have specificity (0,2,0) and (0,1,0) respectively, and .card.warning is higher.
:scope outside @scope
:scope has a meaning outside @scope too:
| Where it is used | What `:scope` matches |
|---|---|
| Inside `@scope (root)` | The scope root element |
| In a stylesheet, outside any `@scope` | The document root, like `:root` |
| In `element.querySelector(':scope > li')` | The element the method was called on |
The third row is a JavaScript feature that predates @scope. It lets you write child-only queries:
const list = document.querySelector("ul.menu");
const topLevel = list.querySelectorAll(":scope > li");
console.log(topLevel.length);Without :scope, the selector > li is invalid. The selector ul.menu > li would work too, but it depends on the list's class, and :scope doesn't.
Don't mix up the three meanings. In CSS, the meaning depends on whether the rule sits inside an @scope block.
Using :scope in the scope limit
The scope limit, the part after to, can also use :scope. There it refers to the same scope root. This lets you stop the scope at a child that sits directly under the root:
@scope (.tabs) to (:scope .tabs) {
:scope > .tab {
padding: 0.5rem 1rem;
}
}The limit (:scope .tabs) means "any .tabs that is a descendant of the current root". A nested tab set is therefore excluded from the outer tab set's styles. Without the limit, the outer rules would leak into an inner .tabs component. Scope limits get their own chapter, so treat this as a preview.
Nested scopes and multiple roots
The root selector may match several elements, and scopes can nest. Each match is its own scope, and :scope always means the root of the scope the rule belongs to.
<div class="panel" id="outer">
<p>Outer text</p>
<div class="panel" id="inner">
<p>Inner text</p>
</div>
</div>@scope (.panel) {
:scope {
border: 2px solid steelblue;
margin: 0.5rem;
}
p {
color: steelblue;
}
}Both #outer and #inner match .panel, so each gets a border. The inner paragraph is in scope for the outer panel and also for the inner panel. Both paths lead to the same declarations, so there's no conflict here. If the two panels used different rules, scope proximity would decide which one wins. A later chapter covers that case.
Common errors
- Writing the root's selector inside the block.
@scope (.card) { .card { ... } }matches nothing, unless a.cardis nested inside another. Use:scope. - Adding a space by mistake.
:scope .featuredselects descendants.:scope.featuredselects the root when it has the class. - Expecting
:scoperules to have zero specificity. Only the implicit prefix is zero. A written:scopecosts one class. - Using
:scopeat the top level and expecting a component root. Outside@scopeit matches the document root, so the rule styles<html>. - Forgetting that the subject must be in scope. A rule like
.dark { ... }inside@scope (.card)never styles the.darkancestor. Only a selector whose subject is the root or a scoped descendant matches.
A complete example
This page runs as shown in a browser that supports @scope. The .card and .note elements each get their own root styling.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:scope demo</title>
<style>
@scope (.card) {
:scope {
--accent: rebeccapurple;
border: 2px solid var(--accent);
border-radius: 0.5rem;
padding: 1rem;
max-width: 20rem;
}
:scope.featured {
--accent: goldenrod;
}
h2 {
color: var(--accent);
margin-block: 0 0.5rem;
}
}
</style>
</head>
<body>
<article class="card">
<h2>Standard card</h2>
<p>Purple accent.</p>
</article>
<article class="card featured">
<h2>Featured card</h2>
<p>Gold accent, set on the root.</p>
</article>
<h2>Page heading, not purple</h2>
</body>
</html>The root rule sets --accent, and :scope.featured replaces it. The h2 rule reads the variable. The last h2 is outside any card, so it isn't styled.
Exercise
Given @scope (.menu) { .menu { color: red; } } and a single <nav class="menu"> with no nested .menu element, why does the nav stay unstyled, and how do you fix it?
Show answer
Selectors inside @scope are relative to the scope root and match elements inside it, so .menu looks for a .menu descendant of the root. The root itself is never matched by a plain selector. Replace the inner selector with :scope, giving @scope (.menu) { :scope { color: red; } }.
Exercise
What is the difference between :scope.active and :scope .active inside an @scope rule?
Show answer
:scope.active has no space and matches the scope root when it has the class active. :scope .active has a descendant combinator and matches elements with class active inside the root, not the root itself.
Exercise
A stylesheet contains @scope (.card) { .dark :scope { background: #222; } }. Which element gets the background, and why is the .dark element allowed to be outside the scope?
Show answer
The .card root gets the background, but only when it has a .dark ancestor. The subject of the selector, the element being styled, is :scope, so it is the root. Other parts of a selector can match outside the scope. Only the subject must be in scope.
Exercise
What does :scope match in a top-level rule with no @scope, and in list.querySelectorAll(":scope > li")?
Show answer
In a top-level stylesheet rule it matches the document root element, like :root. In querySelectorAll it matches the element the method was called on, so the query returns only the direct li children of list.