← Back to HTML & CSS, page 5

html-css-0097 · Hard senior

What does the @scope rule do, and how does it compare with BEM and nesting?

@scope applies rules only inside a scope root, optionally stopping at a lower boundary. It adds proximity to the cascade and does not add the root's specificity. It is newer than BEM or nesting, so check support and keep a fallback outside the block.

Full answer

@scope limits a group of rules to a part of the page. You give it a scope root, and the rules inside only match elements that are inside that root.

css
@scope (.card) {
  h2 { font-size: 1.2rem; }
  a { color: teal; }
}

Here h2 and a styles apply only inside .card. There is no need for a long class name like card__title just to avoid clashes.

Two features set it apart from nesting.

  • A scope limit, written with to: @scope (.card) to (.card-footer). Styles apply inside the card but stop at the footer. This is called a donut scope. It stops styles from leaking into nested components.
  • Proximity in the cascade. If two scopes both match an element, the rule from the scope root that is closest to the element wins, before source order is considered. This helps when a component is nested inside another component, and both set the same properties. The proximity rule comes after specificity.

Two points on specificity. Selectors inside @scope do not get extra specificity from the scope root, unlike the same selector written as .card h2. The :scope pseudo-class refers to the root itself.

Compare with the other ways:

  • BEM solves clashes with names. It works everywhere, but names are long.
  • Nesting is only a way to write selectors. .card { h2 { } } is equal to .card h2 and adds specificity of the parent.
  • CSS Modules rename classes at build time.

@scope is a newer feature. Check the support table before relying on it, and keep a fallback, for example plain class selectors, for browsers that ignore the rule. An unsupported @scope block is skipped completely, so a fallback must live outside it.

The title inside the card is red. The title outside the card is black. The title inside the footer is also black, because the scope ends at .card-footer.

Where the browser does not support @scope, all three titles are black. If red is required, add a fallback rule such as .card > h2 { color: crimson; } before the block.

html
<style>
  h2 { color: black; }
  @scope (.card) to (.card-footer) {
    h2 { color: crimson; }
  }
</style>
<h2 id="out">Outside</h2>
<div class="card">
  <h2 id="in">Inside the card</h2>
  <div class="card-footer"><h2 id="foot">Inside the footer</h2></div>
</div>

Checked in Chrome 156 only. A browser without @scope skips the block, and that case was not run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • @scope (root) limits rules to a part of the DOM.
  • A to (limit) clause makes a donut scope.
  • Proximity prefers the closest scope root when specificity ties.
  • Selectors inside do not gain the root's specificity.
  • Unsupported browsers skip the block, so add a fallback.

Common mistakes

  • Using @scope with no fallback and no support check.
  • Expecting it to add the parent's specificity like a nested selector.
  • Forgetting the lower boundary, so styles leak into nested components.

Follow-ups

  • What is a donut scope?
  • Where does proximity come in the order of the cascade?

Tips

  • Explain the two big ideas: a scope root and an optional lower boundary.
  • Say how it differs from nesting: no added specificity, and proximity.