ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

CSS3 Tutorial• 7 min read

CSS Architecture, Cascade Layers (@layer) & BEM

Managing specificity wars with @layer, BEM naming, and design tokens

As codebases grow, managing CSS specificity and cascade precedence becomes critical. Cascade Layers (@layer) provide native architectural boundaries.

Cascade Layers (@layer)

Cascade Layers solve the "specificity war" where developers resort to !important.

Define layer order at the top of your stylesheet: @layer reset, base, components, utilities;.

Styles in later layers always win over styles in earlier layers regardless of CSS selector specificity!

BEM (Block Element Modifier): A modular class naming convention: .card, .card__title, .card--highlighted.

Structuring Styles with @layer
/* Layer order dictates precedence (later layers win!) */
@layer reset, components, utilities;

@layer components {
  /* High specificity selector */
  .card.featured .title {
    color: #1e293b;
    padding: 16px;
  }
}

@layer utilities {
  /* Simple class selector WINS over .card.featured .title above 
     because 'utilities' is declared after 'components'! */
  .text-emerald {
    color: #04AA6D;
  }
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

In modern CSS Cascade Layers (@layer), which layer wins when styles conflict?