A cascade layer is a named bucket of style rules inside one origin. The cascade compares layers after origin, shadow context and inline styles, but before specificity. So when two normal declarations are in different layers, the one in the later layer wins, even if the earlier layer's selector has IDs in it.
You create layers three ways:
@layer name { … }puts rules into a layer (repeat the name to add more).@layer a, b, c;declares layers, and their order, without rules.@import url(lib.css) layer(vendor);puts a whole file into a layer;layerwithout a name makes an anonymous one.
Order is the order of first appearance. Writing @layer reset, vendor, components; at the top fixes the order, and later blocks only add rules. Without it, the first @layer vendor {} in the source decides vendor's place, wherever its later blocks are.
Unlayered styles count as an implicit last layer, so they beat every named layer. That's why adding layers to part of a codebase can make the unlayered rest win conflicts it used to lose.
Nesting: @layer framework { @layer base { … } }, or framework.base for short. Nested layers are sorted inside their parent, and the parent's own rules (outside any child) come after its children.
Two exceptions to "later layer wins":
- For
!important, the order reverses: an important declaration in the first layer wins. That keeps!importantmeaningful for a reset or vendor layer. - A
styleattribute isn't in a layer; it's decided before layers, so it beats them all.
revert-layer rolls a property back to what the earlier layers would give. Layers shipped in Chrome 99, Firefox 97 and Safari 15.4.