Cascade layers on a real stylesheet

Every large stylesheet ends up in the same place. A component style won't apply, so someone adds a class to the selector.

Every large stylesheet ends up in the same place. A component style won’t apply, so someone adds a class to the selector. Then a utility class stops working, so someone adds !important. A year later nobody wants to touch the CSS.

Cascade layers fix the root of this. Since March 2022 they work in all three major engines.

Order beats specificity

You declare the layers once, at the top:

@layer reset, base, components, utilities;

From then on, a rule in a later layer wins over a rule in an earlier layer, no matter how specific each selector is.

@layer components {
  .card a {
    color: inherit;
  }
}

@layer utilities {
  .text-red {
    color: red;
  }
}
<div class="card">
  <a class="text-red" href="#">Read more</a>
</div>

The link is red. .card a has the higher specificity, but utilities comes after components, and that settles it before specificity is even looked at. Inside a single layer, specificity and source order work exactly like they always did.

That’s the whole idea. Instead of fighting with selectors, you decide up front which group of styles should win.

Putting a stylesheet into layers

You can open a layer as many times as you like, and the rules all end up in the same layer:

@layer reset {
  *, *::before, *::after {
    box-sizing: border-box;
  }
}

@layer base {
  a {
    color: var(--link-color);
  }
}

Third-party CSS can go into a layer straight from the import:

@layer reset, vendor, base, components, utilities;

@import url('datepicker.css') layer(vendor);

This is probably the most useful trick here. Library styles with deep selectors no longer beat your own components, because the whole vendor layer sits underneath them. Note that @import still has to come before your other rules. Only the @layer statement is allowed above it.

Layers can also be nested, like @layer components.buttons, if you want more structure inside a layer.

Two rules that surprise people

Styles outside any layer win. Anything you don’t put in a layer behaves like it lives in one final layer on top of all the others. That’s convenient during a migration: put your old CSS into layers one file at a time, and whatever’s left unlayered still wins like before. It also means a stray unlayered rule can quietly override your utilities.

!important flips the order. For important declarations, earlier layers win over later ones. So an !important in reset beats an !important in utilities. This sounds backwards, but it lets a low-level layer protect something on purpose. For example, a reset can mark [hidden] { display: none !important; } and no component can accidentally undo it.

A migration that doesn’t break everything

  • Add the layer order statement at the very top of your main stylesheet.
  • Move your reset and third-party CSS into reset and vendor. These are low risk.
  • Move your component CSS into components, one area at a time, checking that nothing shifts.
  • Move utilities last, then start deleting the !importants that only existed to beat component selectors.

DevTools shows the layer next to each rule in the Styles panel, which makes it much easier to see why something wins.

Older browsers

This is the catch. A browser that doesn’t support @layer drops the whole block, not just the layering. Everything inside is gone. Check your analytics, and if you still need older versions, the postcss-cascade-layers plugin can rewrite layers into plain CSS with adjusted specificity at build time.