BEM Modifier Naming with Double Hyphens
What You'll Learn
You'll learn how to use the block--modifier and block__element--modifier syntax to create variations of blocks and elements without duplicating CSS code.
The Core Idea
In BEM, modifiers represent different states, themes, or variations of a block or element. You add them using double hyphens (--) to signal "this is the same component, just modified." Think of it like options on a car — a sedan and an SUV are both vehicles (blocks), but "SUV" is a modifier that changes certain characteristics while keeping the core "vehicle" identity.
The syntax works in two ways:
For blocks:
.button--primary { /* blue button variation */ }
.button--secondary { /* gray button variation */ }
.button--large { /* bigger size variation */ }
For elements:
.card__title--highlighted { /* emphasized title */ }
.menu__item--active { /* current menu item */ }
Why Double Hyphens?
The -- separator clearly indicates "variation" while the __ (double underscore) indicates "child element." This visual distinction makes your CSS instantly readable. When you see button--disabled, you immediately know it's still a button, just in a disabled state.
Practical Example
.alert { /* base alert styles */ }
.alert--success { background-color: green; }
.alert--error { background-color: red; }
.alert--warning { background-color: orange; }
.alert__icon--large { font-size: 2rem; }
Each modifier adds or overrides specific properties while inheriting everything from the base block.
Key Takeaway: Use double hyphens (--) to create variations of blocks or elements, making it clear that you're modifying an existing component rather than creating something entirely new.