BEM, short for Block Element Modifier, is a naming methodology for CSS classes that divides a user interface into independent blocks, the elements inside them and the modifiers that change their appearance or state. In CSS, it replaces nested and context-dependent selectors with flat, single-class selectors whose names describe the structure of the component.
A typical BEM class reads card__title--highlighted: the block is card, the element is title and the modifier is highlighted. The convention originated at the Russian search company Yandex in the late 2000s and became one of the most widely used approaches for keeping large stylesheets maintainable across teams.
How BEM structures class names
BEM works by encoding the relationship between parts of a component directly in the class name, so the stylesheet no longer depends on the HTML hierarchy. A block is a standalone, reusable component such as a menu or a search form; an element is a part that has no meaning outside its block; a modifier is a flag that changes appearance, behavior or state.
The classic syntax uses a double underscore to separate a block from an element and a double hyphen to separate a modifier. Words inside one name are joined with a single hyphen, as in search-form__submit-button. The table below summarizes the three entities.
| Entity | Role | Example class | Can exist alone |
|---|---|---|---|
| Block | Independent, reusable component | menu | Yes |
| Element | Dependent part of a block | menu__item | No |
| Modifier | Variant or state of a block or element | menu__item--active | No, always with its base class |
A modifier class is always added next to the base class, never instead of it: <li class="menu__item menu__item--active">. The base class carries the shared styles, while the modifier overrides only what differs, which keeps every rule at the same low specificity.
Why BEM keeps specificity flat
The main technical benefit of BEM is a uniform specificity of one class for almost every rule. Because each selector targets exactly one class, the outcome of the CSS cascade depends mainly on source order rather than on selector weight, and developers rarely need !important to win a conflict.
Descendant selectors such as .sidebar .menu a tie styles to a particular nesting and raise specificity with every level. BEM replaces them with names like .menu__link, so a component can be moved anywhere in the page without inheriting unexpected rules from its new surroundings.
| Selector | Specificity | Depends on markup position |
|---|---|---|
| .menu__link | 0,1,0 | No |
| .menu__link–active | 0,1,0 | No |
| .sidebar .menu a | 0,2,1 | Yes |
| #nav ul li a | 1,0,3 | Yes |
This predictability is also why BEM pairs well with cascade layers and CSS variables: the layer order and custom property values decide the result, not an accidental difference in selector weight.
BEM compared with other CSS approaches
BEM is one of several strategies for organizing CSS at scale, and each answers the problem of global class names differently. The comparison below covers approaches that teams most often evaluate against it.
| Approach | Core idea | Scoping mechanism | Typical trade-off |
|---|---|---|---|
| BEM | Structured, descriptive class names | Naming convention | Long class names |
| Utility-first CSS | Many single-purpose classes | No component classes | Verbose markup |
| CSS Modules | Locally scoped class names | Build-time hashing | Requires a bundler |
| CSS-in-JS | Styles written in JavaScript | Generated class names | Runtime or build cost |
| Shadow DOM styles | Encapsulated component styles | Browser-native boundary | Harder global theming |
BEM needs no tooling at all, which makes it attractive for server-rendered sites, design systems shared across frameworks and projects maintained by mixed teams. Tools such as Sass complement it: the parent selector reference &__title produces BEM names without repeating the block name, while the compiled output stays flat.
Applying BEM in real projects
Successful BEM adoption depends on consistent decisions about block boundaries. A useful rule is that anything which could be reused elsewhere becomes its own block, while parts that only make sense inside one component remain elements.
| Situation | Recommended BEM choice | Reason |
|---|---|---|
| Button used across pages | Separate block button | Reusable outside any parent |
| Title inside a card | Element card__title | Meaningless outside the card |
| Card in a featured state | Modifier card--featured | Variant of the same block |
| Card placed in a grid | Mix card grid__item | Layout belongs to the parent |
| Element of an element | Flat name card__icon | Avoids deep chains |
The mix pattern deserves attention: one HTML node can carry classes from two blocks, so grid__item controls position while card controls appearance. This keeps layout concerns out of reusable components and allows the same card to appear in a grid, a carousel or a sidebar.
Common BEM mistakes
Most problems with BEM come from mirroring the DOM tree in class names or from treating modifiers as standalone classes. The following list covers the errors that reviewers flag most often.
- Element chains such as
card__body__title__icon, which recreate the nesting BEM is meant to remove. - Modifier without base class, for example
button--primaryalone, which loses the shared styles. - Styling elements from another block, which couples components that should stay independent.
- Tag selectors inside blocks, such as
.card h2, which raise specificity and break when markup changes. - Inconsistent separators across the codebase, which make automated linting impossible.
A stylelint rule for class patterns catches most of these errors before they reach production. The main limitation of BEM itself is verbosity: class names grow long, and the method offers no automatic protection against two teams choosing the same block name.
In simple terms
BEM is a naming habit for web page styles. Each piece of a page gets a name that says what it is and where it belongs, much like labeling boxes during a house move: “kitchen, plates, fragile”.
Because every name is unique and descriptive, a designer can change one piece without accidentally changing another. The key benefit is predictability: when a style is written for one component, it stays with that component.
A practical example
A mid-sized online retailer maintained a stylesheet of about 9,000 lines built from nested selectors. Every change to product cards on the category page broke something on the search page, and developers had added more than 200 !important declarations to force their rules through.
The frontend team rewrote the product card as a BEM block named product-card, with elements such as product-card__price and modifiers such as product-card--sold-out. Layout rules moved to mixes like results-grid__item, and a linter enforced the naming pattern on every pull request.
After six months, the team had migrated the main components, reduced the number of !important declarations to fewer than 20, and could reuse the same card in recommendation carousels without writing page-specific overrides.