CSS Methodologies

A CSS methodology is a set of rules for naming classes, so the CSS stays consistent and the class names of two components don't clash. BEM is one such set of…

As a project grows, class names start to clash: two components both have a .title class, and the CSS for one component also changes the other. A CSS methodology is a set of naming rules that a team agrees on, so the CSS stays tidy and class names don't clash. Class names that follow the rules work like shelf labels in a warehouse. The label alone tells you which shelf an item belongs to. The class name alone tells you which component a part belongs to.

BEM naming

BEM stands for Block Element Modifier. This example is a food menu card with its classes written the BEM way:

<div class="card card--active">
  <h3 class="card__title">Fried Rice</h3>
  <p class="card__price">$2.50</p>
</div>

<style>
.card { padding: 16px; border: 1px solid #ddd; }
.card__title { font-size: 20px; }
.card__price { color: gray; }
.card--active { border-color: blue; }
</style>

The result: the card title is 20px, the price is grey, and the card's border is blue, because the card has the class card--active. The name card__price alone shows that the class belongs to the card. If another component has a .title, this card's title does not change, because its class is named .card__title.

Elements can have modifiers too: .card__title--large is the large version of the card title. In that name, card is the block, title is the element, and large is the modifier.

The CSS is still plain CSS

BEM only sets how you name things. In the CSS, each class is still styled with a plain class selector: a dot, then the full class name.

.card__title { font-size: 20px; }

The name in the CSS must match the name in the HTML exactly, including the number of underscores. .card_title with one underscore is a different class, so its rule does not apply to <h3 class="card__title">.

Other ways to name classes

All these ways share one goal: class names that are consistent and easy to maintain, so the CSS for one component does not affect another component. Most important: pick one way, then use it across the whole project.

⚠️ Names that look like BEM but aren't

TL;DR: A CSS methodology is a set of rules for naming classes, so the CSS stays consistent and the class names of two components don't clash. BEM is one such set of rules: you write class names in the pattern block__element--modifier. The block is the component, an element is a part inside the block (written after __), and a modifier is a variant (written after --). For example .card__title--large: the card title, large version. In the CSS, that class is still styled with a plain class selector, with the name exactly as in the HTML. Other methodologies: SMACSS, utility-first and CSS Modules. Pick one and be consistent.

⚡ Block, element, modifier

Write:

<div class="card card--active">
  <h3 class="card__title">Fried Rice</h3>
  <p class="card__price">$2.50</p>
</div>

Meaning: The class names on this card follow BEM (Block Element Modifier), a set of rules for naming classes. card is the block: a component that works on its own. card__title is an element: a part inside the block, written with two underscores __. card--active is a modifier: a variant of the block, written with two hyphens --. A modifier is written together with the original class, card.

⚡ The CSS is still plain

Write:

.card__title { font-size: 20px; }
Guess first: does a .card_title rule (one underscore) apply to <h3 class="card__title">?

No. .card_title is a different class. The name in the CSS must match the name in the HTML exactly, including the number of underscores.

Meaning: BEM only sets how you name things. In the CSS, each class is still styled with a plain class selector: a dot, then the full class name.

⚡ Why bother with names

Meaning: Without naming rules, two components can both have a .title class, and the CSS for one component also changes the other. With BEM, the card title's class is named .card__title, so the CSS for .title does not change it. Other ways of naming, like SMACSS, utility-first and CSS Modules, share the same goal. Pick one way and be consistent.

What you will learn