跪拜 Guibai
← All articles
Frontend · CSS · HTML

BEM Class Names Give Front-End CSS a Naming Contract That Ends Selector Fights

By 进击的明明 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams get a naming contract that stops one person's .btn from silently overriding another's, at the price of longer class names and slightly heavier HTML. No framework or build step is required.

Summary

BEM splits front-end styling into three named roles. A block is a standalone reusable component, like .menu or .card. An element is a child part that only means something inside its block, written with a double underscore: .menu__item. A modifier captures a state or variant with a double hyphen: .button--disabled. The full formula is .block, .block__element, .block--modifier, and .block__element--modifier.

Because selectors stay single-class and flat, BEM removes the specificity battles that deep nesting creates. Class names double as documentation: card__button--primary reads as the primary button inside the card without a stylesheet lookup. The trade-offs are longer names, extra classes in the HTML, and overkill for very small projects. Yandex proposed the convention, and it has become the working contract for teams that need shared CSS rules.

Takeaways
— BEM stands for Block, Element, and Modifier, and was proposed by Russian search company Yandex.
— A block is an independent, reusable component; an element is a child part with no meaning outside its block.
— A double underscore connects block and element, while a double hyphen marks a modifier.
— The full class-name formula is .block, .block__element, .block--modifier, and .block__element--modifier.
— BEM keeps selectors flat and single-class, which lowers specificity and makes overrides easier.
— Modifiers must appear in HTML together with their base class, such as card--dark sitting next to card.
— Class names should be lowercase with hyphens, and element names should not nest (card__text, not card__body__text).
— Sass's & parent selector reduces repeated prefixes while keeping BEM names grouped.
— BEM's downsides are long class names, verbose HTML, and low value on very small projects.
Conclusions

BEM is a convention that only pays off when the whole team follows the same naming rules; the naming rules do the work a style guide would otherwise do.

Self-documenting class names carry structure and state directly in the markup, so a selector like card__button--primary replaces both a comment and a stylesheet lookup.

The modifier rule prevents a common failure: because a modifier must sit alongside its base class, it cannot silently replace the component's base styles.

BEM asks teams to pay an upfront verbosity cost in exchange for less time spent debugging inherited CSS later.

Concepts & terms
BEM
A CSS class-naming methodology from Yandex that stands for Block, Element, and Modifier. It structures class names so each selector states what component it belongs to and what state it represents.
Block (BEM)
The standalone, reusable component in BEM, such as .card or .menu. It does not depend on any other component.
Element (BEM)
A child part of a Block that only has meaning inside that block, written with a double underscore, e.g. .menu__item.
Modifier (BEM)
A state, appearance, or variant of a Block or Element, written with a double hyphen, e.g. .button--disabled.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗