What is BEM and Why we should use BEM?
When coding, have you ever thought about the method of naming the class? I used to give random class names, all kinds of names without following any rules.
And then, a brother in the profession introduced me to the CSS class naming method, which is BEM (Block Element Modifier) a simple naming convention.
👉You can see more about BEM here: https://css-tricks.com/bem-101/
What is BEM?
BEM is an acronym for Block Element Modifier which is a naming method for CSS classes, it is commonly used and relatively easy to understand.
Why use BEM?
Perhaps your first question will be why should you use BEM? and me too when I first approached BEM, I also asked myself the same question, I don't know if it's really helpful.
In fact, in small projects, BEM does not clearly show its role, until in large projects, having to work with the team, BEM gradually reveals its value as a lifeline. Specifically:
+ Help the team work together more easily, when working together, each person will have their own naming method that is prone to conflict with each other, BEM eliminates this problem when members comply when using
+ Calculate Modules: Classes will not be affected by other classes
+ Reusability: greatly reduce the amount of code
+ BEM creates a solid structure, simple to understand
Ingredients of BEM
Blocks: Block is the first class name in a new element. The main feature of blocks is their reuse anywhere on a page. And, these blocks can be inside other blocks. The block name is explicitly set by the element.
For example, I will name the class .btn to define the button Elements: Elements is a child element of a block, the name of the element will include the block name and be separated by a "__" sign.
In block .btn I create 2 elements .btn__name and .btn__counter Modifiers: Modifiers are used to change the display style of blocks or elements. Modifiers will be used as separate components. Modifiers names will include block or element names and are separated by "--"
With the initial .btn block, I used modilfiers to create 2 different variants, .btn--positive and .btn—negative
It will take some time to get used to the BEM, but it will be worth it once you understand its true value.
Through this post, hopefully you will have an overview of BEM and really apply BEM in upcoming projects XOXO















