Key points
- @layer sets a priority order between groups of styles that is checked before specificity.
- Unlayered styles beat every layered style.
- !important flips the layer order: the lowest layer wins.
On a WordPress site, CSS comes from everywhere: the theme, a slider plugin, a form builder, then your own styles. When a plugin ships .slider-wrap .slide .title h3, the traditional way to beat it is an even longer selector or an !important. We’ve inherited stylesheets where half the rules existed only to win those fights. Cascade layers, supported in every current browser, are a much cleaner answer.
Declare the order once
A layer is a named group of rules. Between two layers, declaration order decides, whatever the selectors’ specificity. A single-class rule in components therefore beats a rule in vendor built from three classes and an id.
/* main.css: the first line fixes the priority order, lowest first */
@layer reset, vendor, theme, components, utilities;
@import url("vendor/slider.css") layer(vendor);
@layer theme {
a { color: var(--brand); }
}
@layer components {
.card a { color: inherit; }
}
@layer utilities {
.text-muted { color: #6b7280; }
}
That first line does the heavy lifting: it lists every layer from lowest to highest priority. Without it, the order follows whichever layer happens to appear first in the code, and a file loaded out of sequence can flip everything.
Pushing plugin CSS into a low layer
This is where layers save us the most time. You can’t wrap a <link> tag in a layer, but you can dequeue the plugin’s stylesheet and import it from yours:
// functions.php: stop loading the plugin stylesheet on its own...
add_action( 'wp_enqueue_scripts', function () {
wp_dequeue_style( 'some-slider-plugin' );
}, 100 );
Then copy the file into the theme (or point at its URL) and pull it in with @import ... layer(vendor), as shown above. Keep in mind that @import must come before every other rule except @layer statements. If you copy the file, write that down somewhere: a plugin update may rename its classes.
Two rules that catch everyone out
First: styles outside any layer take precedence over all layers. That’s handy during a gradual migration, since existing CSS keeps priority, but confusing when you forget to wrap a file and it quietly overrides everything.
Second: !important reverses the order. An important declaration in a low layer beats an important declaration in a higher one:
@layer vendor {
.slider-title { color: red !important; } /* wins over theme, even with !important there */
}
@layer theme {
.slider-title { color: black !important; }
}
That’s by design, so a reset or an accessibility sheet can lock certain properties. But if a plugin is heavy on !important, putting it in a low layer actually gives those declarations more weight. In that situation we leave its rules unlayered and only target the specific properties we need to change.
Where to start on an existing site
There’s no need to rewrite everything. On redesigns, we start by wrapping the reset and plugin stylesheets in low layers and leave the theme CSS unlayered. Then we strip out the overlong selectors one at a time, checking each key template as we go. Chrome and Firefox DevTools show each rule’s layer in the Styles panel, which makes debugging far easier.
FAQ
Do cascade layers work with Sass?
Yes. Sass passes @layer straight through to the compiled CSS. Just make sure your @use and @import order doesn’t push the layer order statement away from the top of the output file.
Can I use @layer alongside block editor styles?
For your own stylesheets, yes. Styles generated from theme.json and inline block styles aren’t layered, though, so they will still win over your layers. Plan around that.
Has your WordPress theme’s CSS become impossible to maintain? We take over existing stylesheets and slim them down without breaking the design.



