BEM Methodology: Block, Element, Modifier Naming Convention
What is BEM?
BEM stands for Block, Element, Modifier. It is a CSS naming convention that helps you create reusable, organized, and maintainable stylesheets. BEM gives every CSS class a clear, descriptive name that tells you exactly what it does and where it belongs.
Why Use BEM?
As projects grow, CSS becomes difficult to manage. Common problems include:
- Naming conflicts: Two developers use the same class name for different things
- Specificity wars: Overriding styles requires increasingly specific selectors
- Unclear relationships: You cannot tell which classes belong together
- Fear of editing: Changing one class might break something unexpected
BEM solves all of these by enforcing a strict naming structure.
The BEM Structure
Block
A Block is a standalone component that is meaningful on its own. It represents the top-level abstraction.
/* Blocks */
.card { }
.navbar { }
.search-form { }
.sidebar { }
.button { }Element
An Element is a part of a Block that has no standalone meaning. It is always tied to its Block. Elements use a double underscore (__) separator.
/* Elements: block__element */
.card__title { }
.card__image { }
.card__body { }
.card__footer { }
.navbar__logo { }
.navbar__link { }
.search-form__input { }
.search-form__button { }Modifier
A Modifier changes the appearance, behavior, or state of a Block or Element. Modifiers use a double hyphen (--) separator.
/* Modifiers: block--modifier or block__element--modifier */
.card--featured { }
.card--dark { }
.button--primary { }
.button--large { }
.button--disabled { }
.navbar__link--active { }
.card__title--highlighted { }BEM in Practice
HTML Structure
<article class="card card--featured">
<img class="card__image" src="photo.jpg" alt="Featured post">
<div class="card__body">
<h2 class="card__title">Article Title</h2>
<p class="card__text">Article description goes here.</p>
<a class="card__link card__link--primary" href="#">Read More</a>
</div>
</article>CSS
/* Block */
.card {
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
/* Modifier on Block */
.card--featured {
border: 2px solid #3498db;
}
.card--dark {
background: #2c3e50;
color: white;
}
/* Elements */
.card__image {
width: 100%;
height: 200px;
object-fit: cover;
}
.card__body {
padding: 1.5rem;
}
.card__title {
font-size: 1.25rem;
margin-bottom: 0.5rem;
}
.card__text {
color: #666;
line-height: 1.6;
}
.card__link {
display: inline-block;
margin-top: 1rem;
text-decoration: none;
}
/* Modifier on Element */
.card__link--primary {
color: #3498db;
font-weight: 600;
}Button Component Example
/* Block */
.button {
display: inline-flex;
align-items: center;
padding: 0.625rem 1.25rem;
border: none;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
transition: all 0.2s ease;
}
/* Size Modifiers */
.button--small { padding: 0.375rem 0.75rem; font-size: 0.875rem; }
.button--large { padding: 0.875rem 1.75rem; font-size: 1.125rem; }
/* Color Modifiers */
.button--primary { background: #3498db; color: white; }
.button--secondary { background: #95a5a6; color: white; }
.button--danger { background: #e74c3c; color: white; }
.button--outline {
background: transparent;
border: 2px solid #3498db;
color: #3498db;
}
/* State Modifiers */
.button--disabled { opacity: 0.5; cursor: not-allowed; }
/* Element */
.button__icon { margin-right: 0.5rem; }
.button__text { font-weight: 500; }<button class="button button--primary button--large">
<span class="button__icon">+</span>
<span class="button__text">Add Item</span>
</button>Navigation Example
.nav {
display: flex;
align-items: center;
padding: 1rem 2rem;
background: #2c3e50;
}
.nav__logo {
font-size: 1.5rem;
font-weight: bold;
color: white;
}
.nav__list {
display: flex;
list-style: none;
margin: 0;
padding: 0;
gap: 0.5rem;
margin-left: auto;
}
.nav__item { }
.nav__link {
color: rgba(255,255,255,0.8);
text-decoration: none;
padding: 0.5rem 1rem;
border-radius: 4px;
transition: all 0.2s;
}
.nav__link:hover {
color: white;
background: rgba(255,255,255,0.1);
}
.nav__link--active {
color: white;
background: rgba(255,255,255,0.15);
}BEM Rules
Rule 1: Flat Selectors Only
BEM avoids nesting. Every class has the same specificity:
/* GOOD: Flat, low specificity */
.card__title { font-size: 1.25rem; }
/* BAD: Nested, higher specificity */
.card .title { font-size: 1.25rem; }
.card > .card__title { font-size: 1.25rem; }Rule 2: No Element of an Element
Don't create chains like block__element__subelement:
/* BAD */
.card__body__title { }
/* GOOD: Elements are always tied to the Block directly */
.card__title { }
.card__body { }Rule 3: Modifiers Can't Exist Alone
A modifier class should always be used alongside the base class:
<!-- BAD -->
<button class="button--primary">Click</button>
<!-- GOOD -->
<button class="button button--primary">Click</button>When to Use BEM
- Component libraries: Buttons, cards, modals, forms
- Large projects: Multiple developers working on the same codebase
- Design systems: Consistent, reusable UI patterns
- Long-lived projects: Code that needs to be maintained for years
When BEM May Not Be Needed
- Very small projects or prototypes
- Utility-first frameworks like Tailwind CSS
- CSS-in-JS solutions where scoping is automatic
- Single-developer personal projects
Summary
BEM provides a clear, predictable naming convention: Block for standalone components, Element (double underscore) for parts of a block, and Modifier (double hyphen) for variations. BEM keeps selectors flat, avoids specificity conflicts, and makes CSS self-documenting. It is one of the most widely adopted CSS methodologies and an essential skill for professional front-end development.