Sub topic 5
Scope limits and donut scopes
This chapter assumes you know HTML structure and the DOM tree, CSS selectors and combinators, and the cascade. It also assumes you have written a basic @scope (.card) rule with a scope root. Here you add the second boundary, the scope limit.
The problem a root alone cannot solve
A scope root says where a scope starts. Everything below the root is in scope, however deep. That is a problem when a component contains another component, or a slot for content you do not control.
Take a card with a nested content area:
<div class="card">
<img src="a.jpg" alt="">
<div class="card-content">
<img src="b.jpg" alt="">
</div>
</div>If you write @scope (.card) { img { ... } }, both images match. You want to style only the card's own image and leave whatever sits inside .card-content alone.
Adding a scope limit
A scope limit is a second selector, written after to in the @scope prelude (the part in parentheses before the block). It marks where the scope stops.
@scope (.card) to (.card-content) {
img {
border-radius: 50%;
}
}The syntax is @scope (root) to (limit). Here the first image is inside .card and outside .card-content, so it gets rounded. The second image is inside .card-content, so the rule does not apply to it.
Why it is called a donut
The scope has a hole. The root and everything between it and the limit are in the scope. The limit element and everything under it are outside. The shape is like a donut: styled ring, untouched center.
Rules for the limit
- The limit is exclusive. The element that matches the limit is itself outside the scope, along with all its descendants.
- The limit only counts while it is inside the root. A
.card-contentelement elsewhere on the page does nothing, because there is no scope to cut. - The limit can use
:scopeto refer to the root, for exampleto (:scope > .nested). That excludes only direct children of the root that have the classnested. - A scope can have several limits. Separate them with commas:
to (.card-content, .card-footer).
@scope (.card) to (:scope > .card-content, .widget) {
img {
border-radius: 50%;
}
}Every selector inside the block is also limited, so a rule like a or img cannot reach into the excluded subtree.
Common errors
A limit that does not match what you expect usually has one of these causes:
- The limit matches the root. If the same element is both root and limit, nothing ends up in scope. Check that the limit selector is not too broad, such as
div. - The limit is not a descendant of the root. It is ignored in that case, so the whole subtree stays in scope.
- You expected the limit to be styled. Because the limit is outside the scope, a rule like
@scope (.card) to (.card-content) { :scope > div { ... } }still matches by its own relation to the root. Do not rely on the limit itself being styled by the scoped block. Style it from the inner component's own@scope. - Outside rules still apply. The limit stops this
@scopeblock only. Global rules, and styles inherited from the card such ascolorandfont-family, still reach the excluded content.
A practical pattern
Donut scopes work well for a component that accepts user content. The parent component styles its own structure. The child area is excluded, so the parent's img, a, or p rules do not leak into the embedded content.
@scope (.tabs) to (.tab-panel) {
a {
color: rebeccapurple;
}
}Links in the tab bar are styled. Links inside .tab-panel keep the page's usual link style.
Exercise
Given @scope (.card) to (.card-content) { img { border: 2px solid red; } }, which of the three images gets the border? Image 1 is a direct child of .card. Image 2 is a direct child of .card-content. Image 3 is inside a div that is a child of .card, and that div is not .card-content.
Show answer
Images 1 and 3 get the border. Both are inside .card and neither is inside .card-content. Image 2 is under the scope limit, so it is excluded.