Qué es Constructor de reglas CSS @scope?
The CSS @scope at-rule allows developers to define a boundary for style rules, limiting their effect to a specific DOM subtree. This solves the classic CSS scoping problem without BEM, CSS Modules, or Shadow DOM. The @scope rule takes an upper boundary selector (the scope root, required) and an optional lower boundary selector (the scope limit, optional). Styles within @scope do not leak outside the subtree, and styles outside @scope do not override scoped rules without intentional selector matching. Scoped rules have lower specificity than unscoped global rules, which makes overrides predictable and intentional.
Respuesta rápida
CSS @scope limits style rules to a DOM subtree, preventing style leaks without BEM or Shadow DOM. Configure an upper boundary (scope root) and optional lower boundary (scope limit). Scoped rules have lower specificity than unscoped rules for predictable overrides. Chrome 118+, Safari 17.4+, Firefox 128+ (Baseline 2025).
Limitaciones
- @scope does not create a new stacking context, z-index isolation, or containment -- it only limits selector matching. Properties like z-index, CSS counters, and custom properties still interact across scope boundaries.
- Overly broad scope root selectors (such as a common class used in many places) can cause unexpected style application. Each @scope rule is evaluated for every element matching the scope root, so complex scope trees may affect performance on pages with many DOM elements.
- In browsers that do not support @scope, the rules inside the at-rule are ignored entirely, not un-scoped. If broad browser support is required, duplicate the scoped rules as unscoped rules outside the @scope block as a fallback.
Cómo usar esta herramienta
- Enter the scope root selector -- the element that defines the upper boundary. All style rules inside the @scope block apply only within this element's subtree.
- Optionally add a scope limit selector -- the lower boundary that restricts how deep the scoped styles apply. Style rules do not apply to elements inside the scope limit or deeper.
- Write the CSS style rules inside the @scope block using standard CSS syntax. These rules have lower specificity than unscoped global rules targeting the same elements.
- Copy the generated @scope CSS and add it to your stylesheet. Place @scope rules after global defaults but before component-specific overrides for the most predictable cascade.
Para qué puedes usarla
- Scope widget styles to their container element so they do not leak into the rest of the page, eliminating the need for BEM class naming on every element.
- Apply different link styles within a sidebar versus the main content area without writing higher-specificity selectors or using !important.
- Restrict third-party embed styles to their wrapper div so the embed's internal CSS does not affect the surrounding page layout.