When it comes to writing maintainable and scalable CSS code for your web projects, having a clear and consistent naming convention is crucial. One popular methodology that has gained widespread adoption in the front-end development community is BEM, which stands for Block, Element, Modifier. In this blog post, let's dive deep into CSS BEM architecture, explore its core principles with some code samples.
What is BEM
BEM is a naming convention and methodology for writing HTML and CSS code that promotes modularity and reusability. It was introduced to address the challenges of managing complex CSS in large-scale web projects. The main idea behind BEM is to break down your UI into smaller, self-contained components called blocks, and then further modularize those blocks into elements and modifiers. Let's understand them one by one -
Block
- A block represents a standalone, reusable component on your web page.
- Blocks encapsulate a specific piece of functionality or a UI element.
- Block names are always written in lowercase, like
button
,span
,text-container
etc.
Element
- Elements are the parts or components that make up a block.
- Elements should also be named in lowercase.
- They are always tied to a specific block and are represented by two underscores followed by the element name, like
text-container__heading
,text-container__subheading
.
Modifier
- Modifiers are used to alter the appearance or behavior of blocks or elements.
- They are prefixed with two hyphens and should be named in lowercase
- Modifiers can be applied to blocks or elements to change their style or behavior, like
button--primary
ortext-container__heading--large
ortext-container__heading--small
.
Implementing BEM in HTML and CSS
Here's a practical example of how to implement BEM in your HTML and CSS code.
<!-- HTML CODE -->
<nav class="menu">
<ul class="menu__list">
<li class="menu__item">
<a class="menu__link" href="#">Home</a>
</li>
<li class="menu__item menu__item--active">
<a class="menu__link" href="#">About</a>
</li>
<li class="menu__item">
<a class="menu__link" href="#">Services</a>
</li>
</ul>
</nav>
/* CSS CODE */
.menu {
background-color: #333;
color: #fff;
}
.menu__list {
list-style-type: none;
padding: 0;
}
.menu__item {
margin: 0;
padding: 10px;
}
.menu__link {
color: #fff;
text-decoration: none;
}
.menu__item--active {
background-color: #007bff;
}
Element with class menu
is the block, elements with class menu__list
, menu__item
and menu__link
are the elements of block menu
and .menu__item--active
is a modifier class.
It might look like extra efforts but here are some really significant benefits you should consider -
Modularity and Reusability: By specifying elements within a block, you create self-contained and reusable components. These components can be used across your website or web application without worrying about unexpected side effects or conflicting styles. Each block and its elements are isolated, which promotes modularity in your codebase.
Clarity and Maintainability: BEM's naming convention provides a clear and intuitive way to understand the relationships between elements and their corresponding blocks. When you or other developers review the code, it's easy to see which elements are meant to be part of which blocks. This clarity simplifies debugging, maintenance, and updates.
Consistency: BEM enforces a consistent naming structure for your CSS classes. This consistency is especially valuable when working on large projects with multiple team members. Everyone adhering to the same naming conventions ensures that the codebase remains organized and predictable.
This becomes even fun and simpler when we are working with a CSS pre-processor like SASS. So, in conclusion, understanding and applying BEM principles will undoubtedly enhance your CSS development skills and make you a more effective front-end developer.
Happy Coding! 🤘