CSS: CSS BEM Naming Convention
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 Yourself2. 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
- BEM is one of the best CSS naming conventions
- Through Block, Element, Modifier structure, CSS classes become self-explanatory, conflict-free, and maintainable
- BEM doesn't require nested selectors — each class is independent, avoiding specificity wars
- Whatever convention you choose, team consistency is most important
📝 Exercises
- Create a "user comment" component using BEM naming: avatar, username, time, comment text, like button.
- Rewrite this CSS in BEM style:
CSS
.profile { ... }
.profile .photo { ... }
.profile .info { ... }
.profile .info .name { ... }
.profile .actions .btn { ... }
- Consider introducing BEM naming in your project — practice with a small module on one page.