TExcelTAD
Home › Sass › BEM with Sass

BEM with Sass

BEM (Block, Element, Modifier) is a naming convention that keeps CSS predictable in large projects. Every class name says what component it belongs to and what role it plays. Sass's parent selector & makes writing BEM short and tidy.

The BEM naming pattern

Part Meaning Syntax Example
Block A standalone component .block .card
Element A part of the block .block__element .card__title
Modifier A variation or state .block--modifier / .block__element--modifier .card--featured

Every selector is a single class, so specificity stays flat and low — no fights between deeply nested rules.

Writing BEM with &

&__element and &--modifier append to the block's name instead of creating a descendant selector:

scss · try it yourself
.card {
  border: 1px solid #e5d5de;
  border-radius: 10px;
  padding: 16px;
  max-width: 260px;
  font-family: system-ui, sans-serif;

  &__title {
    margin: 0 0 8px;
    font-size: 1.1rem;
  }

  &__body {
    color: #555;
    line-height: 1.5;
  }

  &--featured {
    border-color: #cc6699;
    box-shadow: 0 4px 14px rgb(204 102 153 / 0.25);
  }
}
.card {
  border: 1px solid #e5d5de;
  border-radius: 10px;
  padding: 16px;
  max-width: 260px;
  font-family: system-ui, sans-serif;
}
.card__title {
  margin: 0 0 8px;
  font-size: 1.1rem;
}
.card__body {
  color: #555;
  line-height: 1.5;
}
.card--featured {
  border-color: #cc6699;
  box-shadow: 0 4px 14px rgba(204, 102, 153, 0.25);
}

Notice the output: .card__title, not .card .card__title. Every rule is one class.

Modifiers that change elements

Sometimes a block modifier must restyle an element. Nest the element under the modifier — this creates one intentional descendant selector:

scss · try it yourself
.card {
  &__title { color: #2d2a32; }

  &--dark {
    background: #2d2a32;

    .card__title { color: white; }
  }
}
.card__title {
  color: #2d2a32;
}
.card--dark {
  background: #2d2a32;
}
.card--dark .card__title {
  color: white;
}

To avoid repeating the block name, store & in a variable at the top of the block:

scss · try it yourself
.media {
  $self: &;

  display: flex;
  gap: 12px;

  &__image { width: 64px; }

  &--reversed {
    flex-direction: row-reverse;

    #{$self}__image { outline: 2px solid #cc6699; }
  }
}
.media {
  display: flex;
  gap: 12px;
}
.media__image {
  width: 64px;
}
.media--reversed {
  flex-direction: row-reverse;
}
.media--reversed .media__image {
  outline: 2px solid #cc6699;
}

Generating modifiers from a map

scss · try it yourself
$variants: (
  primary: #cc6699,
  neutral: #6b7280,
  success: #2e7d32,
);

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  color: white;

  @each $name, $color in $variants {
    &--#{$name} { background: $color; }
  }
}
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  color: white;
}
.badge--primary {
  background: #cc6699;
}
.badge--neutral {
  background: #6b7280;
}
.badge--success {
  background: #2e7d32;
}

BEM rules of thumb

  • Elements belong to blocks, not to other elements: use .card__icon, not .card__header__icon.
  • A modifier never stands alone in HTML: class="btn btn--large", not just btn--large.
  • State classes like .is-open or .is-active are often used alongside BEM for JavaScript-toggled states: &.is-open { … }.
  • One block per partial: _card.scss contains only .card styles. This pairs perfectly with the 7-1 pattern (next lesson).

Tip: Don't nest &__element inside &__other-element — .card { &__header { &__title {} } } produces .card__header__title, which breaks the convention.

Test yourself

Inside .nav { … }, what does &__link compile to?

  1. .nav .__link
  2. .nav__link
  3. .nav.__link
  4. __link.nav

In BEM, what does .button--large represent?

  1. A block
  2. An element
  3. A modifier
  4. A state from JavaScript

Why is BEM good for specificity?

  1. It uses IDs
  2. Most selectors are a single class, so specificity stays flat
  3. It uses !important
  4. It nests deeply