· Documentation · 2 min read
BEM Methodology
The BEM (Block, Element, Modifier) methodology is a naming convention for CSS classes that helps you build web interfaces in a more organized, reusable, and maintainable way. Created by the team at Yandex, BEM makes it easier to develop reusable components and maintain clear, scalable code.
BEM Principles
Block:A block is an independent entity that represents a fundamental component of the interface. Think of it as a container for a specific piece of functionality, like a button, a menu, or a header.Example: .button, .menu, .header
Element:An element is a part of a block that doesn’t make sense on its own. It’s a subordinate component within a block. Example: .button__icon, .menu__item, .header__title
Modifier:A modifier is a flag on a block or element. It changes the appearance or behavior of that block or element.Example: .button—primary, .menu__item—active, .header—large
Benefits of BEM
Clarity and Consistency:BEM’s naming conventions provide a clear and consistent structure, making the code easy to read and understand, which is particularly useful in large teams.
Reusability:Blocks and elements defined with BEM can be easily reused throughout the project, reducing code duplication and promoting modular development.
Maintainability:Following BEM makes CSS easier to maintain. Developers can quickly locate the code related to a specific component and make changes without affecting other parts.
Scalability:BEM is perfect for projects of any size, but it shines in large, complex projects. Its modular, clear structure supports scalability and growth without cluttering the code.
Collaboration:BEM’s standardized naming improves collaboration between developers and designers. Everyone can understand and work with the code more efficiently.
Example of BEM in HTML and CSS
HTML
<div class="menu">
<ul class="menu__list>
<li class="menu__item menu__item-active>Home</li>
<li class="menu__item>About</li>
<li class="menu__item>Contact</li>
</ul>
</div>
CSS
.menu {
/*Styles for the menu block*/
}
.menu__list {
/*Styles for the list element within the menu block*/
}
.menu__Item {
/*Styles for the item element within the menu block*/
}
.menu__Item--active {
/*Styles for the active modifier applied to the item element*/
}
Benefits When Developing a Product
Name Conflict Reduction:BEM minimizes name conflicts in CSS by providing a clear convention for naming blocks, elements, and modifiers.
Componentization:Allows developers to break the interface into independent, reusable components, making maintenance and updates easier.
Performance Improvement:Facilitates selective loading of styles and components, enhancing page performance.
Ease of Refactoring:The clear structure of BEM makes code refactoring simpler and safer since changes to one component don’t affect others.
Agile Development:BEM supports agile methodologies by enabling rapid iteration and improvement of components, aligning with continuous development and delivery cycles.
Additional Resources
For a deeper dive into BEM, check out these resources:
BEM Official DocumentationCSS-Tricks on BEM
Smashing Magazine on BEM