CSS @layer: Managing Cascade Layers in WordPress Themes

CSS cascade layers (@layer) in a WordPress theme

In a WordPress page, the CSS comes from many places: core block styles, global styles from theme.json, the theme, every plugin. Which rule wins depends on specificity and order, and themes end up with long selectors or !important to beat a plugin. @layer adds a new step to the cascade: rules in a later layer win over rules in an earlier layer, whatever their specificity. Used in a WordPress theme, there is one trap that decides everything.

Problem: A theme’s reset and base styles keep overriding small component tweaks, and the developers raise specificity to fix it. A first attempt to put all theme styles into layers made things worse: now the core block styles win over the theme everywhere.

Solution: Declare the layer order once at the top of the theme stylesheet, put the reset, the base element styles and the utilities into layers, and keep the theme’s component rules that must override core and plugin styles outside of any layer, because unlayered styles beat all layered styles.

The rule that decides everything

The order of the cascade for normal declarations is: layers in the order they are declared, then everything that is in no layer at all. Core block styles, global styles and plugin stylesheets are loaded with normal <link> and <style> elements and are not in a layer. So:

  • A theme rule inside @layer loses against .wp-block-button__link from core, even with a more specific selector.
  • A theme rule outside a layer competes with core as before, by specificity and order.

Layers are therefore a tool to order the theme’s own styles, not to win against others.

Layers for the theme’s own styles

/* style.css (after the theme header comment) */
@layer ha-reset, ha-base, ha-utilities;

@layer ha-reset {
    *, *::before, *::after { box-sizing: border-box; }
    img, video { max-inline-size: 100%; block-size: auto; }
}

@layer ha-base {
    body { line-height: 1.6; }
    a { color: var(--wp--preset--color--primary); text-underline-offset: .15em; }
    :where(h1, h2, h3) { line-height: 1.2; text-wrap: balance; }
}

@layer ha-utilities {
    .ha-visually-hidden {
        position: absolute; inline-size: 1px; block-size: 1px; overflow: hidden; clip-path: inset(50%);
    }
}

/* Components: unlayered, they compete with core and plugin CSS normally */
.ha-card { padding: 1.5rem; border-radius: .5rem; }
.ha-card a { color: inherit; }

The first line fixes the order: ha-reset is the weakest, ha-utilities the strongest of the three. The a color in ha-base can be overridden by .ha-card a without any specificity game, because the card rule is unlayered. The prefix ha- keeps the theme’s layer names apart from layers that a plugin might declare.

Put a third-party stylesheet into a low layer

The one way to make foreign CSS weaker is to load it into a layer with @import:

/* css/vendor-layer.css */
@import url("../vendor/slider/slider.css") layer(ha-vendor);
add_action( 'wp_enqueue_scripts', function (): void {
    wp_dequeue_style( 'some-slider' ); // the plugin's own <link>
    wp_enqueue_style( 'ha-vendor-layer', get_theme_file_uri( 'css/vendor-layer.css' ), array(), wp_get_theme()->get( 'Version' ) );
}, 20 );

@import must be at the top of a stylesheet, before all other rules except @charset and layer statements; inside an @layer { } block it is invalid. Each @import is another request that is discovered late, so use this for one or two problematic stylesheets, not for all of them. Add ha-vendor to the start of the order line: @layer ha-vendor, ha-reset, ha-base, ha-utilities;. The order line must come before any stylesheet that uses the layers, or the order is the order of first appearance.

!important reverses the order

For !important declarations the layer order is reversed: an important declaration in ha-reset beats an important declaration in an unlayered stylesheet. That is intended for resets that must never be overridden, for example prefers-reduced-motion rules, and it is a reason to avoid !important elsewhere in layered code.

Check in DevTools

Chrome and Firefox show the layer of each rule in the Styles panel, and Chrome has a layer tree in the Elements panel (the “Layers” icon in the Styles toolbar). When a rule does not apply, the panel shows whether it lost because of the layer order or because of specificity.

NOTE: Cascade layers work in all current browsers since 2022 (Chrome 99, Firefox 97, Safari 15.4). Browsers without support ignore the layered rules completely, which on today’s traffic is a negligible share. Other modern CSS for themes: native CSS nesting.