CSS @scope At-Rule

The @scope at-rule limits where a group of CSS selectors can match within a document.

It lets authors style component subtrees without repeatedly prefixing every selector. An optional lower boundary can prevent styling from reaching nested components.

@scope Syntax

@scope (.card) {
  h2 {
    color: #0056b3;
  }
}
@scope (.card) to (.nested-card) {
  p {
    line-height: 1.6;
  }
}

The optional first selector identifies a scoping root. The to clause identifies a lower boundary. Scoped selectors match only within the permitted subtree.

How @scope Works

The @scope rule limits where nested style rules can match, without requiring a long selector prefix on every rule. It is useful for component styles and repeated content blocks.

The scoping root itself is not automatically matched by an ordinary descendant selector inside the scope; use :scope when the root element needs styling.

Scope Roots

@scope (.panel) {
  :scope {
    border: 1px solid #bbb;
  }
  h3 {
    color: #175d91;
  }
}

:scope targets the current scoping root. The h3 selector targets matching descendants inside it.

Lower Boundaries

@scope (.card) to (.nested-card) {
  p {
    color: #175d91;
  }
}

The lower boundary excludes matching .nested-card subtrees from the outer scope. This helps keep a parent component from styling nested components of the same family.

Scoping Proximity

Scoping proximity is a cascade tie-breaker: when declarations are otherwise tied at the relevant cascade stages, the rule whose scoping root is closer to the matched element can win.

Origin, importance, cascade layers, and specificity still matter. A scoped rule does not automatically outrank every unscoped rule.

Nested and Implicit Scopes

Text inside the widget.

An @scope rule with no explicit root can use the parent of an embedded <style> element as its scope root. Use this pattern carefully when authoring reusable HTML.

@scope Example

This example styles the heading inside a card while leaving the heading outside the card unchanged.

HTML

<div class="card">
  <h2>Scoped heading</h2>
  <p>Card content.</p>
</div>
<h2>Outside heading</h2>

CSS

@scope (.card) {
  h2 {
    color: #0056b3;
  }
}

Play in Editor

Browser Support

@scope is supported in modern browsers, but compatibility varies by browser version. Check Can I Use? for CSS @scope before relying on it for essential styling. Provide a usable fallback when necessary.

Specifications

@scope is specified in CSS Cascading and Inheritance Level 6.

CSS Cascading and Inheritance Level 6