HTML & CSSMedium
What is BEM methodology?
BEM (Block, Element, Modifier) is a CSS naming convention that makes class names describe a component's structure: .block for a standalone component, .block__element for a part of it, and .block--modifier for a variant or state. It keeps selectors flat with low, equal specificity and makes it obvious which styles belong to which component.
The parts
- Block: a reusable component, meaningful on its own:
.card,.search-form. - Element: a part that only makes sense inside its block:
.card__title,.card__image. Never chain elements (.card__body__title); flatten to.card__title. - Modifier: a variation of a block or element:
.card--featured,.button--small,.card__title--muted. Used in addition to the base class:class="card card--featured".
Why teams use it
- Flat specificity: almost every selector is a single class, so there are no specificity wars.
- No leaking styles: names are unique by convention, so components do not affect each other.
- Readable HTML: you can see the structure and relationships from class names alone.
- Easy to delete: all styles for a component share a prefix.
Drawbacks
- Verbose class names.
- It is a convention, not enforced by tools.
- Scoping tools (CSS Modules, Vue scoped styles, CSS-in-JS) and utility-first frameworks (Tailwind) now solve the same naming and leaking problems automatically, so BEM is less common in component-based apps, though still widely used in design systems.
Interview tip
Explain the problem BEM solves (global namespace and specificity) rather than only the syntax.
Code example
html
1<style>2 body { font-family: system-ui; display: flex; gap: 12px; flex-wrap: wrap; }3 .card { width: 220px; border: 2px solid #0b1020; border-radius: 10px; overflow: hidden; }4 .card__image { width: 100%; height: auto; display: block; }5 .card__body { padding: 10px; }6 .card__title { margin: 0 0 4px; font-size: 1.1rem; }7 .card__text { margin: 0; color: #475569; }8 .card__button { margin-top: 8px; padding: 6px 10px; border: 2px solid #0b1020; background: white; }9 .card--featured { box-shadow: 5px 5px 0 #0b1020; background: #fef9c3; }10 .card__button--primary { background: #2b4bff; color: white; }11</style>12<article class="card">13 <img class="card__image" src="https://picsum.photos/id/1060/440/200" alt="Coffee being poured" width="440" height="200">14 <div class="card__body">15 <h3 class="card__title">Standard card</h3>16 <p class="card__text">Block + elements</p>17 <button class="card__button">Read</button>18 </div>19</article>20<article class="card card--featured">21 <img class="card__image" src="https://picsum.photos/id/1062/440/200" alt="Dog in a sweater" width="440" height="200">22 <div class="card__body">23 <h3 class="card__title">Featured card</h3>24 <p class="card__text">Block modifier + element modifier</p>25 <button class="card__button card__button--primary">Read</button>26 </div>27</article>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.