BEM Naming Convention: Brief Guide
🎯 What is BEM?
BEM stands for Block Element Modifier - a CSS naming methodology that creates clear, maintainable CSS class names.
📝 BEM Syntax
/* Block */
.block {}
/* Element */
.block__element {}
/* Modifier */
.block--modifier {}
.block__element--modifier {}
🧱 The Three Parts
Block (Component)
Independent, reusable component:
.card {}
.button {}
.navigation {}
.search-form {}
Element (Part of Block)
Uses double underscore __:
.card__header {}
.card__title {}
.card__body {}
.button__icon {}
.button__text {}
Modifier (Variation)
Uses double dash --:
.button--primary {}
.button--large {}
.card--featured {}
.card__header--dark {}
💡 Real Example
<!-- HTML -->
<div class="card card--featured">
<header class="card__header card__header--dark">
<h2 class="card__title">Featured Article</h2>
</header>
<div class="card__body">
<p class="card__text">Article content...</p>
</div>
<footer class="card__footer">
<button class="button button--primary button--large">
<span class="button__icon">📖</span>
<span class="button__text">Read More</span>
</button>
</footer>
</div>
/* CSS */
.card {
padding: 1rem;
border-radius: 8px;
background: white;
}
.card--featured {
border: 2px solid #007bff;
}
.card__header {
margin-bottom: 1rem;
}
.card__header--dark {
background: #333;
color: white;
}
.card__title {
margin: 0;
font-size: 1.25rem;
}
.button {
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
}
.button--primary {
background: #007bff;
color: white;
}
.button--large {
padding: 0.75rem 1.5rem;
font-size: 1.125rem;
}
.button__icon {
margin-right: 0.5rem;
}
✅ Benefits
- No naming conflicts - Each class is scoped
- Clear structure - You know what belongs where
- Reusable - Blocks work anywhere
- Maintainable - Easy to find and modify styles
- Team-friendly - Everyone follows same convention
🎯 Quick Rules
- Block names: Describe what it is (
.search-form,.user-card) - Element names: Describe the part (
.search-form__input,.user-card__avatar) - Modifier names: Describe the variation (
.button--large,.card--featured) - No nesting:
.block__element__subelement❌ - Use semantic names:
.card__title✅ not.card__big-text❌
BEM makes CSS as organized and maintainable as your JavaScript code!