Sub topic 4
Styling the root with :scope
This chapter assumes you know CSS selectors and combinators, the cascade and specificity, and basic HTML structure. Earlier chapters showed that @scope (.card) limits rules to the part of the DOM under a .card element. Here you will learn how to style the .card element itself.
The root is not matched by plain selectors
Selectors inside an @scope block are matched against the scope root's descendants. The root is not one of them. Take this rule:
@scope (.card) {
.card {
border: 1px solid #ccc;
}
}This rule does not style the card. It only matches a .card nested somewhere inside another .card. To target the root, you need the :scope pseudo-class.
What :scope matches
Inside an @scope rule, :scope is a pseudo-class that matches the scope root. Each matched root element is its own scope, so :scope refers to whichever root the current rule applies to.
@scope (.card) {
:scope {
border: 1px solid #ccc;
padding: 1rem;
}
img {
width: 100%;
}
a {
color: rebeccapurple;
}
}Here :scope styles the .card element. The img and a rules style only elements inside it. The rules behave as if each selector were written as :scope img and :scope a.
Combining :scope with other selectors
:scope works like any other selector. You can attach pseudo-classes, attribute selectors and classes to it, or use it with combinators.
@scope (.card) {
:scope:hover {
box-shadow: 0 2px 8px rgb(0 0 0 / 0.2);
}
:scope > img {
display: block;
}
}The first rule styles the card on hover. The second matches only images that are direct children of the card. Images nested deeper are not affected.
Specificity of :scope
:scope is a pseudo-class, so it adds the specificity of one class selector (0,1,0). A rule like :scope { ... } therefore beats a rule like img { ... } when both target the same element. They never do, because they target different elements. The comparison matters when an outside rule also styles the root:
.card {
padding: 2rem;
}
@scope (.card) {
:scope {
padding: 1rem;
}
}Both rules have specificity (0,1,0) and both match the same element. The later one wins by order of appearance, so the padding is 1rem. If you swap the two blocks, the padding becomes 2rem. The scope's effect on specificity is covered in a later chapter.
Common mistakes
- Writing the root selector again inside the block, as in
@scope (.card) { .card { ... } }. This matches nested cards, not the root. Use:scope. - Expecting
:scopeto work in a rule outside@scope. There it matcheshtml, so a rule like:scope { color: red; }changes the whole page. - Assuming a style on
:scopestays inside the card. Properties such ascolorandfont-familyare inherited, so they pass to descendants, including any excluded by a scope limit. The scope limits chapter returns to this.
Worked example
Here is the markup for a card, followed by a scoped stylesheet that styles both the card and its content.
<div class="card">
<h2 class="title">Trail map</h2>
<img src="map.jpg" alt="Map of the trail">
<a href="/trails">All trails</a>
</div>
<a href="/home">Home</a>@scope (.card) {
:scope {
border: 1px solid #ccc;
padding: 1rem;
}
.title {
margin: 0 0 0.5rem;
}
img {
width: 100%;
}
a {
color: rebeccapurple;
}
}The card gets a border and padding. The title, image and "All trails" link are styled inside it. The "Home" link sits outside .card, so it keeps its default color.
Exercise
Inside @scope (.panel), you want the .panel element itself to get a red border when hovered. Which selector do you write, and why does .panel:hover not work?
Show answer
Write :scope:hover { border-color: red; }. Selectors inside @scope match descendants of the root, so .panel:hover would only match a .panel nested inside another .panel. :scope matches the root itself.
Exercise
What does :scope { color: red; } match when it appears in a stylesheet outside any @scope rule?
Show answer
It matches the document root, the html element, so the text color of the whole page changes (through inheritance).