Sub topic 9
Combining @scope with CSS nesting
@scope limits where a rule applies. CSS nesting lets you write a rule inside another rule. The two features work together, but & means different things at different depths, and that trips people up. This chapter covers how nested rules resolve inside @scope, what & refers to at each level, and how that affects specificity.
Prerequisites
This chapter assumes you know the following:
- How
@scope (root) to (limit)selects a region of the DOM. - What
:scopematches inside a scope. - Basic CSS nesting, including the
&nesting selector. - How specificity and order of appearance decide the cascade.
How selectors resolve inside @scope
A selector written directly inside @scope is relative to the scope root. The browser treats it as if it began with :where(:scope) and a descendant combinator. The :where() wrapper contributes zero specificity. That is why the scope root adds nothing to a rule, as you saw in the specificity chapter.
Nesting builds on this. A rule nested inside a scoped rule is also scoped, because its parent already is. The nested rule only matches elements that sit inside the scope and outside any scope limit.
| What you write inside `@scope (.card)` | What it means | Specificity |
|---|---|---|
| `img` | `:where(:scope) img` | (0,0,1) |
| `.title` | `:where(:scope) .title` | (0,1,0) |
| `.title { &:hover { } }` | `:where(:scope) .title:hover` | (0,2,0) |
| `& > img` | `:where(:scope) > img` | (0,0,1) |
| `:scope > img` | `:scope > img` | (0,1,1) |
The last two rows are the main reason this chapter exists. The sections below explain them.
Nested rules inside scoped rules
Here is a complete page you can save and open. The CSS uses @scope and nesting together.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>@scope with nesting</title>
<style>
a { color: black; }
@scope (.card) {
.title {
font-weight: 700;
font-size: 1.25rem;
&:hover {
text-decoration: underline;
}
@media (width >= 40rem) {
font-size: 1.75rem;
}
}
a {
color: rebeccapurple;
&:visited {
color: indigo;
}
}
}
</style>
</head>
<body>
<article class="card">
<h2 class="title">Scoped and nested</h2>
<p>Read the <a href="#docs">docs</a>.</p>
</article>
<p>Outside the card: <a href="#other">this link stays black</a>.</p>
</body>
</html>Three things happen here:
.titlematches only inside.card. The globala { color: black; }rule still applies outside the card.&:hoverattaches the pseudo-class to.title. Without&,:hoveralone would be read as a descendant of.title, which is a different meaning. Always use&for pseudo-classes and compound selectors.- The
@mediablock inside.titlecontains declarations directly. The browser applies them to the same.titleselector when the condition is true.
Relative selectors nest too
You can nest descendant and combinator selectors without a leading &. Modern browsers accept a bare type selector such as span. For combinators, start with the combinator:
@scope (.card) {
.title {
span {
color: crimson;
}
+ p {
margin-top: 0;
}
}
}The first nested rule means .title span. The second means .title + p. Both are still limited to elements inside .card.
The & selector at the top of @scope
Inside a nested rule, & stands for the parent rule's selector. A style rule placed directly in the @scope block has no parent style rule. In that position, & stands for the scope root itself, as :where(:scope).
@scope (.card) {
& {
border: 1px solid #ccc;
padding: 1rem;
}
& > img {
width: 100%;
}
}The first rule styles the .card element. The second styles images that are direct children of .card. It does the same job as the :scope version from the earlier chapter. The difference is specificity, covered next.
Meaning changes with depth
The same character refers to different things at different levels. Compare these two rules:
@scope (.card) {
& > img { /* & is the .card root */
border-radius: 4px;
}
.title {
& > img { /* & is .title, not .card */
border-radius: 50%;
}
}
}The first rule styles images that are direct children of .card. The second styles images that are direct children of .title. Read & as "the selector of the nearest enclosing style rule, or the scope root if there is none."
& versus :scope: a specificity difference
:scope is a pseudo-class, so it adds (0,1,0) to a selector's specificity. The top-level & inside @scope is :where(:scope), which adds nothing. That makes & > img weaker than :scope > img.
@scope (.card) {
:scope > img {
border: 2px solid red; /* (0,1,1) */
}
& > img {
border: 2px solid blue; /* (0,0,1) */
}
}Both rules match the same images. The :scope rule has higher specificity, so the border is red, even though the & rule comes later in the source.
This gives you a practical choice:
- Use
&when you want the rule to stay easy to override. It keeps the scoped rule at the weight of its plain selectors. - Use
:scopewhen you want the root to carry weight. This is rarely needed. If you find yourself reaching for it to win a conflict, consider whether scope proximity or source order is a cleaner fix.
Scope limits and nested rules
Nested rules respect the scope limit. This works the same as in the donut-scope chapter. Excluded elements are never matched, no matter how deeply the rule is nested.
<div class="card">
<img src="a.jpg" alt="">
<div class="card-content">
<img src="b.jpg" alt="">
</div>
</div>@scope (.card) to (.card-content) {
img {
border-radius: 50%;
&:hover {
outline: 2px solid gold;
}
}
}Only the first image gets rounded corners and the hover outline. The second image is inside .card-content, which is the scope limit, so neither the img rule nor its nested &:hover rule reaches it.
Both .card and .card-content are selectors the browser tests against the DOM. The limit is not a stylesheet boundary. If you later add a second .card-content somewhere else in the card, it is also excluded.
Scope proximity applies to nested rules
When two scoped rules have equal specificity, the one whose scope root is closer to the element wins. This applies to nested rules in the same way. Specificity is computed for the whole selector, including the zero-weight :where(:scope), and then proximity is checked before source order.
@scope (.dark) {
a {
color: plum;
&:hover { color: white; }
}
}
@scope (.light) {
a {
color: darkmagenta;
&:hover { color: black; }
}
}For <div class="light"><div class="dark"><a href="#">Link</a></div></div>, the link is plum, and its hover color is white. The .dark root is closer to the link than .light, so both nested rules from the .dark block win, even though the .light block comes later.
Common errors
Most problems come from reading & the wrong way or forgetting that nesting adds weight.
- Expecting
&to always mean the scope root. At the top of the@scopeblock it does. Inside.title { }it means.title. If a rule matches too little, check what&refers to at that depth. - Omitting
&before a pseudo-class. Writing.title { :hover { } }produces.title :hover, which matches hovered descendants of.title. Write&:hoverto style.titleitself. - Mixing
:scopeand&and getting unexpected overrides. The two differ by (0,1,0). A rule using:scopecan beat a later rule using&. - Accidental weight from deep nesting. Each class or pseudo-class in a nested chain adds specificity.
.title { &.featured { &:hover { } } }has specificity (0,3,0) even though the scope root contributes nothing. Keep nesting shallow. - Assuming nested rules escape the limit. They cannot. A nested rule only ever matches inside the scope and outside the limit.
- Putting a rule meant for the root inside a child rule. Style the root with a top-level
&or:scope. A.cardrule written inside.titlewould never match the card.
Choosing a style
Both of these produce the same result for matching elements:
/* Flat */
@scope (.card) {
.title { font-weight: 700; }
.title:hover { text-decoration: underline; }
}
/* Nested */
@scope (.card) {
.title {
font-weight: 700;
&:hover { text-decoration: underline; }
}
}Nesting groups the states and media conditions of one element in one place. Flat rules are easier to scan when each rule is short. Pick one convention per codebase, and keep nesting to two or three levels so the final specificity stays predictable.
Exercise
Inside @scope (.card), you write .title { & > img { border: 1px solid; } }. Which images does this rule match: direct children of .card, or direct children of .title?
Show answer
Direct children of .title. Inside the .title rule, & stands for the parent selector .title, not for the scope root. Only a & placed directly in the @scope block means the root. The rule is also still limited to elements inside .card.
Exercise
A .card contains an image. These two rules appear inside @scope (.card), in this order: :scope > img { border-color: red; } and then & > img { border-color: blue; }. What color is the border, and why?
Show answer
Red. :scope is a pseudo-class and adds (0,1,0), so the first rule has specificity (0,1,1). The top-level & is :where(:scope) and adds nothing, so the second rule has specificity (0,0,1). Higher specificity wins before source order is considered, so the later & rule loses.
Exercise
You want the hover style text-decoration: underline on a .title element, written as a nested rule inside .title { }. Why does .title { :hover { text-decoration: underline; } } fail to do that, and how do you fix it?
Show answer
Without &, :hover is read as a descendant selector, so the rule means .title :hover and matches hovered elements inside the title. Write &:hover so the pseudo-class attaches to .title itself.