日本語

Organizing Style Priority with CSS @layer

​​CSS​Playwright

CSS @layer (cascade layers) lets you decide which style wins based on the layer it is written in. A style in a later layer wins over a style in an earlier layer, even if the later one has lower specificity. You no longer need longer selectors or !important to override styles written with a strong ID selector.

Note

The test environment is Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. For each syntax I wanted to check, I made a test page, opened it in Chromium, and checked the text color with getComputedStyle(). I confirmed how the browser parsed the CSS with the list of rules in document.styleSheets (CSSOM). I did not test Firefox or Safari.

Declare the layer order at the top and assign rules to layers

First, declare the names of the layers you will use, from weakest to strongest, separated by commas. Then write the rules inside @layer name { … } blocks.

css
@layer base, utilities;

@layer base {
  #t.box { color: red; }
}
@layer utilities {
  .text-blue { color: blue; }
}
html
<p id="t" class="box text-blue">文字</p>

#t.box contains an ID and a class, so it is far more specific than .text-blue. Even so, in my test environment the text turned blue. The style in utilities, which was declared later, won.

As described on MDN's @layer page, once the layer order decides the winner, neither specificity nor source position matters. Specificity only decides conflicts within the same layer.

The next image shows the same thing with a button background color. Without @layer, the more specific #send.btn wins. When the rules are split into layers, .bg-pink in the later layer wins.

Two code examples stacked vertically. The labels in the image are in Japanese. The top frame, with a label meaning "not using @layer", contains CSS that gives #send.btn a gray background and .bg-pink a pink background. The button below it, with a label meaning "Send", is gray. The bottom frame, with a label meaning "deciding the order with @layer", contains the same two rules split into base and utilities layers. The button below it is pink.
The same two rules give different results once they are split into layers (Chromium 153)

The order is set by the first place each layer name appears

The layer order is determined by the first time each name appears in the CSS. If you do not declare the order at the top, the order in which the @layer blocks are written becomes the layer order.

css
@layer components { .t { color: blue; } }
@layer base { .t { color: red; } }

Written this way, components, which appears first, becomes the weaker layer. In my test environment the text was red, from base. When I added the line @layer base, components; before the same two blocks, the text turned blue, from components.

Once the order is set, it cannot be changed later. Even when I appended @layer components, base; at the end of the CSS above, the text stayed blue. In the CSSOM, the later declaration still exists as a rule, but it does not affect the order of layers that already exist.

text
CSSLayerStatementRule nameList=[base, components]
CSSLayerBlockRule name="components"
  CSSStyleRule ".t" { color: rgb(0, 0, 255); }
CSSLayerBlockRule name="base"
  CSSStyleRule ".t" { color: rgb(255, 0, 0); }
CSSLayerStatementRule nameList=[components, base]

The same applies when the stylesheets are split. In my test environment, I put only @layer base, components; in the first <style> element and the components and base blocks above in the second one. The text turned blue, from components. The order declared in the first stylesheet applied to the second one as well. The MDN Learn page on cascade layers also recommends declaring the order in advance. Putting one line such as @layer reset, base, components, utilities; at the top of the first CSS file you load lets you manage the order in one place.

Unlayered styles are stronger than any layer

Styles outside any @layer block are stronger than all layers.

css
p { color: green; }

@layer utilities {
  #t.text-blue { color: blue; }
}

In my test environment, the text turned green. The unlayered p is the least specific selector in this example and comes before the layer, but it still beats #t.text-blue inside a layer. Styles in an anonymous layer (@layer { … }) behave the same way: even when written after an unlayered style, they lost to it.

The CSS Cascading and Inheritance Level 5 spec says that unlayered styles are treated as belonging to an implicit final layer that comes after all named layers.

This matters when you add @layer to existing CSS little by little. If you put only the newly written styles into layers, the older unlayered styles still win. To make the new styles take effect, move the older styles into a weaker layer such as legacy.

Import external CSS into a layer with @import

For CSS you do not edit yourself, such as a library, you can put it into a layer by loading it with @import and layer().

css
@import url("vendor.css") layer(vendor);
@layer vendor, components;

@layer components {
  .t { color: blue; }
}

In my test, I wrote the ID selector rule #t-import { color: red; } in vendor.css. When loaded with layer(vendor), the text turned blue, from components. When loaded without layer(), the rule from vendor.css went outside any layer, and the text turned red.

As described on MDN's @import page, apart from @charset and layer order declarations (in the form @layer a, b;), @import must come before all other rules. In my test environment, I put an @import after a @layer components { … } block, and the @import was ignored. No @import rule appeared in the CSSOM either.

There is no way to put CSS loaded with a <link> element into a layer. The MDN Learn page on cascade layers also advises using @import in that case.

Adding !important reverses the layer order

Among declarations marked !important, the order of strength is reversed.

css
@layer base, utilities;

@layer base {
  .t { color: red !important; }
}
@layer utilities {
  .t { color: blue !important; }
}

In my test environment, the text was red. Among !important declarations, the layer declared first (base) wins. The relationship between unlayered and layered styles also reverses. When I put !important on both, comparing the unlayered .t (green) with the layered .t (red), red won. Remove the !important and the unlayered green wins.

Diagram of layer strength for @layer base, components, utilities;. The left column, with a label meaning "normal declarations", lists from top to bottom: base, components, utilities, and unlayered. Unlayered is at the bottom and is pink. The right column, with a label meaning "!important declarations", lists from top to bottom: unlayered, utilities, components, and base. Base is at the bottom and is pink. Higher means weaker and lower means stronger. The labels in the image are in Japanese.
Normal and !important declarations reverse the order of layer strength

As described on MDN's @layer page, declarations with !important take precedence over those without it. Among !important declarations, the order is the reverse of the normal order.

You can use this to place a "do not override this" rule in a weak layer. For example, if you put !important on a rule in a reset or base layer, later layers cannot override it, even when they also use !important.

Nested layers are ordered within their parent layer

Layers can contain other layers. To add rules to a child layer inside a parent layer from outside the block, join the names with ., as in framework.layout (MDN's @layer page).

css
@layer framework {
  @layer layout, theme;
}
@layer framework.theme  { .t { color: blue; } }
@layer framework.layout { .t { color: red; } }

In my test environment, the text was blue. Because layout, theme was declared in that order in the parent, framework.theme is stronger than framework.layout, even though the framework.layout block comes later in the source.

A style written directly in the parent layer beats the child layers inside it. When I wrote .t { color: red; } directly inside @layer framework { … } and .t { color: blue; } in the child layer layout, the text was red. As with the outer layers, the style that is not in a child layer wins.

A child layer cannot move outside the order of its parent. Even when I declared @layer framework, app; and wrote a highly specific #t.t { color: red; } in framework.layout, the .t { color: blue; } in app won, and the text was blue. If you put a whole library into one layer, its normal declarations stay weaker than your own layers, no matter which layers it uses internally.

Use revert-layer to fall back to a value from a lower layer

To undo a layer's declaration for just one element within that layer, write revert-layer as the value.

css
@layer base, components;

@layer base {
  .t { color: green; }
}
@layer components {
  .t { color: red; }
  .t.plain { color: revert-layer; }
}
html
<p id="t" class="t plain">文字</p>

In my test environment, the text was green, from base, not red from components. According to MDN's revert-layer page, revert-layer recalculates the value as if the element had no rule in that layer. If no lower layer sets a value either, the value falls back to the browser's default style or the like.

Supported browsers

According to MDN's browser compatibility data for @layer, @layer is supported in Chrome 99, Firefox 97, and Safari 15.4. The compatibility data for revert-layer shows support from the same versions.

​Posted by 86