Every color rule for Mr. Harun’s banner is an order on Mrs. Lastri’s board, one lane per @layer: the browser picks the colour, and each order shows why it won…
Why it matters
Big CSS (resets, libraries, components, utilities) easily ends up in a specificity war and !important. With layers you write the order once, @layer reset, library, components, utilities;, and the later ones always win without very long selectors.
The key idea
Layers are checked before specificity
Common mistakes
Thinking the more specific selector always wins Not with layers. Layers are checked FIRST: an #id in an early layer loses to a .class in a later one.
Forgetting that unlayered CSS beats every layer Rules outside any @layer (say a library imported the usual way) beat everything layered. Put everything in a layer, third-party code too: @import url(lib.css) layer(library);.
The layer order named in many places The order is locked the FIRST time a layer name appears, in @layer a, b; or in a block. Write @layer reset, base, theme; once at the very top, before any block.
Try it in this playground
Swap the two names in @layer base, theme; The order named first is the one used. Swap it, and the winner moves although the selectors stay.
Move .title out of its layer Unlayered CSS beats everything layered, even the more specific #hero.
Add !important to the losing rule Now it wins, and with both !important, see the flipped order in the panel.
Remove every @layer Back to the old rule: #hero (1,0,0) always beats .title (0,1,0).