Vipin
Back

28 Nov 2026 · 4 min read

How I use BEM to name CSS classes

Block, Element, Modifier: the naming rules I follow so plain CSS stays readable as a project grows, and where I still reach for Tailwind instead.

Naming CSS classes looks like a small problem until a project has forty components and you're scared to delete any rule because you don't know what it styles. BEM is the convention I use to avoid that. It's not a library or a tool. It's just a set of naming rules.

The three parts

BEM stands for Block, Element, Modifier.

  • Block: a standalone piece of UI that makes sense on its own. card, navbar, search-form.
  • Element: a part of a block that has no meaning outside it. Written with two underscores: card__title, card__image, navbar__link.
  • Modifier: a variant or state of a block or element. Written with two hyphens: card--featured, navbar__link--active.
<article class="card card--featured">
  <img class="card__image" src="/project.png" alt="">
  <h3 class="card__title">PillBridge</h3>
  <p class="card__text">Medication reminders for patients and caregivers.</p>
  <a class="card__link card__link--disabled" href="#">View code</a>
</article>

Just reading the HTML, I can tell that card__title belongs to card, that the card is the featured variant, and that the link is disabled. I don't need to open the stylesheet to find that out.

The rules I actually follow

1. One class per selector, no nesting.

/* Yes */
.card__title { font-size: 1.25rem; }

/* No */
.card h3 { font-size: 1.25rem; }
.card .title { font-size: 1.25rem; }

Every selector has the same specificity, so I never get into a !important war. Changing <h3> to <h2> doesn't break the styles either.

2. Never chain elements. If there's a list inside a card, it's card__item, not card__list__item. The class name says which block the element belongs to, not where it sits in the DOM. The HTML can be restructured without renaming everything.

3. A modifier never appears alone. It's always class="card card--featured", never just card--featured. The modifier only holds the difference; the base class holds everything else.

.card { padding: 1.5rem; border: 1px solid #222; }
.card--featured { border-color: #fff; }

4. When an element gets big, it becomes a new block. If card__footer grows its own buttons, icons and states, I turn it into card-footer with its own card-footer__button. This is my rule of thumb for when to split a component.

5. Layout belongs to the parent, not the block. A card shouldn't set its own margin or width, because it doesn't know where it will be used. The grid that holds it decides that:

.project-grid { display: grid; gap: 2rem; }
.project-grid__item { min-width: 0; }
<div class="project-grid">
  <article class="project-grid__item card">...</article>
</div>

This is called a mix: one element with classes from two blocks. The card is reusable anywhere, and the grid controls spacing.

State: modifiers vs is- classes

For state that JavaScript toggles (open, active, loading), I prefer modifiers, because the class name says which block it belongs to:

link.classList.toggle('navbar__link--active', isCurrentPage);

When the state comes from an attribute that's already there, like aria-expanded or disabled, I style the attribute directly instead of adding a duplicate class. That keeps accessibility and styling in sync:

.accordion__panel[hidden] { display: none; }
.menu__button[aria-expanded="true"] { background: #fff; color: #000; }

A file per block

Each block gets its own file, so finding the CSS is just a matter of the class name:

styles/
  blocks/
    card.css
    navbar.css
    search-form.css
  base.css

When I delete a component, I delete its file, and I know nothing else depended on it. That's the biggest win for me: deleting code becomes safe.

Where I don't use BEM

This portfolio is built with Tailwind, and for most React work I use utility classes or CSS Modules. Both already solve the problem BEM solves (no global name clashes, styles live next to the component), so adding BEM on top would just be extra typing.

I reach for BEM when:

  • the project is plain HTML/CSS or a server-rendered template with a global stylesheet,
  • several people write CSS in the same files, or
  • I'm writing a small widget or component library that others will theme by overriding classes.

Common mistakes to avoid

  • Long names that mirror the DOM: header__nav__list__item__link. It should just be nav__link.
  • Using modifiers for one-off tweaks: button--margin-top-12. That's a layout concern for the parent, not a variant of the button.
  • Mixing conventions: half the project in camelCase, half in BEM. Pick one and write it down in the README.

Takeaway

BEM is boring on purpose. Every class tells you what it is and what it belongs to, every selector has the same weight, and every block can be moved or deleted on its own. For a global stylesheet, that predictability is worth a few extra underscores.