Sub topic 3
Choosing a scope root
This chapter assumes you know CSS selectors and combinators, the cascade, and the basic @scope (selector) { ... } form from the previous chapter. It covers how the scope root is picked, what it includes, and how to choose a good one.
What the scope root is
The scope root is the element that an @scope rule is anchored to. You write its selector in parentheses after @scope. The browser finds every element in the document that matches that selector. Each match becomes a scope root, and the rules inside the block apply only to elements below it.
@scope (.card) {
img {
width: 100%;
}
}Two details matter here:
- The root selector is evaluated against the whole document, not against some enclosing scope.
.cardmatches every.cardon the page. - Selectors inside the block are relative to the root.
imgbehaves like "animgthat is a descendant of a.card".
The root itself is not selected
An inner selector matches descendants of the root. It never matches the root element itself. In this markup, the img rule affects the image inside the card and does not affect the .card div:
<div class="card">
<img src="a.jpg" alt="">
</div>
<img src="logo.jpg" alt="">The logo.jpg image sits outside every .card, so the rule ignores it. To style the root itself you need :scope, which a later chapter covers.
Several roots at once
A page usually has many matches for the root selector, and they can nest. An element can sit inside more than one scope at the same time.
If a .card contains another .card, an img in the inner card is a descendant of both roots. It matches the rule once. Nesting does not stack the effect, but it does matter for proximity, which a later chapter covers.
Root selectors can be more than one class
The prelude accepts any selector list, so you can use compound selectors, combinators, or several alternatives:
@scope (.card, .panel) {
a {
color: rebeccapurple;
}
}
@scope (main > article[data-theme="dark"]) {
p {
color: #eee;
}
}In the first rule, both .card and .panel elements are scope roots. In the second, only an article that is a direct child of main and has data-theme="dark" is a root. Keep the selector as plain as it can be. A complicated root selector is harder to read and breaks when the markup is reorganised.
Choosing a good root
A scope root should name a component boundary: the outermost element that "owns" the styles inside it. Use this comparison when deciding:
| Root selector | Good for | Risk |
|---|---|---|
| `.card` (a class) | Reusable components | Class name may be reused by unrelated code |
| `[data-component="card"]` | Marking components separately from visual classes | Verbose to write |
| `my-card` (custom element) | Web components or clearly named widgets | Needs a custom element in the markup |
| `#sidebar` (an ID) | One-off page regions | Cannot be reused, and an ID is hard to override elsewhere |
| `div` or `body` | Almost nothing | So broad that the scope gives no protection |
Choose a root that is stable, specific to one component, and present on the outermost element of that component. If the root is too high in the tree, the rules leak into neighbouring components. If it is too low, parts of the component fall outside it.
Common mistakes
- Expecting the root to match inner selectors.
@scope (.card) { .card { ... } }matches a.cardnested inside a card, not the outer one. - Using a root that is not in the DOM yet. If no element matches the prelude, the block applies to nothing. Check the class name first when a scoped rule seems to do nothing.
- Writing the root into the inner selectors.
@scope (.card) { .card img { ... } }is redundant and only matches images in a nested card. - Picking an overly generic root. A root like
sectionscopes every section on the page.
Exercise
Given @scope (.panel) { p { margin: 0; } } and the markup <div class="panel"><p>One</p><div class="panel"><p>Two</p></div></div><p>Three</p>, which paragraphs get margin: 0, and why?
Show answer
"One" and "Two" get it. Both are descendants of a .panel root ("Two" is inside the outer and the inner panel, but the rule applies once). "Three" is outside every .panel, so it is not matched.
Exercise
You write @scope (.card) { .card { border: 1px solid; } } and the card on your page gets no border. Why?
Show answer
Inner selectors match only descendants of a scope root, never the root itself. The rule would only affect a .card nested inside another .card. To style the root itself, use :scope, covered in the next chapter.