@scope limits a block of style rules to a subtree:
@scope (.card) to (.slot) {
p { color: red; }
}
- The scoping root is each element matching
.card. Rules match only elements that are the root or its descendants. - The optional scoping limit (
to (.slot)) cuts holes: a.slotinside the card and everything in it are out of scope. That's the "donut", which lets a component style itself without reaching into content slotted into it. - Inner selectors are relative to the root:
pmeans:scope p. Only the subject must be in scope;.page :scope pcan still look outside. - The root's selector adds no specificity:
@scope (#hero) { em {} }is (0, 0, 1) and lost todiv em. Use:scope(0, 1, 0) or&(zero, it's:where(:scope)) to target the root itself. @scope { … }with no prelude inside a<style>element scopes to that element's parent.
Scope proximity is a new cascade step. Between two declarations from rules with different scoping roots, the one whose root is fewer generational or sibling hops from the element wins. Unscoped rules count as infinitely far. The step comes after specificity and before order of appearance:
- Specificity still wins:
@scope (.c2) { p {} }lost to an unscopedp.n. - With equal specificity, a scoped
pbeat a later unscopedp, and the nearer of two theme roots beat the later one.
The draft's own §3.5.1 prose says proximity applies "regardless of specificity", but its cascade criteria list puts it after specificity, and Chrome 156 and Firefox 159 follow the list.
@scope isn't encapsulation: outside rules still match inside, and inheritance is unaffected; isolation is Shadow DOM's job.
Support: Chrome 118, Safari 17.4, Firefox 146.