CSS Reference ยท At-rules
The @scope at-rule
Reference for the CSS @scope at-rule: limit rules to a subtree with a scoping root and an optional lower boundary, use :scope, and see how proximity wins.
The @scope at-rule limits a group of style rules to one part of the document. You name a scoping root, and the selectors inside the block can only match the root itself or elements below it. Optionally you add a scoping limit, a lower boundary where the scope stops, which gives you a "donut": styles that apply around a nested component but not inside it.
Unlike a naming convention such as BEM, the browser enforces the boundary, and unlike nesting it can stop at an inner element and it adds proximity to the cascade.
@scope (.card) to (.card-footer) { :scope { border: 2px solid steelblue; } p { color: navy; } }
Try it
The card and its direct paragraph are blue and bold. The paragraph inside .slot is plain, because .slot is the scoping limit and the rules do not reach into it.
Both scopes match the second link, and both have equal specificity. The .dark scope wins because its root is closer to the link in the DOM tree, even though it comes later in the source here. Swap the two @scope blocks and the result stays the same.
Syntax
@scope (<scope-start>)? (to (<scope-end>))? {
<rule-list>
}
Initial value, inheritance and animation do not apply: this is an at-rule, not a property.
Syntax and arguments
| Part | Description |
|---|---|
(<scope-start>) |
A selector list. Every element it matches becomes a scoping root. |
to (<scope-end>) |
A selector list for the lower boundary. Elements matching it, and their descendants, are outside the scope. |
:scope |
Inside the block, matches the scoping root. |
& |
Also refers to the scoping root, and works in nested rules. |
| No prelude | @scope { ... } inside a <style> element scopes to the element's parent. |
Usage notes
- Selectors are relative to the root. A bare
pinside@scope (.card)means.card pfor matching, but the root adds no specificity of its own. Only:scopecounts as a pseudo-class. - Proximity is a cascade step. After specificity and before source order, the rule whose scoping root is nearer to the element wins. This makes nested themes work without escalating specificity.
- The root can contain itself. Use
:scopeto style the root; a plaindivselector never matches the root unless it is below another scope. - The limit must be a descendant.
to (...)only matters for elements that are inside the root. The limit element itself is excluded as well. - Not a security boundary. Scoping affects styles only. JavaScript, inheritance and CSS custom properties still pass through normally.
- Inline form. A style element with a bare
@scope { }is handy in component-based templates, but the style element must sit inside the root.
Browser support
Supported in current versions of Chrome, Edge, Safari and Firefox, with Firefox being the most recent to add it, so check your audience's versions before depending on it.
Related
@layerโ control precedence between groups of rules:scopeโ the scoping root pseudo-class- Nesting โ write child rules inside parent rules
- Lesson: @scope
- Lesson: Specificity