BEM
БЭМ
BEM stands for Block, Element, Modifier. It organizes a web interface into reusable pieces and gives CSS classes names that show which piece they belong to and whether they describe a variant or state.
Also known as: Блок Элемент Модификатор
What BEM does
A class like title tells you what something is, but not which part of the page owns its styles. product-card__title tells you it belongs to a product card. BEM uses that idea to organize interfaces and their styles around three kinds of names:
- A block is a self-contained piece of UI, such as
product-card. You can use it in search results and on a catalog page without naming it after either location. - An element is a part of a block, such as
product-card__title. It has a role within that block rather than being designed for independent use. - A modifier describes a variant or state of a block or element. A compact card gets
product-card--compactalongsideproduct-card, not instead of it.
How the names work
A common convention uses block__element for an element and block--modifier or block__element--modifier for a variant. You may also see the classic underscore convention, such as product-card_compact or product-card_size_small. The separators have no special meaning to the browser; use the convention agreed on by the project.
Element names identify ownership, not DOM depth. Even if the heading sits inside product-card__body, call it product-card__title, not product-card__body__title. A selector written as .product-card__title will keep matching if you add or remove a wrapper around the heading.
Example: a compact product card
<article class="product-card product-card--compact">
<h2 class="product-card__title">Desk lamp</h2>
</article>
.product-card { padding: 1rem; }
.product-card--compact { padding: 0.5rem; }
.product-card__title { margin: 0; }
The base class supplies the default padding. The modifier changes just that property. Both padding rules use a single class selector, so they have the same specificity; in this stylesheet, the later rule wins. If the modifier is removed, the base padding still applies.
Where it helps and what it does not solve
BEM is useful in shared stylesheets and design systems because you can search for a class and see which block it belongs to without tracing a chain of nested selectors. It works with plain HTML or a framework such as React or Vue. The trade-off is longer class names, and unlike a mechanism that scopes styles, BEM relies on naming discipline: a global h2 rule can still match the card title.
How it comes up in interviews
You might be asked whether a button inside a card is a block or an element. Start with whether it is reusable on its own. A design-system button can keep its button block class and also carry product-card__action on the same node. The first class styles the reusable button; the second positions it within the card. If the action is specific to the card and has no independent styling to reuse, an element class may be enough.