DEV Community

Cover image for Want to Master BEM CSS Like a Pro? Check out this Ultimate Guide!
Jaimal Dullat
Jaimal Dullat

Posted on • Originally published at jaimaldullat.Medium

Want to Master BEM CSS Like a Pro? Check out this Ultimate Guide!

Ever felt lost in a maze of CSS classes, unsure of where one style ends and another begins? You’re not alone. Many developers have faced the daunting task of untangling CSS, only to wish for a magic spell to make everything orderly.

Enter BEM: the spellbook for writing cleaner, more readable class names in HTML and CSS. It’s not just a methodology; it’s a mindset shift. Dive in, and discover how BEM can transform chaos into clarity, and make your next project a breeze.

“In the world of CSS, design is an art, but naming conventions? That’s pure science.”


What is BEM?

BEM stands for Block, Element, Modifier. It’s a naming convention for writing cleaner and more readable class names in HTML and CSS. The main idea behind BEM is to divide the user interface into independent blocks, making it easier to maintain and scale your code.

  • Block: A standalone entity that is meaningful on its own. E.g., header, container, menu.
  • Element: A part of a block that has no standalone meaning and is semantically tied to its block. E.g., menu__item, header__logo.
  • Modifier: A flag on a block or element used to change appearance or behaviour. E.g., button--large, menu__item--active.

Why Use BEM?

  1. Modularity: BEM promotes the creation of independent blocks, making it easier to reuse code across different parts of your project or even different projects.
  2. Clarity: With BEM, developers can easily discern the relationship between the HTML and CSS, reducing the learning curve for team members.
  3. Avoids specificity wars: Since BEM avoids nesting and relies on class names, you won’t find yourself battling with overly specific selectors.

How to Implement BEM

  1. Start with Blocks: Identify standalone entities in your design. These will be your blocks. For example, a navigation bar can be a block named nav.
  2. Identify Elements: Look within your blocks to find elements. Using our nav example, individual links would be elements. They can be named nav__item.
  3. Add Modifiers as Needed: If you have variations of a block or element, use a modifier. For instance, if one of the navigation links is the current page and you want to style it differently, you could use a modifier like nav__item--active.

BEM in Practice

Here’s a simple example to illustrate BEM:

File: index.html

<!-- Block -->
<nav class="nav">

  <!-- Element -->
  <a href="#" class="nav__item">Home</a>

  <!-- Element with Modifier -->
  <a href="#" class="nav__item nav__item--active">About</a>

  <a href="#" class="nav__item">Contact</a>
</nav>
Enter fullscreen mode Exit fullscreen mode
  • Block (<nav class="nav">): This is the main container or parent component, which in this case is a navigation bar. It's given the class name nav, representing the block in BEM.
  • Element (<a href="#" class="nav__item">): These are the individual links within the navigation bar. They are given the class name nav__item, where nav is the block name and item is the element name. The double underscores (__) indicate that item is an element of the nav block.
  • Element with Modifier (<a href="#" class="nav__item nav__item--active">): This is also an individual link but with an additional class nav__item--active. The double hyphens (--) indicate a modifier. This modifier represents a variation of the nav__item element, which in this case is an active or currently selected link.

File: style.css

/* Block */
.nav {
  background-color: #333;
  padding: 1rem;
}

/* Element */
.nav__item {
  color: white;
  text-decoration: none;
  margin-right: 1rem;
}

/* Modifier */
.nav__item--active {
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode
  • Block (.nav): This CSS rule targets the nav block. It sets a dark background color (#333) and a padding of 1rem around the navigation bar.
  • Element (.nav__item): This rule targets the nav__item elements, which are the individual links. It sets the text color to white, removes any underlines (text-decoration: none), and adds a margin to the right of each link (margin-right: 1rem).
  • Modifier (.nav__item--active): This rule targets the nav__item element with the --active modifier. It makes the font weight bold, indicating that this particular link is the active or currently selected link.

Tips for Using BEM

  1. Stay Consistent: Once you decide to use BEM, stick to its conventions throughout your project.
  2. Avoid Deep Nesting: The beauty of BEM is its flat structure. Avoid the temptation to nest elements within elements.
  3. Use Tools: There are pre-processors like SASS and LESS that can make working with BEM even more efficient.

Conclusion

BEM is a powerful methodology that can bring clarity, consistency, and modularity to your CSS. While it might seem verbose at first, the benefits in terms of maintainability and scalability are immense. Give BEM a try in your next project and experience the difference for yourself!

🔥 Wait! 🔥
Craving more insights like these? 🧠 Follow me and fuel that curiosity. And if this lit a spark? 👏 Like to keep the flame alive!

Top comments (0)