Color scheme

@scope

CSS has always been global. Every rule you write competes in a single namespace, and any selector can accidentally reach elements it was never meant to touch. Developers invented BEM, CSS Modules, and CSS-in-JS specifically to work around this problem. The @scope at-rule is the native CSS solution. It lets you confine styles to a subtree of the DOM, set hard boundaries, and even use scoping proximity to resolve conflicts between nested components, all without build tools or JavaScript.

@scope reached Baseline status in December 2025 when Firefox 146 shipped support, joining Chrome and Safari. It is defined in the CSS Cascading and Inheritance Level 6 specification.

How @scope Works

A @scope block takes a scope root selector that defines the upper boundary of the subtree you want to target. Any rules inside the block can only match elements that descend from that root. You can optionally add a scope limit with the to keyword, which defines a lower boundary where the scope stops. Elements matching the limit selector and their descendants are excluded. This combination is sometimes called a "donut scope" because it selects a ring of elements between two boundaries.


        /* Scope root only: styles apply inside .card */
        @scope (.card) {
            h3 { color: var(--color-heading); }
            p { line-height: 1.6; }
        }

        /* Donut scope: styles apply inside .card but stop at .card-footer */
        @scope (.card) to (.card-footer) {
            img {
                border-radius: var(--radius-md);
                width: 100%;
            }
        }
    

In the donut scope example, the img rule applies to images inside .card but not to any images nested inside .card-footer. The scope limit is non-inclusive, meaning the matching element itself and everything below it is excluded.

The :scope Pseudo-Class

Inside a @scope block, the :scope pseudo-class refers to the scope root element. This lets you style the root itself, not just its descendants:


        @scope (.card) {
            :scope {
                background: var(--color-surface);
                border-radius: var(--radius-md);
                padding: var(--space-md);
            }
            h3 { font-size: var(--text-lg); }
        }
    

Be aware that :scope adds its own specificity (0-1-0) as a pseudo-class. The & selector inside @scope behaves differently: it carries zero specificity, just like it does in regular nesting. The MDN reference covers this distinction in detail.

Scoping Proximity

@scope adds scoping proximity to the cascade. When two scoped rules conflict, the one whose scope root is closer to the target element (fewer DOM hops) wins. The browser checks this after specificity but before source order, making it a powerful tool for nested component variants.

Consider a theme that wraps a card, and that card contains another nested theme. Without proximity, you would need increasingly specific selectors to override the outer theme. With @scope, the inner scope automatically wins because it is closer in the DOM tree:


        @scope (.theme-light) {
            p { color: #1e293b; }
        }

        @scope (.theme-dark) {
            p { color: #f1f5f9; }
        }
    

If a .theme-dark container is nested inside a .theme-light container, the paragraphs inside the dark container get light text because that scope root is closer to them. No extra specificity required.

Inline <style> Scoping

When you use @scope inside a <style> element without specifying a root selector, the scope root automatically becomes the parent element of the <style> tag. This enables a pattern where components carry their own scoped styles directly in the markup:


        <article class="card">
            <style>
                @scope {
                    :scope {
                        background: var(--color-surface);
                        padding: var(--space-md);
                    }
                    h3 { color: var(--color-heading); }
                }
            </style>
            <h3>Card Title</h3>
            <p>Card content that is styled by the scoped rules above.</p>
        </article>
    

This approach trades reusability for locality. The styles are right next to the markup they affect, which can be valuable for one-off components. For reusable patterns, keeping @scope blocks in external stylesheets with explicit root selectors is usually the better choice.

Live Demo

The demo below uses the same .badge class in two different containers. Each container is a scope root, and each @scope block styles .badge completely differently. No extra classes, no specificity tricks. The scope boundary does all the work:

Status

Same .badge class, scoped to success.

Status

Same .badge class, scoped to danger.

Here is the CSS behind the demo. Both .badge rules are identical selectors with identical specificity. The only difference is which @scope block they belong to:


        @scope (.success-container) {
            .badge {
                background: #16a34a;
                color: #fff;
                border-radius: 2rem;
            }
        }

        @scope (.danger-container) {
            .badge {
                background: #dc2626;
                color: #fff;
                border-radius: 2rem;
            }
        }
    

When to Reach for @scope

@scope is not a replacement for cascade layers or native nesting. It solves a specific problem: confining styles to a DOM subtree without resorting to naming conventions, Shadow DOM, or build-time tooling. It is most useful when you have components that appear in different contexts and need different styles based on where they sit in the document, or when you want to prevent a component's internal styles from leaking into nested sub-components (the donut scope pattern).

If you are working in a layered architecture with well-named classes and cascade layers, you may not need @scope for every component. But for cases where naming alone is not enough or where DOM nesting creates ambiguity, @scope is the right tool.

Resources

These resources provide the depth you will need to understand @scope thoroughly. Spend time with them before recording your teaching video.

Teaching Video Checklist

When recording your video, make sure you cover these points. You do not need to follow this order, but your teammates should come away understanding each of them:

  • What problem @scope solves that cascade layers and class naming alone do not
  • The syntax for scope root and scope limit (donut scope)
  • The difference between :scope and & inside a @scope block (specificity difference)
  • How scoping proximity works as a cascade tiebreaker
  • A working demo that shows the same class styled differently in two different scopes