1.4 · Selectors & structure

Ending specificity wars: @layer

Decide up front which group of styles wins, whatever the specificity.

Cascade layers let you decide up front which group of styles wins. @layer reset, components, utilities; says utilities beat components and components beat the reset. Specificity only matters within a layer. A single class in a later layer beats an ID in an earlier one.

Styles outside any layer beat every layer. That’s handy for overrides, and it explains why third-party CSS placed in a layer stops fighting your own.

Before
.text-danger {
  color: crimson !important;
}

/* …or a selector arms race */
body .stack .text-danger { … }
Now
@layer reset, components, utilities;

@layer components {
  #buy-now.button { color: navy; }
}
@layer utilities {
  .text-danger { color: crimson; } /* wins */
}