CSS @layer Reference

CSS cascade layers — @layer declaration, block, import — with precedence rules.

Reference for the CSS @layer at-rule — declaring layer order, layer blocks, layered @import, and how cascade layers interact with specificity and !important.

How do cascade layers change precedence?

Cascade layers add a step to the cascade that runs before specificity. A rule in a later-declared layer beats any rule in an earlier layer, regardless of selector specificity. Within a single layer, normal specificity and source order apply as usual.

Organising the cascade with @layer

CSS cascade layers let you split a stylesheet into explicitly ordered groups so that source order and specificity no longer decide everything. A rule in a later layer beats one in an earlier layer regardless of specificity — making resets, frameworks and overrides predictable. This reference covers the at-rule forms and the precedence rules.

How it works

Declare the layer order once with a statement-form @layer, then add rules with the block form. Order is set by first appearance, so an upfront declaration is the safest pattern:

@layer reset, base, components, utilities;   /* fixes precedence */

@layer reset { * { margin: 0; } }
@layer base { a { color: blue; } }
@layer utilities { .text-red { color: red; } }   /* wins over base */
@import url("framework.css") layer(base);
@import url("theme.css") layer();   /* anonymous layer */

For normal declarations a later layer wins; unlayered rules outrank all layers. For !important declarations the order inverts — the earliest layer wins — and important unlayered styles rank lowest.

Tips and notes

  • Declare the order upfront so import position cannot reshuffle layers.
  • Unlayered styles always beat layered normal styles — keep ad-hoc overrides there.
  • Nest layers with @layer outer { @layer inner { ... } } or outer.inner.
  • Use revert-layer to fall back to lower layers for one property.