Sobes.tech
Senior

What is the BEM methodology in CSS?

sobes.tech AI

Answer from AI

The BEM (Block, Element, Modifier) methodology is an approach to writing CSS that helps create reusable and scalable styles.

  • Block: an independent component, e.g., menu, button.
  • Element: a part of a block that cannot exist independently, e.g., menu__item, button__icon.
  • Modifier: a variant of a block or element that changes its appearance or behavior, e.g., button--disabled, menu__item--active.

Example of using classes according to BEM:

<div class="menu">
  <a class="menu__item menu__item--active" href="#">Home</a>
  <a class="menu__item" href="#">About</a>
</div>

This approach makes the structure easier to understand and maintain CSS, reduces the likelihood of style conflicts, and improves code readability.

What is the BEM methodology in CSS? — Frontend - sobes.tech