Sub topic 9
Combining @scope with CSS nesting
This chapter assumes you know the basics of HTML and the DOM, CSS selectors and combinators, the cascade and specificity, and CSS nesting syntax. It builds on the earlier chapters about the scope root, :scope, and specificity inside @scope.
Why combine the two
@scope limits a group of rules to a region of the DOM. CSS nesting lets you write a rule inside another rule, so related selectors stay together. The two features work together. You can nest rules inside the @scope block, and you can nest rules inside the style rules within it.
Rules inside @scope are already relative
Inside @scope, a selector such as .title is matched only against elements inside the scope root. You don't write .card .title. The scope supplies that part.
@scope (.card) {
.title {
font-weight: 700;
&:hover {
text-decoration: underline;
}
}
}The .title rule applies to .title elements inside .card. The nested rule &:hover uses &, which stands for the enclosing .title rule. It matches a .title element that is hovered, still inside .card.
What & means in each position
The meaning of & depends on where you write it.
| Where `&` appears | What it refers to |
|---|---|
| In a style rule nested in another style rule | The selector of the parent rule, such as `.title` |
| In a style rule directly inside `@scope` | The scope root, as `:scope` does |
| In a nested rule inside a rule inside `@scope` | The parent rule, scoped to the root |
The second row matters. At the top level of an @scope block, & acts like :scope. Both of these rules style the .card element itself:
@scope (.card) {
:scope {
padding: 1rem;
}
& {
border: 1px solid #ccc;
}
}Use :scope when you want the reader to see at a glance that the rule targets the root. Use & when you are writing nested rules and want to stay consistent.
Nesting a relative selector
Nested rules can begin with a combinator, with or without &. This lets you describe elements next to or inside the current rule's match.
@scope (.card) {
.title {
font-size: 1.25rem;
& + p {
margin-top: 0.25rem;
}
.icon {
width: 1em;
}
}
}The rule & + p matches a paragraph directly after a .title. The rule .icon matches an .icon inside a .title. Both are still limited to the .card scope.
Nesting an @scope rule inside a style rule
You can also put @scope inside a style rule. The scope root is then the element matched by the parent rule.
.card {
@scope (.card-body) {
p {
line-height: 1.6;
}
}
}Here the scope start is .card-body, evaluated relative to .card. Use this form sparingly. It is harder to read than a top-level @scope, and the plain version nearly always says the same thing more clearly.
Specificity of nested rules
Recall that the scope root adds no specificity to rules inside @scope. Nesting still follows the usual nesting rules. The specificity of a nested rule that uses & includes the specificity of the parent selector. So .title { &:hover { } } has the specificity of .title:hover, which is a class plus a pseudo-class.
A bare & at the top level of @scope is different. It refers to the scope root, and it does not add the root's specificity as it would outside @scope. When a rule loses a conflict you didn't expect, check where & sits before you look elsewhere.
Common errors
- Writing the root in every selector.
@scope (.card) { .card .title { } }looks for a.cardinside.card. Drop the prefix and write.title. - Expecting
&to mean the same thing everywhere. At the top of@scopeit is the scope root. In a nested rule it is the parent rule. - Nesting
@scopeinside style rules for no reason. It adds a layer of meaning without adding capability. - Forgetting the scope limit still applies. In
@scope (.card) to (.card-content), nested rules still skip the subtree under.card-content.
Exercise
In the following code, which element does the rule with & + p style, and which does the rule with & on its own style? @scope (.card) { & { border: 1px solid; } .title { & + p { margin: 0; } } }
Show answer
The lone & at the top of @scope refers to the scope root, so it styles the .card element itself, giving it the border. The & + p rule is nested in .title, so & is the .title rule. It styles a p that directly follows a .title, and only when that p is inside .card.
Exercise
Rewrite @scope (.card) { .card .title { color: navy; } } so it works as intended, and explain what was wrong.
Show answer
Write @scope (.card) { .title { color: navy; } }. The original selector .card .title searches for a .card inside the scope root, which is a .card inside another .card. Inside @scope, selectors are already relative to the root.