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>
@layer reset, theme, components;is the list that sets the order of the layers. The leftmost layer is the weakest, and the rightmost layer is the strongest.@layer reset { ... }holds what is in theresetlayer. The CSS inside the curly brackets is written as usual, only wrapped in@layer.backgroundis set in three layers.componentscomes last in the order, sogreenwins.coloris only set intheme, so the text colour stayswhite.
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?
- Forgetting about CSS outside layers. Old CSS outside any layer wins without you noticing. You change what is in a layer, and nothing on screen changes.
- A typo in a layer name.
@layer component { }(no s) makes a new layer. A new layer goes at the end of the order, so the misspelled layer becomes the strongest. - No order list. Without an
@layer a, b, c;line at the top, the layers are ordered by where each layer name first appears in the file. !importantinside a layer. For declarations with!important, the layer order is reversed: the weakest layer wins. If, in the three-layer example, you add!importantto thebackgroundinresetand incomponents, the button has no background, becauseresetwins. For CSS outside layers, the result is reversed too:!importantinside a layer beats!importantoutside all layers. Avoid!importantinside layers.
💡 Libraries can go into a layer too.
@importloads another CSS file.@import url("library.css") layer(vendor);puts the whole CSS of that library into avendorlayer. Putvendorfirst 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.