CSS: CSS BEM Naming Convention

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

1. What is BEM?

BEM stands for Block, Element, Modifier — a naming convention that makes CSS classes self-explanatory and conflict-free.

(1) Structure

CSS
.block { }
.block__element { }
.block--modifier { }
.block__element--modifier { }

▶ Example

▶ Try it Yourself

▶ Example

BEM naming for a navigation component:

▶ Try it Yourself

2. Why BEM?

Benefit Description
Self-documenting Class names explain what they style
No conflicts Unique names prevent style collisions
No specificity wars Flat selectors, no nesting needed
Easy maintenance Change one class without affecting others

▶ Example

BEM for a button component with variants:

▶ Try it Yourself

❓ FAQ

Q How should I name CSS classes? Is there a recommended convention?
A Freestyle naming survives a few pages; past that you get collisions and override chaos. The common recommendation is BEM: block__element--modifier, like card__title--large — the name tells you which component, which part, what state. Whether your team picks BEM, SMACSS or SUIT matters less than everyone using one convention site-wide.
Q What is BEM naming? What do __ and -- mean?
A BEM splits a class name into three parts: Block is the standalone component (.card), Element is a part of it joined with double underscores (.card__title), Modifier is a state joined with double hyphens (.card--featured). The payoff: self-explanatory, project-unique class names — you can change one block without fear and skip deep nesting entirely.
Q How do I write a BEM modifier? Show me an example.
A Attach it after the block or element: <button class="button button--large"> in HTML, .button--large { font-size: 1.25rem; } in CSS. Keep both the base and modifier classes together (button button--large) and let the modifier only declare the differences, not repeat base styles; element states like menu__item--active work the same way.
Q BEM class names are too long — what to do?
A Long names are BEM's design trade-off — better long names than style conflicts. If it bothers you, agree on abbreviations as a team (card__title → card-t), but it must be consistent project-wide; Sass nesting with &__title also kills most of the repetitive prefixes.
Q Can -- and __ be mixed in BEM?
A Yes. block__element--modifier is the standard form, meaning "an element of a block in a given state", like card__title--highlight. Just don't reverse the order into block--modifier__element.
Q Is BEM suitable for small projects?
A For a handful of pages BEM is overkill — casual .title and .btn names are fine. Past 10 pages or the second developer, BEM's collision-proofing pays off immediately; in team work, "afraid to touch the styles" costs far more than long class names.
Q BEM or atomic classes (Tailwind) — which should I pick?
A They're not rivals and are often mixed: BEM carries component structure (.card__title), atomic classes handle generic tweaks (.mt-10, .text-center). Want clean structure and easy handover → BEM; want to ship pages fast with a Tailwind-fluent team → atomic. A common mid-size setup: BEM skeleton, atomic spacing.
Q With BEM, do I still write nested selectors like .card .title?
A No. Flattening nesting is BEM's core win: .card .header .title becomes one class, .card__title. Specificity stays at a single class, so overrides never pile up !important and performance is better. Whenever you feel like writing .card .title, name it .card__title instead.
Q What's the relation between state classes like is-active and BEM modifiers?
A is-active is SMACSS's state prefix; BEM expresses the same thing with a modifier: menu__item--active. Don't mix them — "active" being is-active in one place and --active in another will drive future maintainers crazy. Pick BEM → standardize on --active; keeping generic states like is-hidden is fine if the style guide says so.

📖 Summary

📝 Exercises

  1. Create a "user comment" component using BEM naming: avatar, username, time, comment text, like button.
  2. Rewrite this CSS in BEM style:
CSS
.profile { ... }
.profile .photo { ... }
.profile .info { ... }
.profile .info .name { ... }
.profile .actions .btn { ... }
  1. Consider introducing BEM naming in your project — practice with a small module on one page.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏