CSS @scope Rule: Scoped Styles Without Frameworks

CSS @scope: styles that stay inside a component

A rule like .card a { color: inherit; } also hits links in a newsletter form that someone places inside the card, and .prose p styles paragraphs in a nested block that should look different. BEM class names and growing specificity were the workarounds. @scope limits rules to a subtree of the page, optionally with a lower boundary where the scope ends, and resolves conflicts by proximity: the nearer scope wins.

Problem: A block theme’s card styles leak into nested blocks: a Buttons block inside a card gets the card’s link color, and a quote inside the post content inherits the article’s paragraph spacing, which then needs overrides with higher specificity.

Solution: Wrap component styles in @scope (.ha-card) to (.ha-card__slot) so they apply inside the card but stop at the slot where other blocks are inserted, use :scope for the root element, and rely on scoping proximity instead of specificity when two components nest.

A scope with a lower boundary

@scope (.ha-card) to (.ha-card__content) {
    :scope {
        display: grid;
        gap: .75rem;
        padding: 1.25rem;
        border: 1px solid #e5e7eb;
        border-radius: .5rem;
    }

    a {
        color: inherit;
        text-decoration-thickness: 1px;
    }

    img {
        border-radius: .25rem;
    }
}

The rules apply to elements inside .ha-card, but not inside .ha-card__content, the area where editors put arbitrary blocks. A Buttons block in the content keeps its own link styles; the title link in the card header gets the card’s color. :scope refers to the scope root, the card itself.

The selectors inside the block have the specificity they have alone: a stays (0,0,1), not (0,1,1) like .ha-card a. That makes them easy to override.

Proximity instead of specificity

@scope (.ha-theme-dark) {
    a { color: #93c5fd; }
}
@scope (.ha-theme-light) {
    a { color: #1d4ed8; }
}
<div class="ha-theme-dark">
    <div class="ha-theme-light">
        <a href="#">Light link inside a dark area</a>
    </div>
</div>

Both rules match the link with the same specificity. Without scopes, the later rule would win regardless of nesting. With @scope, the cascade first compares scoping proximity, the number of generations between the element and the scope root, and the nearer scope (.ha-theme-light) wins, which is what nested color schemes need.

Scoped style elements in block markup

<div class="wp-block-group ha-promo">
    <style>
        @scope {
            :scope { padding: 2rem; background: #fef3c7; }
            h2 { margin-block-start: 0; }
        }
    </style>
    <h2>Summer sale</h2>
    <p>…</p>
</div>

An @scope without a selector inside a <style> element uses the style element’s parent as root. This is useful for a pattern or a Custom HTML block that brings its own styles without leaking them into the page. Editors without unfiltered_html cannot save <style> elements, so this is for patterns registered in code.

@scope and the block editor

Themes load their CSS into the editor iframe with add_editor_style() or through theme.json. Scoped rules work there like on the front end, as long as the classes exist in both places. Block styles from register_block_style() with inline_style are a good place for scopes: register_block_style.

NOTE: @scope works in Chrome and Edge 118 (2023) and Safari 17.4 (March 2024); Firefox has it only behind a flag so far (November 2025). In browsers without support the whole @scope block is ignored, so a component that depends on it for its basic layout needs a fallback; keep layout rules outside the scope and use the scope for the parts that should not leak. Cascade layers solve a related problem from the other side: CSS cascade layers.

Update (2025): Firefox 146 (December 2025) shipped @scope, so it is now supported in all major browsers.