Sub topic 8
Inline @scope in a Style Element
This chapter covers @scope written without a prelude, the parenthesized selector after @scope, inside a <style> element. I assume you can already write an @scope (.card) rule with a scope root and use :scope to style that root. You should also know the cascade basics (specificity, then scope proximity, then order of appearance) and how CSS nesting works.
What a missing prelude means
In the earlier chapters, every @scope rule named its scope root explicitly:
@scope (.card) {
img { width: 100%; }
}The selector in parentheses is the prelude. It tells the browser which elements are scope roots. When you write @scope with no prelude and place the rule in a <style> element, the browser picks the root for you. The scope root is the parent element of that <style> element.
The root is determined by where the <style> element sits in the document. You don't write a selector for it. Here is the example from the topic plan:
<section class="promo">
<style>
@scope {
h2 { color: crimson; }
:scope { background: #fff7f0; }
}
</style>
<h2>Sale ends soon</h2>
</section>
<h2>Unaffected heading</h2>The <style> element is a child of section.promo, so section.promo is the scope root. The h2 rule matches only h2 elements that are descendants of that section. The heading after the section is outside the scope and keeps its normal color. The :scope rule gives the section itself the pale background.
How the browser finds the root
The browser looks at the element that owns the stylesheet, which is the <style> element, and takes its parent. That parent is the scope root. Every other part of the rule works as in an explicit scope.
A selector such as h2 inside the block is relative to the root. It is treated as if you had written :where(:scope) h2. The :where() wrapper contributes no specificity. That detail matters later in this chapter.
A worked example with two instances
A common use for inline @scope is a self-contained snippet of markup that carries its own styles. Save this file and open it in a browser that supports @scope:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inline @scope demo</title>
<style>
a { color: blue; }
</style>
</head>
<body>
<p><a href="#one">Global link</a></p>
<article class="note">
<style>
@scope {
:scope {
border-left: 4px solid teal;
padding: 0.5rem 1rem;
}
a { color: teal; }
p { margin: 0; }
}
</style>
<p>A note with a <a href="#two">scoped link</a>.</p>
</article>
<p><a href="#three">Another global link</a></p>
</body>
</html>The result:
- The first and third links stay blue. They are outside
article.note. - The link inside the article is teal. The inline rule
ahas specificity(0,0,1), the same as the globalarule, and it comes later in the document, so it wins. - The paragraph margin reset applies only to paragraphs inside the article. The two outer paragraphs are untouched.
- The article gets the border and padding through
:scope.
If you copy the <article> block, the copy gets its own <style> element and its own root. Each copy styles only its own subtree. You never need to rename a class or add a unique ID to make the root selector unique.
Combining an implicit root with a scope limit
The prelude has two optional parts: the scope start in parentheses and the scope limit introduced by to. You can drop the start and keep the limit. The root is still the parent of the <style> element, and the limit still excludes a subtree, which gives a donut scope:
<div class="gallery">
<style>
@scope to (.caption) {
img { border-radius: 50%; }
}
</style>
<img src="a.jpg" alt="">
<figure class="caption">
<img src="b.jpg" alt="">
</figure>
</div>The first image is inside the scope and gets rounded corners. The second image is inside .caption, which is the scope limit, so the rule does not reach it. The limit selector is matched against descendants of the root, so it needs no knowledge of what the root is.
Placement matters
Because the root comes from position, moving the <style> element changes what the rule styles. The table lists common placements.
| Where the `<style>` element sits | Scope root | Effect |
|---|---|---|
| Directly inside a component wrapper | The wrapper | The intended case. Styles apply to that component only. |
| Directly inside `<body>` | `body` | Descendant selectors can match the whole page. This is almost never what you want. |
| Inside `<head>` | `head` | Nothing visible is a descendant of `head`, so ordinary content is not styled. |
| Inside a nested element deeper in the wrapper | That nested element | Only that sub-part is styled. Placing the style higher or lower silently changes the scope. |
Two practical habits help here:
- Put the
<style>element as the first child of the element you want to scope, so a reader can see the root at a glance. - Don't wrap the
<style>in another element just for layout reasons. That would change the root.
Specificity and proximity with inline scopes
An inline @scope follows the same cascade rules as an explicit one. The implicit root adds no specificity to the selectors inside it. Consider this page:
<style>
.box a { color: blue; }
</style>
<div class="box">
<style>
@scope {
a { color: green; }
}
</style>
<a href="#">Link</a>
</div>The global rule .box a has specificity (0,1,1). The inline rule a has specificity (0,0,1), because the implicit :where(:scope) contributes zero. The link stays blue. Scope proximity does not rescue the inline rule, because specificity is compared first and proximity only breaks ties.
To win, make the selector more specific inside the scope, for example a[href] or .link. Or remove the competing global selector. Avoid !important as a first choice.
Proximity is useful when you nest components that both carry inline styles:
<div class="outer">
<style>
@scope {
a { color: darkmagenta; }
}
</style>
<div class="inner">
<style>
@scope {
a { color: plum; }
}
</style>
<a href="#">Nested link</a>
</div>
</div>Both rules match the link with equal specificity. The .inner root is closer to the link than the .outer root, so the plum rule wins. Source order is not consulted, so swapping the two blocks would not change the result.
Inline versus linked stylesheets
Inline @scope without a prelude only makes sense inside a <style> element. The browser derives the root from the element that owns the stylesheet. In a separate .css file loaded with <link>, that owner is the <link> element, which usually sits in <head>. You would not get a useful component root there. In a file, write the prelude explicitly:
@scope (.promo) {
h2 { color: crimson; }
}Choose between the two forms using these criteria:
- Use the inline form when the markup and its styles ship together, for example in a server-rendered partial, a CMS block, or an email-like snippet that is copied around.
- Use the explicit form for shared stylesheets, because it keeps the root visible in the CSS and survives being moved between files.
- Consider the cost of repetition. A component rendered 200 times with an inline
<style>repeats the CSS 200 times in the document. Each copy is parsed and increases page size. For repeated components, one linked stylesheet with@scope (.card)is usually better.
Other things to know
- The HTML specification only allows
<style>in head contexts. A<style>inside<div>works in all browsers, but an HTML validator may flag it. If validation matters to you, use an explicit@scope (.root)in a normal stylesheet. - Inline scoping does not stop inheritance. Properties such as
colorandfont-familystill flow into the scope from outside. Elements inside the scope still match rules from outside it, such as.box aabove. - Rules inside an inline
@scopecan use nesting, including&, as in explicit scopes. - Browsers without
@scopesupport drop the entire at-rule, so the inline styles do not apply at all. If a fallback matters, write base styles outside the@scopeblock and use@scopeonly for refinements. You can also guard a block with@supports at-rule(@scope)where that syntax is supported, but check current support before you rely on it.
Exercise
A page has <div class="widget"><div class="body"><style>@scope { a { color: red; } }</style><a href="#">Go</a></div></div>. Which element is the scope root, and does the link turn red?
Show answer
The scope root is div.body, the parent of the <style> element, not div.widget. The link is a descendant of div.body, so the rule matches it and it turns red, unless a more specific rule from outside the scope, such as .widget a, sets a different color.
Exercise
You want an inline scope that rounds every img in a .profile block except images inside a nested .ad element. Write the <style> element and say where it must be placed.
Show answer
Place <style>@scope to (.ad) { img { border-radius: 50%; } }</style> as a direct child of the .profile element. The omitted start selector means the root is its parent, .profile, and the limit .ad excludes images inside the ad subtree.
Exercise
A global rule main a { color: blue; } and an inline @scope { a { color: green; } } both match the same link, and the inline rule comes later. Which color does the link get, and why?
Show answer
Blue. The global rule has specificity (0,0,2). The inline rule has (0,0,1), because the implicit root adds nothing. Specificity is compared before scope proximity and source order, so the global rule wins.