BEM with State Classes
What You'll Learn
You'll learn how to combine BEM naming with state classes like is-active or has-error to handle dynamic styling cleanly and predictably.
Why Mix BEM with State Classes?
BEM gives you a solid foundation for naming components, but what happens when a button becomes disabled, a menu opens, or a form field shows an error? These temporary conditions need styling too.
State classes are simple, reusable class names that describe transient conditions—states that change through user interaction or JavaScript. They typically use prefixes like is- or has- to signal they're temporary modifiers of your component's appearance.
How They Work Together
BEM handles your component's structure and variants, while state classes handle dynamic behavior. Think of BEM as the blueprint of a house (rooms, walls, doors) and state classes as whether the lights are on or off.
/* BEM structure */
.button { }
.button--primary { }
/* State class applied alongside BEM */
.button.is-disabled {
opacity: 0.5;
cursor: not-allowed;
}
In HTML, you combine them:
<button class="button button--primary is-disabled">
Submit
</button>
The state class .is-disabled works with any button variant without creating redundant BEM modifiers like .button--primary--disabled, .button--secondary--disabled, etc.
When to Use Each
- BEM modifiers (
.button--large): Permanent variations chosen at authoring time - State classes (
.is-loading): Dynamic conditions toggled by JavaScript
Key Takeaway: Combining BEM with state classes like is-active or has-error keeps your CSS maintainable by separating structural variations (BEM modifiers) from temporary, dynamic states (state classes).