@layers in CSS

  • CSS
  • Architecture
  • Scalability
@layer reset priority 1: lowest
* { box-sizing: border-box; margin: 0; }
body   { font-family: system-ui; }
button { font: inherit; cursor: pointer; }
@layer base priority 2
/* specificity: (0,3,1); high, but lower layer */
body .card--featured.card--featured {
  border-color: rgba(127,255,138,.4);
  background:   rgba(127,255,138,.07);
  color:        #7fff8a;
}
@layer components priority 3: highest
/* specificity: (0,1,0); low, but higher layer */
.card--featured {
  border-color: rgba(64,217,255,.45);
  background:   rgba(64,217,255,.08);
  color:        #40d9ff;
}

// Rendered Output

Default card

The featured card is cyan: @layer components wins because it is declared later, regardless of specificity. Disable it to see @layer base take over.