Guides · CSS
BEM still matters when you use Tailwind
Tailwind makes writing styles fast, but a long string of utility classes on every element gets hard to read, hard to change, and hard to toggle. Pairing it with BEM keeps both strengths: Tailwind writes the styles, and BEM gives every piece of the UI a name. This is how this site's own CSS is written.
The problem with utilities everywhere
Here's a card written the way Tailwind's docs start you off:
<article
className={`flex flex-col gap-4 rounded-lg bg-white p-6 text-slate-900 ${
selected ? "ring-2 ring-blue-500" : ""
}`}
>
<h3 className={`text-lg font-semibold leading-tight ${selected ? "text-blue-700" : ""}`}>
{title}
</h3>
<p className="text-sm text-slate-600">{body}</p>
</article>It works, and for a one-off it's fine. The cost shows up later:
- You can't tell what anything is. In the browser's dev tools, every card, banner and modal looks like the same pile of
flex p-6 rounded-lg. - State is spread across the markup. "Selected" means two utilities on the root and another on the title. Change what selected looks like and you're editing class strings in every component that renders a card.
- The design is copied, not shared. Render a card in three places and its look lives in three places.
- Tests and scripts have nothing stable to hold. Class names change every time someone tweaks a colour.
BEM fixes all four without giving up Tailwind.
BEM in one minute
BEM stands for Block, Element, Modifier. It's a naming scheme, nothing more.
| Part | What it is | Class |
|---|---|---|
| Block | A piece of UI that makes sense on its own | .card |
| Element | A part of a block that only makes sense inside it | .card__title |
| Modifier | A state or variant of a block or element | .card--selected |
Two underscores join an element to its block. Two dashes add a modifier. Names are lowercase, with single dashes between words: .site-header__nav-link.
The same card, in BEM:
<article className={clsx("card", selected && "card--selected")}>
<h3 className="card__title">{title}</h3>
<p className="card__body">{body}</p>
</article>Now the markup says what each element is, and "selected" is one class you add or remove.
Where the utilities go
The utilities don't disappear. They move into the stylesheet, inside the BEM rule, with @apply:
@layer components {
.card {
@apply flex flex-col gap-4 rounded-lg bg-white p-6 text-slate-900;
}
.card__title {
@apply text-lg font-semibold leading-tight;
}
.card__body {
@apply text-sm text-slate-600;
}
.card--selected {
@apply ring-2 ring-blue-500;
}
.card--selected .card__title {
@apply text-blue-700;
}
}You still write styles in Tailwind's vocabulary, with its spacing scale, colours and theme. You still get its purge step, so unused styles don't ship. You just write them once, in one place, under a name.
Putting the rules in @layer components matters. It places them after Tailwind's base styles and before its utilities, so a one-off utility on a layout wrapper can still win when you need it to.
The rules that keep it clean
BEM is easy to get almost right. These are the rules that stop it drifting.
Keep class names flat
The DOM nests; class names don't. An element is always named after its block, however deep it sits.
<nav class="menu">
<ul class="menu__list">
<li class="menu__item">
<a class="menu__link" href="/">Home</a>
</li>
</ul>
</nav>Never menu__list__item__link. Flat names mean you can move menu__link up or down a level without touching any CSS.
A modifier is an extra class
Keep the base class and add the modifier next to it: class="card card--selected". A modifier on its own (class="card--selected") loses all the base styles.
One class per selector
Write .card__title, not .card .card__title, article.card or #card. When every selector is a single class, every rule has the same weight, and the one that comes later in the file wins. Put modifiers after the base rule and overrides just work, with no !important.
There's one exception: a block's modifier may reach into its own elements, like .card--selected .card__title above. That's the only nesting allowed. Never style one block from inside another (.sidebar .card). If the card needs to look different in the sidebar, give it a modifier.
Name the state, not the look
/* bad */
.tab--border-bottom-blue {}
/* good */
.tab--active {}The day the active tab gets a background instead of a border, the first name is a lie, and you're renaming it in CSS, markup and JavaScript. The second name is still true.
No global helpers
Classes like .hidden, .red or .mt-4-important that you sprinkle on any block bring back the problem BEM solves. If a block can be hidden, give it .card--hidden and decide what hidden means for that block.
Blocks don't place themselves
A block styles its insides and nothing outside it: no outer margin, no absolute position, no grid column. Where a card sits is up to its parent:
.dashboard__cell {
@apply col-span-2;
}<div class="dashboard__cell">
<article class="card">…</article>
</div>That's what makes a block portable. Drop it anywhere and it looks the same.
What Tailwind still does best
Pairing the two doesn't mean fighting Tailwind. Some things stay its job.
Hover, focus, breakpoints and dark mode go inside @apply. Don't invent .card--mobile or .card--dark modifiers for things the browser already knows:
.card {
@apply flex flex-col gap-4 p-6 md:flex-row md:p-8;
@apply dark:bg-slate-900 dark:text-slate-100;
}
.card__link {
@apply underline-offset-4 hover:underline focus-visible:outline-2;
}Keep modifiers for state only your app knows: --selected, --disabled, --loading, --error.
Theme tokens beat one-off values. @apply bg-surface text-brand follows your theme; @apply bg-[#3b82f6] has to be hunted down when the brand colour changes.
Unnamed layout wrappers can keep their utilities. A page-level wrapper that's used once, has no state and no name is fine as it is:
<div class="mx-auto max-w-3xl px-4 py-8">
<article class="card">…</article>
</div>The moment it gets a second use, a state, or a name you'd say out loud ("the page shell"), it becomes a block.
A note for Tailwind v4
In v4, styles in a component's own scope (a Vue or Svelte <style> block, a CSS Module) can't see your theme by default. Add @reference "../app.css"; at the top of that file, pointing at your main stylesheet, or @apply won't know your colours and spacing.
A real example
This blog's post header is written exactly this way. The markup carries only names:
<header className="w-blog-header">
<p className="w-blog-header__kicker">{kicker}</p>
<h1 className="w-blog-header__title">{title}</h1>
<p className="w-blog-header__lede">{lede}</p>
</header>And the stylesheet does the styling:
.w-blog-header {
@apply mb-12 flex flex-col gap-5 border-b border-primary/15 pb-10;
}
.w-blog-header__title {
@apply text-balance font-anton text-5xl uppercase leading-[0.9] text-primary;
@apply sm:text-6xl lg:text-7xl;
}The w- prefix is a namespace for the site, so its blocks can't clash with a third-party library's class names. It's optional, but if you use one, use it everywhere.
The trade-offs
This isn't free, and it's worth being honest about what you give up.
- Two files instead of one. You read the markup in one place and the look in another. Editor tools like Tailwind IntelliSense still autocomplete inside
@apply, which helps. - You have to name things. Naming is work. It's also the work that tells you what your UI is made of.
- It's not Tailwind's default. Tailwind's docs lean towards utilities in markup and reusable components for sharing. This approach is a deliberate choice against that, and new team members may need it explained.
In return you get markup you can read, state that's one class, design that lives in one place, and stable class names for tests and analytics. It also turns out to be easy to hand to an AI coding agent. A short list of rules like the ones above, in a CLAUDE.md or similar file, keeps generated code consistent with what you'd write yourself.
Checklist
Before calling a stylesheet done:
- Every class is a
block, ablock__element, or a--modifier. - No class has
__twice. - Every modifier sits next to its base class.
- No tag selectors, no IDs, no
.a.bcombinations. - The only nesting is a block modifier reaching its own element.
- Modifiers name a state, not a CSS property.
- No global helper classes, and no block styling another block.
- No block sets its own outer margin or position.
- Utilities live in
@apply, inside@layer components, except on one-off layout wrappers. - Hover, focus, breakpoints and dark mode are variants in
@apply, not modifiers. - No
!important; modifiers come after their base rule.
BEM was designed long before Tailwind, but the two fit well: one decides what things are called, the other decides how they look. Keep the names in the markup and the utilities in the stylesheet, and both stay readable.