Sub topic 2
Your first @scope rule
This chapter assumes you know HTML structure and the DOM tree, CSS selectors and combinators, and the basics of the cascade. Later chapters cover specificity and nesting in more detail.
The shape of the rule
@scope is an at-rule that limits the style rules inside it to part of the DOM tree. You name the top of that part in parentheses. That element is the scope root.
@scope (.card) {
img {
width: 100%;
}
a {
color: rebeccapurple;
}
}The rule has two parts:
- The prelude is
(.card). It holds a selector that picks the scope root. The parentheses are required. - The body is a list of ordinary style rules. Each one only applies inside a scope.
The body selectors are relative to the root. The browser reads img as "an img that is a descendant of a .card". You don't write .card img yourself.
Seeing it work
Take this markup and the stylesheet above.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>First @scope rule</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p><a href="#">Normal link</a></p>
<div class="card">
<img src="a.jpg" alt="">
<a href="#">Card link</a>
</div>
</body>
</html>The card's link is rebeccapurple and its image fills the card width. The link outside the card keeps its default color, and any image outside the card keeps its normal size. The rules never reach outside the .card subtree.
How matching works
An element is in scope when it is a descendant of a scope root. The browser finds the roots by matching the prelude selector against the whole document. The result can be many elements.
- If the page has ten
.cardelements, each one is a scope root, and the body rules apply inside all ten. - The prelude can be a selector list, such as
@scope (.card, .panel). Each match is a root. - The root itself is not matched by plain body selectors. A rule
imginside@scope (img)would style no element, because an element is not its own descendant. Styling the root takes the:scopepseudo-class, which a later chapter covers.
Compared with a normal rule
The scoped rule above looks like this when written without @scope:
.card img {
width: 100%;
}
.card a {
color: rebeccapurple;
}The match results are the same for this markup. The differences are in how the cascade treats the rules, and a later chapter covers those. For now, the gain is that you write the root once. Moving or renaming the component means changing one line.
Common errors
- Missing parentheses.
@scope .card { ... }is invalid, and the browser drops the whole block. - Repeating the root in the body.
@scope (.card) { .card a { ... } }looks for a.cardlink inside another.card. This is almost never what you want, so writea. - Expecting isolation.
@scopelimits where your rules apply. Rules from outside can still style elements inside the card, and inherited properties such ascolorstill flow in. A later chapter covers this limit. - Testing in an old browser. If
@scopeis unsupported, the browser ignores the whole block and nothing is styled. Check the support table before you ship.
Try it
Save the HTML as index.html and the CSS as styles.css in one folder. Add a second .card and a link outside any card, then open the page. Both cards should get the scoped styles and the outside link should not.
Exercise
In @scope (.card) { a { color: red; } }, which links turn red, and why does a link in <p> outside any .card stay unchanged?
Show answer
Every a that is a descendant of any .card element turns red. A link in a <p> outside all .card elements is not a descendant of a scope root, so the rule never matches it.
Exercise
A developer writes @scope (.card) { .card { padding: 1rem; } } to pad the card itself, and nothing changes. Why?
Show answer
The body selector is relative to the root and matches descendants only, so .card here means a .card nested inside another .card. The root is not its own descendant. Styling the root needs :scope, which a later chapter covers.