Sub topic 8
Inline @scope in a style element
This chapter assumes you know how to write @scope (root) { ... } with a scope root, how :scope refers to that root, and how CSS nesting works. Here you will use @scope with no root selector, placed in a <style> element inside the markup it styles.
The idea: the parent is the root
Normally you name the scope root in the prelude, as in @scope (.card). The prelude is the part in parentheses after @scope. If you leave it out and put the @scope rule in a <style> element, the browser uses the parent element of that <style> as the scope root. This is called an implicit scope.
<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's parent is section.promo, so that element is the scope root. The rules behave as follows:
h2matches onlyh2elements inside.promo. The secondh2is outside the root and keeps its normal color.:scopematchessection.promoitself, so the background applies to the section.- No class name appears in the CSS. The position of the
<style>element decides the scope.
Why use it
Inline scoping suits self-contained pieces of markup, such as a component rendered by a template or a snippet pasted into a CMS. The styles travel with the markup, and you do not need to invent a unique class name to attach them to.
It also works when the same component appears several times. Each copy contains its own <style> element, and each one scopes to its own parent.
<div class="note" data-kind="info">
<style>
@scope {
:scope { border-left: 4px solid steelblue; padding: 0.5rem 1rem; }
p { margin: 0; }
}
</style>
<p>Backups run nightly.</p>
</div>
<div class="note" data-kind="warn">
<style>
@scope {
:scope { border-left: 4px solid darkorange; padding: 0.5rem 1rem; }
p { margin: 0; }
}
</style>
<p>Disk is almost full.</p>
</div>Each :scope rule affects only its own .note, so the two border colors do not collide.
Where you place the style element matters
The scope root is whatever element contains the <style>, not the element you intended. Two mistakes are common:
- Style in the head. The parent is
<head>, so the root ishead. Its descendants are only metadata elements, so theh2rule matches nothing. Use a prelude such as@scope (.promo)in a stylesheet, or move the<style>into the markup. - Style placed next to the target. If you put the
<style>as a sibling after the.promosection instead of inside it, the root becomes the shared parent, which is often much larger than you meant.
Combining with a scope limit
You can still add a limit to an implicit scope, which gives you a donut scope with an inferred root:
<article class="post">
<style>
@scope to (.embed) {
img { border-radius: 8px; }
}
</style>
<img src="hero.jpg" alt="">
<div class="embed">
<img src="third-party.jpg" alt="">
</div>
</article>The first image is rounded. The image inside .embed is not, because the limit excludes that subtree.
Specificity and proximity still apply
Inline scoping follows the same cascade rules as @scope with a prelude:
- The implicit root adds no specificity, so
h2inside the rule has the specificity of one type selector. A class rule written elsewhere, such as.title { color: navy; }, wins over it. - When two scopes both match an element and the specificity is equal, scope proximity decides. The root with fewer levels between it and the element wins. With nested components that each carry their own
<style>, the innermost component's styles win over the outer ones, even if the outer<style>comes later in the document.
Limits
- Repeating a
<style>element in every copy of a component adds bytes to the page and makes the browser parse each one. For many copies, a stylesheet with@scope (.note)is cheaper. - Inline
@scopedoes not stop inheritance. Properties such ascolororfont-familyset outside the root still flow in. - Rules outside the scope can still style elements inside it.
Exercise
A <style> containing @scope { a { color: teal; } } is placed directly inside <head>. Which links does it color, and why?
Show answer
None. With no prelude, the scope root is the parent of the <style> element, which is <head>. The a selector matches only descendants of head, and links live in body. To color links, move the <style> inside the element that should be the root, or use a prelude such as @scope (.card).
Exercise
Two nested components each contain their own inline @scope { a { color: ... } }. The outer one sets a to darkmagenta and appears later in the document than the inner one, which sets a to plum. What color is a link in the inner component, and which rule decides it?
Show answer
Plum. Both rules have the same specificity, so scope proximity decides. The inner component's root is closer to the link, so its rule wins even though the outer rule appears later.