Why BEM?
hebaShakeel
Posted on October 21, 2020
What is BEM?
BEM stands for Block, Element, Modifier.It is a popular naming convention for classes in HTML and CSS. Its goal is to help developers better understand the relationship between the HTML and CSS in a given project.
Why should you use BEM?
CSS is a language that’s easy to learn but very hard to maintain. As the project grows larger, without proper structure, maintaining CSS is unbearable, hence we use the BEM methodology to make CSS maintainable.
Benefits of using BEM
BEM provides a modular structure to your CSS project. Because of its unique naming scheme, we won’t run into conflicts with other CSS names. BEM also provides a relationship between CSS and HTML. Ambiguous names are hard to maintain in the future.
Overall, BEM is my favourite CSS naming scheme, and I strongly suggest you try using it too!
It will save you SOOO much debugging time in the future.
Example
/ * Block Component * /
.btn{}
/ * Element that depends upon the block * /
.btn__price{}
/ * Modifier that changes the style of the block * /
.btn--orange {}
.btn--big {}
In this CSS methodology, a block is a top-level abstraction of a new component. This block can be thought of as a parent.
Elements, or child items are placed inside the block and can be denoted by two underscores following the name of the block.
Modifiers can modify the block so that we can style that particular component without infliction changes on a completely unrelated module. This is done by appending two hyphens to the block.
Another smart part of BEM is that everything is a class and nothing is nested. That makes CSS specificity very flat and low, which is a good idea.
BEM is extraordinarily useful for constructing scalable and maintainable interfaces where everyone on the team should have a clear idea of how things can be improved. This is because a great deal of front end development is not just about the nice tricks that solve one little problem in the short term; we need agreements, promises and binding social contracts between developers so that our codebase can adapt over time.
Thank You!
Posted on October 21, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.