CSS BEM Naming Convention
Official Documentation and Further Reading
- Official BEM website
- BEM Methodology (bem.info)
- BEM by Example: Best Practices for BEM CSS Naming
- CSS-Tricks: BEM 101
In the world of web development, writing CSS that is maintainable, reusable, and easy to understand becomes increasingly challenging as projects grow in size and complexity.
Without a proper naming strategy, CSS codebases can quickly become unwieldy, leading to specificity issues and making future modifications difficult.
This is where the BEM naming convention comes to the rescue.
What is BEM?
BEM stands for Block, Element, Modifier - a methodology developed by Yandex, a Russian search engine company. It’s a naming convention that helps you achieve reusable components and code sharing in front-end development.
The core principle behind BEM is to break down user interfaces into independent blocks, create a clear relationship between HTML and CSS, and maintain low specificity in your selectors. This approach ensures everyone working on a project uses the same language and structure when writing CSS.
The Three Parts of BEM
1. Block
A Block represents a standalone component that is meaningful on its own. Think of blocks as the larger, independent sections of your interface.
Examples of blocks include:
- Header
- Navigation menu
- Search form
- Card
- Button
In BEM, blocks are simply represented by class names:
1 |
|
2. Element
Elements are parts of a block that have no standalone meaning and are semantically tied to their block. Elements are denoted by adding double underscores (__
) after the block name.
Examples of elements within blocks:
- Menu items (part of a menu block)
- Header logo (part of a header block)
- Form input (part of a form block)
1 |
|
3. Modifier
Modifiers are variations or different states of blocks or elements. They are used to change appearance, behavior, or state. Modifiers are denoted by adding double hyphens (--
) after the block or element name.
Examples of modifiers:
- Disabled button
- Active menu item
- Large input
1 |
|
A Practical Example
Let’s create a simple card component using BEM conventions:
1 |
|
1 |
|
Benefits of Using BEM
Modularity - BEM promotes creating reusable components that can be easily moved around your codebase without breaking things.
Readability - By looking at a class name, you can immediately understand its purpose and how it relates to other elements on the page.
Low Specificity - BEM makes CSS specificity very flat and low because everything is a class and nothing is nested. This prevents specificity conflicts and makes your CSS more maintainable.
Team Collaboration - BEM gives everyone on a project a declarative syntax to share so they’re on the same page. This common language helps teams work more effectively together.
Developer Confidence - With strict BEM conventions, you can update and add to your CSS with confidence that your changes will not have unexpected side effects.
Common BEM Mistakes to Avoid
1. Creating Elements of Elements
Avoid nesting elements within elements in your class naming. This is a common mistake:
1 |
|
Instead, keep the structure flat:
1 |
|
2. Forgetting to Name Child Elements
Don’t omit class names from child elements in your HTML. Leaving those classes off may be more succinct, but it will increase risks of cascade issues in the future.
3. Using Single Underscores or Hyphens
BEM names intentionally use double underscores and double hyphens instead of single ones to separate Block-Element-Modifier. This allows single hyphens to be used as word separators within names.
1 |
|
Best Practices
Use meaningful names - Your class names should be descriptive and clearly indicate the purpose of the element.
Keep blocks independent - Blocks should not depend on other elements on a page to function correctly.
Avoid excessive nesting - BEM is designed to keep specificity low, so avoid deeply nested selectors.
Use modifiers wisely - Only create modifiers when you have genuine variations of a block or element.
Be consistent - Stick to the BEM naming convention throughout your project for maximum benefit.