Sub topic 5
Scope limits and donut scopes
This chapter assumes you know basic HTML and the DOM tree, CSS selectors and combinators, and the cascade (specificity and order of appearance). You should also have written a basic @scope (.root) { ... } rule and used :scope to style the root. Scope proximity and nesting come in later chapters, so they get only a mention here.
What a scope limit is
A plain @scope (.card) rule covers the whole subtree under every .card. Sometimes that is too much. A card might contain a slot where other components or user-generated content appear, and you don't want the card's img or a rules to reach into it.
A scope limit gives the scope a lower boundary. You add it with to (...) after the root:
@scope (.card) to (.card-content) {
/* rules here */
}The region that results has an outer edge (the root) and an inner edge (the limit). Because the area between the edges is shaped like a ring, with a hole in the middle, people call it a donut scope.
Syntax and matching rules
The general form is:
@scope (<scope-start>) to (<scope-end>) {
<rules>
}An element is in scope when all of these are true:
- It is a descendant of an element matching the scope start (the root).
- It is not a scope limit, meaning it does not match the scope end.
- It is not a descendant of a scope limit.
The second point is easy to miss. The limit element is outside the scope, not just its children. A rule inside the scope can never style the element that matches the limit.
The limit selector is evaluated relative to the root. You can use :scope in it to refer to the matched root, and you can use combinators. Both the root and the limit accept selector lists, so you can name several limits:
@scope (.card) to (.card-content, .ad-slot) {
img {
border-radius: 50%;
}
}Here everything inside .card-content and .ad-slot is excluded, along with those two elements themselves.
A worked donut scope
This is the markup from the example in the tutorial plan:
<div class="card">
<img src="a.jpg" alt="">
<div class="card-content">
<img src="b.jpg" alt="">
</div>
</div>The stylesheet is:
@scope (.card) to (.card-content) {
img {
border-radius: 50%;
}
}The first img is a descendant of .card and is not inside .card-content, so it gets rounded. The second img is a descendant of .card-content, which is the limit, so it falls in the hole and the rule does not apply.
To try it, save this as one file and open it in a browser that supports @scope. The two images are replaced by colored boxes, so no image files are needed:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Donut scope demo</title>
<style>
@scope (.card) to (.card-content) {
img {
border-radius: 50%;
}
}
img {
width: 80px;
height: 80px;
background: steelblue;
display: block;
margin: 8px;
}
.card-content {
border: 1px dashed gray;
padding: 8px;
}
</style>
</head>
<body>
<div class="card">
<img alt="">
<div class="card-content">
<img alt="">
</div>
</div>
</body>
</html>The first square renders as a circle. The second stays square. The bare img rule at the bottom applies to both images, so it supplies the size and color. The scoped rule adds the rounding only where the scope allows.
What each element gets
The table lists each element in the example and how the donut scope treats it.
| Element | Inside the root? | Matches the limit? | In scope? |
|---|---|---|---|
| `div.card` | It is the root itself | No | Only through `:scope` |
| First `img` | Yes | No | Yes |
| `div.card-content` | Yes | Yes | No |
| Second `img` | Yes | Inside a limit | No |
The root is a special case. A bare selector such as img inside @scope behaves like :scope img, so it can match only descendants of the root. The root itself is reachable only through :scope (or &, which you will meet in the nesting chapter).
Using :scope inside the limit
Because the limit selector is evaluated relative to the root, :scope works there too. This is useful for components that can nest inside themselves. A tab set that contains another tab set is a common case:
@scope (.tabs) to (:scope .tabs) {
[role="tab"] {
padding: 0.5rem 1rem;
}
}The limit :scope .tabs means "any .tabs element that is a descendant of the current root". The outer tab set styles its own tabs. The inner .tabs is a limit, so the outer rule does not reach it.
The inner .tabs is also a scope root in its own right, because it matches the root selector .tabs. It gets the same rules as its own scope. Each tab set is styled once, by its own scope, with no double application.
Written as to (.tabs), the limit would match any .tabs, including the root. Spelling out :scope .tabs states the intent exactly, so prefer it for self-nesting components.
Styling the limit element itself
A common surprise is that the limit element cannot be styled from inside the same @scope:
@scope (.card) to (.card-content) {
.card-content {
padding: 1rem; /* never applies */
}
}.card-content is the limit, so it is outside the scope, and a selector whose subject is out of scope cannot match. Style the boundary element in one of these ways:
- Write a normal rule outside any
@scope. - Put it in a separate
@scopewhose root is the boundary element, for example@scope (.card-content) { :scope { padding: 1rem; } }. - Make the limit one level deeper, so that the boundary element stays inside the first scope.
Edge cases
Limits that are not descendants of the root
A limit only matters when it appears inside a root. If nothing matching the limit exists under the root, the scope behaves exactly like a scope without a limit. A limit never makes the scope larger and never affects elements outside the root.
Several roots on one page
If the page has many .card elements, each one defines its own region. An element is in scope when at least one root contains it and the limit does not exclude it. Nested roots can overlap. An inner root that matches the root selector gets its own scope as well, so content inside a nested card can end up styled by two scopes. Scope proximity decides conflicts between them. A later chapter covers that.
Limits do not block inheritance
A scope limit restricts which elements a selector can match. It does not stop property values from flowing down the tree. Consider:
@scope (.card) to (.card-content) {
:scope {
color: darkgreen;
}
}The text inside .card-content still renders dark green, because color is an inherited property and .card-content inherits it from .card. The limit only prevented rules whose subject is inside the hole from matching. To stop inheritance, set the property explicitly on the hole's root, for example color: initial or color: inherit from a different source.
Limits do not block outside rules
Global rules and rules from other scopes still apply everywhere. The limit controls only what this @scope block can select. It does not protect the hole from a global img { border: 2px solid red; }, and it does not protect the card from rules elsewhere.
Combining a limit with other selectors
Selectors inside a scoped block may combine in complex ways, such as img + p or :is(h1, h2). The test for scope applies to the subject, the element the selector finally selects. A selector like .card-content img has img as its subject, and that img is inside the hole, so the selector never matches. You cannot reach into the hole by naming the limit inside the selector.
Choosing roots and limits well
These guidelines work in most projects:
- Use one limit per slot. Name content slots and embedded widgets with a class such as
.card-contentor.embed. Use a selector list to give the limit several values. - Prefer a limit over adding more specific overrides inside the hole. A limit removes the problem. Overrides only fight it.
- Keep limits short and local. A limit like
:scope > .slotis cheaper to reason about than a long selector. - Do not use a limit to hide content from the cascade. For true encapsulation you need Shadow DOM, which a later chapter compares with
@scope.
Exercise
Given @scope (.panel) to (.panel-body) { p { color: teal; } } and the markup <div class="panel"><p>One</p><div class="panel-body"><p>Two</p></div></div>, which paragraphs are teal? Can a rule inside this same @scope block set the padding of .panel-body?
Show answer
Only "One" is teal. The second paragraph is inside .panel-body, which is the scope limit, so it is in the hole. A rule inside this block cannot style .panel-body, because the limit element itself is excluded from the scope. Set its padding in a rule outside the scope or in a separate @scope (.panel-body) block.
Exercise
Write an @scope rule for a .comment component that can contain replies, which are also .comment elements. The rule must make each comment's .author bold without a comment's rule reaching into its replies.
Show answer
Use the limit :scope .comment.
@scope (.comment) to (:scope .comment) {
.author {
font-weight: 700;
}
}
The outer comment styles its own .author. A nested .comment is a limit for the outer scope, so the outer rule skips it. The nested comment is also a root, so its own .author is bolded by its own scope.
Exercise
A card uses @scope (.card) to (.card-content) { :scope { color: darkgreen; } }. Why is the text inside .card-content still dark green?
Show answer
The limit controls selector matching only. color is an inherited property, so .card-content inherits the value from .card. The scope limit does not stop inheritance.