<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kresho</title>
    <description>The latest articles on DEV Community by Kresho (@shokre).</description>
    <link>https://dev.to/shokre</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F21650%2F1d2cefb5-be68-423f-b501-196aec7f3ee6.jpeg</url>
      <title>DEV Community: Kresho</title>
      <link>https://dev.to/shokre</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shokre"/>
    <language>en</language>
    <item>
      <title>CSS Cascade Layers: The Specificity Solution Your Build Tool Can Automate</title>
      <dc:creator>Kresho</dc:creator>
      <pubDate>Wed, 29 Apr 2026 06:49:58 +0000</pubDate>
      <link>https://dev.to/shokre/css-cascade-layers-the-specificity-solution-your-build-tool-can-automate-1f6i</link>
      <guid>https://dev.to/shokre/css-cascade-layers-the-specificity-solution-your-build-tool-can-automate-1f6i</guid>
      <description>&lt;p&gt;If you've ever added &lt;code&gt;!important&lt;/code&gt; to a CSS rule just to override a third-party component's styles, you know the feeling. You know it's wrong. You know it'll come back to haunt you. But the alternative, writing an even more specific selector, feels just as bad.&lt;/p&gt;

&lt;p&gt;CSS specificity has been the source of countless hours of frustration in frontend development. And while conventions like BEM and methodologies like ITCSS have helped, they're all workarounds for a fundamental problem in how CSS resolves conflicts.&lt;/p&gt;

&lt;p&gt;That changed with &lt;strong&gt;CSS Cascade Layers&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Cascade Layers?
&lt;/h2&gt;

&lt;p&gt;Cascade Layers, introduced via the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@layer" rel="noopener noreferrer"&gt;&lt;code&gt;@layer&lt;/code&gt; rule&lt;/a&gt;, give you explicit control over which styles take priority — independent of selector specificity. You declare an order of layers, and CSS respects it. Period.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@layer&lt;/span&gt; &lt;span class="n"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;components&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;@layer&lt;/span&gt; &lt;span class="n"&gt;reset&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@layer&lt;/span&gt; &lt;span class="n"&gt;components&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@layer&lt;/span&gt; &lt;span class="n"&gt;utilities&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;.text-red&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;.text-red&lt;/code&gt; will always override &lt;code&gt;.button&lt;/code&gt;'s color — not because it has higher specificity, but because the &lt;code&gt;utilities&lt;/code&gt; layer is declared after &lt;code&gt;components&lt;/code&gt;. No &lt;code&gt;!important&lt;/code&gt; needed. No specificity hacks. The layer order is the single source of truth.&lt;/p&gt;

&lt;p&gt;This is a game-changer for a few reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Third-party styles become manageable.&lt;/strong&gt; Put your vendor CSS in an early layer, and your own styles will always win.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Utility classes just work.&lt;/strong&gt; Tailwind-style utilities in a later layer override component styles without needing specificity tricks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Team conventions become enforceable.&lt;/strong&gt; The layer order is explicit and visible, not buried in selector complexity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the browser support is solid — Chrome 99+, Firefox 97+, Safari 15.4+, Edge 99+. If you're targeting modern browsers, you can use this today.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem Layers Don't Solve
&lt;/h2&gt;

&lt;p&gt;There's a catch, though. While the &lt;em&gt;concept&lt;/em&gt; of layers is elegant, the &lt;em&gt;practice&lt;/em&gt; of maintaining them in a real codebase gets messy fast.&lt;/p&gt;

&lt;p&gt;Consider a typical project with hundreds of CSS or SCSS files spread across directories like &lt;code&gt;components/&lt;/code&gt;, &lt;code&gt;pages/&lt;/code&gt;, &lt;code&gt;utilities/&lt;/code&gt;, and &lt;code&gt;vendor/&lt;/code&gt;. To use layers, you'd need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wrap every CSS file in the appropriate &lt;code&gt;@layer&lt;/code&gt; block&lt;/li&gt;
&lt;li&gt;Keep a single &lt;code&gt;@layer&lt;/code&gt; order declaration in sync with your architecture&lt;/li&gt;
&lt;li&gt;Remember to wrap new files as you create them&lt;/li&gt;
&lt;li&gt;Handle edge cases like Sass &lt;code&gt;@use&lt;/code&gt; statements that can't live inside a layer block&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's a lot of manual bookkeeping. And manual bookkeeping in CSS is exactly the kind of thing that breaks quietly and gets discovered in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let Your Build Tool Do It
&lt;/h2&gt;

&lt;p&gt;This is the kind of tedious, pattern-based work that build tools are perfect for. That's why I built &lt;strong&gt;css-layering-webpack-plugin&lt;/strong&gt; (and later, a Vite equivalent).&lt;/p&gt;

&lt;p&gt;The idea is simple: you define your layers with glob patterns, and the plugin wraps matching files in &lt;code&gt;@layer&lt;/code&gt; blocks at build time. It also generates and injects the layer order declaration into your HTML automatically.&lt;/p&gt;

&lt;p&gt;Here's what a typical configuration looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;layers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/reset.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/base/**/*.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;components&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/components/**/*.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utilities&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/utilities/**/*.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. Every CSS file matching &lt;code&gt;**/components/**/*.css&lt;/code&gt; gets wrapped in &lt;code&gt;@layer components { ... }&lt;/code&gt;, and the plugin injects &lt;code&gt;@layer reset, base, components, utilities;&lt;/code&gt; into your HTML &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A few things happen automatically that you'd otherwise have to handle yourself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sass &lt;code&gt;@use&lt;/code&gt; statements are preserved&lt;/strong&gt; at the top of the file, outside the layer block (since Sass requires &lt;code&gt;@use&lt;/code&gt; to come before any other rules).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The layer order declaration stays in sync&lt;/strong&gt; with your configuration — add a layer, and it appears in the right place.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;First-match-wins&lt;/strong&gt; behavior means files are only wrapped in the first matching layer, so overlapping patterns are predictable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also exclude specific files from a layer, which is useful for incremental migration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/components/**/*.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/components/legacy/**&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;components&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or define layers without a &lt;code&gt;path&lt;/code&gt; to include manually-created layers in the order declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;third-party&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// No path — just reserves its spot in the layer order&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where This Shines
&lt;/h2&gt;

&lt;p&gt;Here are some scenarios where automated layering really pays off:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You're integrating a design system.&lt;/strong&gt; Your team uses a shared component library, and its styles keep clashing with your app's styles. Put the library in an early layer, and your app styles always win — without touching the library's code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You're adopting utility-first CSS alongside existing component styles.&lt;/strong&gt; Whether it's Tailwind or your own utility classes, putting them in a later layer means they'll override component styles as intended, without &lt;code&gt;!important&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You're working on a large codebase with multiple teams.&lt;/strong&gt; Layers make the CSS architecture explicit. A new developer can look at the layer configuration and immediately understand the intended specificity order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You're migrating incrementally.&lt;/strong&gt; You don't have to layer everything at once. Start by wrapping new code in layers while excluding legacy files. Over time, bring more files into the layered architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;The plugin is available for both major bundlers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Webpack&lt;/strong&gt;: &lt;a href="https://github.com/kburich/css-layering-webpack-plugin" rel="noopener noreferrer"&gt;css-layering-webpack-plugin&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vite&lt;/strong&gt;: &lt;a href="https://github.com/kburich/vite-plugin-css-layering" rel="noopener noreferrer"&gt;vite-plugin-css-layering&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both use the same configuration format, so the examples above work regardless of which bundler you're using. Install the one that matches your setup and add the layer configuration to your build config.&lt;/p&gt;

&lt;p&gt;The layer order injection supports multiple strategies — inline &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tags (the default), external &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tags pointing to an emitted CSS file, or no injection at all if you prefer to manage the declaration manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Note on AI usage
&lt;/h2&gt;

&lt;p&gt;AI was used to create tests for the Webpack plugin. The plugin was ported to Vite using AI pretty effectively. This article was created with help from AI. &lt;/p&gt;




&lt;p&gt;If you've been fighting specificity battles in your CSS, give Cascade Layers a try. And if you don't want to wrap every file by hand, let your build tool do it for you.&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>webpack</category>
      <category>vite</category>
    </item>
  </channel>
</rss>
