← Back to HTML & CSS, page 5

html-css-0083 · Hard senior

Compare BEM, utility-first CSS and CSS Modules. How would you choose for a team project?

BEM is a naming rule with flat selectors, utility-first uses small single-purpose classes, and CSS Modules makes class names local through a build step. Choose by team size, framework and tooling, and use custom properties for tokens and @layer for ordering in any of them.

Full answer

All three try to solve the same problem: global CSS is shared by every page, so a change in one place can break another place.

BEM (Block, Element, Modifier) is a naming rule. A block is a standalone part such as card. An element is a part of it, written card__title. A modifier is a variant, written card--featured. Every selector is one class, so specificity stays flat and low, and the name tells you where the style belongs. Cost: long names, and discipline is needed because nothing in the tools enforces it.

Utility-first CSS uses many small single-purpose classes such as p-4, flex and text-center, put straight in the HTML. Styles are not named after the content, and the CSS file stops growing after a while. Cost: long class lists in markup, and a need to extract repeated patterns into components. A build step usually removes unused utilities.

CSS Modules gives each CSS file local class names. The build tool renames .title to something unique such as card_title_x7k2, so classes cannot clash across files. You write normal CSS and import it into the component. Cost: needs a bundler, and global theming still needs custom properties or a shared layer.

How to choose:

  • Many contributors and a component framework: CSS Modules or a utility system keep scope under control.
  • Content sites with plain templates and no build step: BEM works with nothing but discipline.
  • Design system with tokens: use custom properties for the tokens in all three.

Combine with other tools. @layer can order resets, base, components and utilities, so utilities win without !important. Tokens in custom properties are shared across any method.

The BEM card has a blue border, and each selector is a single class, so the specificity is 0,1,0 for all of them. The utility box gets padding and centred text from two classes. The last class stands for what a CSS Modules build would generate from .title.

The class name in the last line is written by hand for the example. A real build tool creates it.

html
<style>
  .card { border: 1px solid #999; padding: 8px; }
  .card__title { margin: 0; font-size: 18px; }
  .card--featured { border-color: #0a6cff; }
  .p-4 { padding: 16px; }
  .text-center { text-align: center; }
  .card_title_x7k2 { color: teal; }
</style>
<div class="card card--featured" id="bem">
  <h3 class="card__title" id="bemt">BEM card</h3>
</div>
<div class="p-4 text-center" id="util">Utility classes</div>
<h3 class="card_title_x7k2" id="mod">Module-style generated name</h3>

The generated-looking class name is written by hand. No build tool was run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • BEM uses block, element and modifier names with single-class selectors.
  • Utility-first puts small single-purpose classes in the markup.
  • CSS Modules renames classes per file at build time.
  • Choose based on team, framework and build setup.
  • Tokens and @layer work with all three.

Common mistakes

  • Choosing a method for style and not for team size and build setup.
  • Mixing BEM names with deep selectors like .card .card__title a, which defeats the flat specificity.
  • Using utilities without extracting repeated patterns into components, so the same ten classes are pasted everywhere.

Follow-ups

  • How can @layer help when utilities need to win over component styles?
  • What does a CSS Modules build do to a class name?

Tips

  • Say what each one solves: naming, repetition or scope.
  • Add that tokens and @layer work with all three.