Why developers should use BEM with CSS

The advantages and importance of applying BEM to CSS

Michel de Groot
3 min readJan 23, 2023
image source: Coder Champ

BEM, which stands for “Block-Element-Modifier,” is a methodology for writing CSS that is designed to improve the maintainability and scalability of code by naming and organizing CSS classes in a way that makes it easy to understand the relationships between the different elements on a web page. By using BEM, developers can create more maintainable and scalable CSS code, which is especially important when working on large projects with multiple teams.

The core idea behind BEM is to break down the structure of a webpage into three distinct parts: blocks, elements, and modifiers. Blocks are the highest-level building blocks of a webpage, such as a header or a sidebar. Elements are parts of blocks, such as a logo within a header or a button within a sidebar. Modifiers are used to change the appearance or behavior of blocks or elements, such as a button that is in a hover state.

Image source: BEM

One of the main advantages of BEM is that it makes it easy to identify which CSS classes are related to which elements on a web page. This is because BEM uses a naming convention that includes the name of the block, the name of the element, and any modifiers that are applied to that element. For example, a CSS class for a button might be named “button,” while a class for a disabled version of that button might be named “button — disabled.” This makes it easy to understand the relationship between the different classes and elements, even when working with a large amount of code.

“CSS is like a box of chocolates on a hotday, you never know what you’re gonna get, but you know it’s gonna be messy.”

Another advantage of BEM is that it helps to keep CSS code organized and easy to maintain. By using a consistent naming convention, developers can easily find and update specific classes, even when working on a large project with multiple teams. This makes it much easier to make changes or fix bugs, which is especially important when working on a project with a tight deadline.

BEM promotes reusability

By breaking down a webpage into distinct blocks and elements, it becomes easier to reuse code across different parts of the site or web application. This can save time and effort when building and maintaining a webproject, as well as make it easier to apply consistent styling across different pages.

BEM also helps in avoiding naming conflicts. By using a naming convention (block__element — modifier) it helps to avoid naming conflicts and also make it easy to understand the structure of the codebase.

Because BEM uses a modular approach to naming and organizing CSS classes, it makes it easy to reuse code and create new elements that are consistent with the rest of the project. With blocks, elements, and modifiers clearly defined, it is simple to see how different parts of the code should be written. This can make it easier for developers to understand how different parts of the codebase work together, and can make it easier to maintain and scale the codebase over time. This helps to prevent code bloat and makes it easier to add new features or make changes to existing ones.

In conclusion, BEM is a powerful methodology for naming and organizing CSS classes and it has many advantages over conventional CSS methodologies. By breaking down a webpage into distinct blocks, elements, and modifiers, it makes it easier to understand the relationships between different parts of the code, promotes reusability, avoids naming conflicts, and creates clear and consistent code that imporves scalabilty. This can save time and effort when building and maintaining a web application, as well as make it easier to apply consistent styling across different pages.

--

--

Michel de Groot

I'm a creative front-end developer with a passion for UI/UX