CSS + BEM Methodology = ?

We all follow naming conventions in programming languages, don’t we? Now, what about CSS? Is there any need of naming convention in that? Of course! Maybe not in a small project, but when you start working on large projects you should organize your cod…


This content originally appeared on DEV Community and was authored by Mohit Tanwani⚡

We all follow naming conventions in programming languages, don't we? Now, what about CSS? Is there any need of naming convention in that? Of course! Maybe not in a small project, but when you start working on large projects you should organize your code properly so that anyone can easily get the gist of the functionality by just looking into the code.

Great codebases don't need comments.

What is BEM?

BEM stands for Block Element Modifier. It is a Methodology that every web developer should follow. This makes development easy and enforces re-usability.
Enough of the theory Let's understand this by an example ?

image
Here We can say,

  • Form = Block(An Independent meaningful entity)
  • Input, Link, Button = Element(Part of Black, Which has no standalone meaning)
  • Button types (primary/secondary) = Modifier(Represent state of Block or Element.)

BEM Methodology

Let's continue with the above example. This is how you will name your CCS classes.

For Block:

.form {
   // Your CSS
}

For Element:

.form__input {
   // Your CSS
}
.form__button {
   // Your CSS
}
.form__link {
   // Your CSS
}

For Modifier:

.form__button--primary {
   background: "blue";
}
.form__button--secondary {
   background: "green";
}

NOTE: When using modifiers you will put all the common styles in form__button so that you don't repeat them again
And here is how your HTML will look like:

<form class="form">
   <input type="email" class="form__input" placeholder="Email">
   <input type="email" class="form__input" placeholder="Email">
   <button class="form__button form__button--primary">Login</button>
   <button class="form__button form__button--secondary">Signup</button>
</form>

That's it! I know it looks like an overhead but trusts me it will help you a lot in many different ways.


This content originally appeared on DEV Community and was authored by Mohit Tanwani⚡


Print Share Comment Cite Upload Translate
APA
Mohit Tanwani⚡ | Sciencx (2023-03-24T02:22:26+00:00) » CSS + BEM Methodology = ?. Retrieved from https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/.
MLA
" » CSS + BEM Methodology = ?." Mohit Tanwani⚡ | Sciencx - Tuesday July 20, 2021, https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/
HARVARD
Mohit Tanwani⚡ | Sciencx Tuesday July 20, 2021 » CSS + BEM Methodology = ?., viewed 2023-03-24T02:22:26+00:00,<https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/>
VANCOUVER
Mohit Tanwani⚡ | Sciencx - » CSS + BEM Methodology = ?. [Internet]. [Accessed 2023-03-24T02:22:26+00:00]. Available from: https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/
CHICAGO
" » CSS + BEM Methodology = ?." Mohit Tanwani⚡ | Sciencx - Accessed 2023-03-24T02:22:26+00:00. https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/
IEEE
" » CSS + BEM Methodology = ?." Mohit Tanwani⚡ | Sciencx [Online]. Available: https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/. [Accessed: 2023-03-24T02:22:26+00:00]
rf:citation
» CSS + BEM Methodology = ? | Mohit Tanwani⚡ | Sciencx | https://www.scien.cx/2021/07/20/css-bem-methodology-%f0%9f%a4%98/ | 2023-03-24T02:22:26+00:00
https://github.com/addpipe/simple-recorderjs-demo