Cascade Layers (@layer) — CSS

@layer groups your CSS into layers. The line @layer reset, theme, components; sets their order: the layer named last in that line wins, whatever the…

You add a CSS library (ready-made CSS written by someone else), then try to change the colour of its buttons. Your rule keeps losing, because the library's selectors are more specific. In the end you write longer and longer selectors, or !important everywhere.

When two CSS rules set the same thing on the same element, the browser has to pick one. Usually the more specific selector wins. This is called specificity: #id beats .class, and .class beats a tag name. When two selectors are equally strong, the rule written lower down wins. @layer adds one thing the browser looks at before specificity and writing order: layers. You group your CSS into layers, then you decide which layer wins.

Three layers for one button

This example has one button and three layers that style that button:

<button id="buy" class="btn">Buy</button>

<style>
@layer reset, theme, components;

@layer reset {
  button { background: none; border: none; }
}

@layer theme {
  button { background: navy; color: white; }
}

@layer components {
  .btn { background: green; }
}
</style>

The result is a green button with white text. If you change the first line to @layer components, theme, reset;, reset becomes the strongest layer, so the button loses its background (background: none from reset wins).

Layer order beats specificity

Say you write #buy { background: red; } in the theme layer. An id selector is far more specific than .btn. But the button stays green, because components comes later in the order.

Between two layers, only the order of the layers counts. Specificity only decides between two rules inside the same layer. That is the point of @layer: your rule can win without longer and longer selectors.

CSS outside layers beats every layer

A rule written outside every @layer beats every rule inside a layer, whatever its specificity. Say you add button { background: orange; } to the three-layer example, outside every layer. The button turns orange, even though .btn in components is more specific.

⚠️ Why doesn't the layer you changed win?

💡 Libraries can go into a layer too. @import loads another CSS file. @import url("library.css") layer(vendor); puts the whole CSS of that library into a vendor layer. Put vendor first in the order list, so that it is the weakest layer. Your CSS then wins with no !important.

TL;DR: @layer groups your CSS into layers. The line @layer reset, theme, components; sets their order: the layer named last in that line wins, whatever the specificity (the strength of the selectors) in earlier layers. Specificity only counts between rules inside the same layer. CSS outside any layer beats every layer.

⚡ You set the order

Write:

@layer reset, theme, components;

@layer reset {
  button { background: none; border: none; }
}

@layer theme {
  button { background: navy; color: white; }
}

@layer components {
  .btn { background: green; }
}

Meaning: @layer groups your CSS into layers. The first line is the list that sets their order: the leftmost layer is the weakest, the rightmost the strongest. background is set in three layers, and components comes last. So the button with the class btn is green.

⚡ Order beats specificity

Guess first: that button also has the id buy. The theme layer gets #buy { background: red; }. Does the button turn red?

No, it stays green. Between two layers only the order counts, and components comes later in the order than theme.

Meaning: Specificity (the more specific selector wins) only decides between two rules inside the same layer.

⚡ CSS outside layers is strongest

Guess first: there is a button { background: orange; } outside every layer. What colour is the button?

Orange. CSS outside layers beats every rule inside a layer, whatever its specificity.

Meaning: If you changed what is in a layer and nothing on screen changed, look for old CSS outside the layers.

What you will learn