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:
.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:
.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:
.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
$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 justbtn--large. - State classes like
.is-openor.is-activeare often used alongside BEM for JavaScript-toggled states:&.is-open { … }. - One block per partial:
_card.scsscontains only.cardstyles. This pairs perfectly with the 7-1 pattern (next lesson).
Tip: Don't nest
&__elementinside&__other-element—.card { &__header { &__title {} } }produces.card__header__title, which breaks the convention.
Test yourself
Inside .nav { … }, what does &__link compile to?
- .nav .__link
- .nav__link
- .nav.__link
- __link.nav
In BEM, what does .button--large represent?
- A block
- An element
- A modifier
- A state from JavaScript
Why is BEM good for specificity?
- It uses IDs
- Most selectors are a single class, so specificity stays flat
- It uses !important
- It nests deeply