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 { ... } }orouter.inner. - Use
revert-layerto fall back to lower layers for one property.