DEV Community

Cover image for Mastering CSS with the BEM Naming Convention
waelhabbal
waelhabbal

Posted on

Mastering CSS with the BEM Naming Convention

The Block Element Modifier (BEM) convention is a popular naming convention for CSS classes. It was created by Yandex, a Russian search engine company, and has since gained widespread use in the web development community. BEM is a powerful tool for creating reusable, maintainable, and scalable code.

BEM Naming Convention

The BEM naming convention consists of three parts: Block, Element, and Modifier. A Block is a standalone component that can be reused throughout your codebase. An Element is a part of a Block that has no standalone meaning and is semantically tied to the Block. A Modifier is a variation of a Block or Element that changes its appearance or behavior.

Here's an example of how to apply BEM to HTML and CSS:

<div class="card">
  <div class="card__header">
    <h2 class="card__title">Title</h2>
  </div>
  <div class="card__body">
    <p class="card__text">Lorem ipsum dolor sit amet.</p>
  </div>
  <div class="card__footer card__footer--highlighted">
    <button class="card__button">Click me</button>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode
.card {
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 16px;
}

.card__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card__title {
  font-size: 24px;
  margin: 0;
}

.card__body {
  margin-top: 16px;
}

.card__text {
  font-size: 16px;
  margin: 0;
}

.card__footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-top: 16px;
}

.card__footer--highlighted {
  background-color: #f2f2f2;
}

.card__button {
  background-color: #007bff;
  border: none;
  color: #fff;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}
Enter fullscreen mode Exit fullscreen mode

In this example, the card class is the Block, and the card__header, card__body, and card__footer classes are Elements. The card__footer--highlighted class is a Modifier that changes the appearance of the Footer.

Using BEM makes it easy to create reusable code that can be easily maintained and scaled. By following this convention, you can avoid conflicts between different parts of your codebase, which can lead to a cleaner and more organized codebase.

Why Use BEM?

There are many benefits to using BEM in your projects. One of the main advantages is that it makes it easier to collaborate as a team. When everyone is following the same naming convention, it is easier to understand each other's code and work together more efficiently.

Another benefit of using BEM is that it makes your code more maintainable and scalable. By breaking down your code into Blocks, Elements, and Modifiers, you can create a more modular codebase that is easier to update and modify as your project grows.

How to Implement BEM in Your Projects

Implementing BEM in your projects is easy. Here are the steps to follow:

  1. Choose a Block name and add it to the parent element's class
  2. Choose an Element name and add it to the child element's class
  3. If you need to modify the Block or Element, choose a Modifier name and add it to the class

For example, let's say you're creating a navigation menu. You could use the following BEM naming convention:

<nav class="nav">
  <ul class="nav__list">
    <li class="nav__item">
      <a href="#" class="nav__link">Home</a>
    </li>
    <li class="nav__item nav__item--active">
      <a href="#" class="nav__link">About</a>
    </li>
    <li class="nav__item">
      <a href="#" class="nav__link">Contact</a>
    </li>
  </ul>
</nav>
Enter fullscreen mode Exit fullscreen mode

Here, the nav class is the Block, the nav__list, nav__item, and nav__link classes are Elements, and the nav__item--active class is a Modifier.

When implementing BEM in your projects, it's important to follow best practices. Here are some tips to keep in mind:

  • Use meaningful names for your Blocks, Elements, and Modifiers
  • Avoid using IDs in your CSS selectors
  • Use Sass or another CSS preprocessor to make it easier to write BEM code
  • Use nesting to make your code more organized and easier to read
  • Avoid using descendant selectors (e.g. .nav ul) as they can lead to performance issues

Conclusion

BEM is a powerful naming convention for CSS classes that can help you create reusable, maintainable, and scalable code. By following the Block Element Modifier structure, you can break down your code into smaller, more modular components that are easier to work with. When implementing BEM in your projects, be sure to follow best practices and use meaningful names for your Blocks, Elements, and Modifiers. With a little practice, you'll be able to write BEM code with ease and create more organized and efficient projects.

Top comments (0)