Taming the CSS Chaos: A Developer's Guide to BEM - Organize, Collaborate, and Conquer

Hey there, fellow "Idan" developer 👋🏾 Have you ever found yourself in a coding nightmare, battling a bug that seems to have a mind of its own? I have! Just last week, I was knee-deep in an intricate web of CSS code, desperately trying to debug a styling issue. Luckily, a colleague came to my rescue, introducing me to a lifesaver called Block, Element, Modifier (BEM). Let me share my experience and explain why BEM could be your secret weapon in such situations.

Picture this: I was working on a website I designed for coding practice(here's the Figma link). Everything seemed fine until I finished styling the contact section. The more I tried to make it align with the Figma design, the more chaotic my CSS became. Selectors piled up like a tangled mess, and I couldn't figure out which styles were affecting what elements. It was a classic case of CSS confusion, and the bug was eluding me.

That's when my colleague, let's call them Idan, noticed my struggle and offered their assistance. They asked me if I had heard of BEM—a system that could bring order to the chaos I was facing. I admitted I hadn't, but I was willing to give it a shot.

Idan explained BEM to me in a way that even my tired brain could comprehend. They said, "Think of BEM as a naming convention that helps you structure your CSS in a logical and modular manner. It breaks down your code into blocks, elements, and modifiers."

As they spoke, it clicked. BEM was a way to organize my CSS and make it more manageable! Motivated and armed with the knowledge of BEM, I started refactoring my HTML & CSS. Instead of random classes and unclear relationships, I began using BEM's naming conventions. Each block, element, and modifier had its designated class, making it easier to understand and maintain. The bug turned out to be a conflicting style applied to an element 😭

What is BEM?

So, what exactly is BEM? Think of it as a system of naming conventions that helps you structure your CSS in a logical and modular manner. It promotes consistency, scalability, and maintainability in your codebase. Let's dive into the three building blocks of BEM:

  1. Block: A block is a standalone component on your webpage. Examples could include a navigation bar, a search form, or a product card. Blocks are typically the largest piece of code you'll write and are named after the function they serve on the page.

  2. Element: An element is a part of a block that performs a specific function. Elements are always contained within a block and are named using two underscores followed by a descriptive name. For example, a search form block might contain elements like "input__text" and "input__submit".

  3. Modifier: A modifier is a variation of a block or element. Modifiers allow you to change the appearance or behavior of a block or element without having to write entirely new code. Modifiers are named using two hyphens followed by a descriptive name. For example, a product card block might have a modifier called "product--highlighted" that adds a special border or background color to the card.

Why should you care about BEM?

Now that you understand the basic principles of BEM, you might wonder why you should consider adopting it. Well, my friend, here are a few compelling reasons:

  1. Organization: BEM brings order to chaos. By explicitly defining the structure of your components, you'll have a clear understanding of how everything fits together. No more digging through mountains of code trying to decipher the relationships between different styles.

  2. Reusability: Blocks and elements are designed to be reusable. Once you create a component, you can easily drop it into different parts of your website without worrying about conflicts or unexpected side effects. This saves time and effort in the long run.

  3. Collaboration: BEM promotes collaboration among developers. Since everyone follows the same naming conventions, it becomes easier for multiple people to work on the same codebase. Plus, new team members can quickly grasp the structure and purpose of components, speeding up their onboarding process.

One possible concern you might have about using BEM is that it can lead to longer class names. This is true, but it's a small price to pay for the benefits of a more organized codebase. Additionally, many developers use tools like preprocessors or automation scripts to generate BEM class names automatically, so you don't have to type them out by hand.

Let's take a look at some examples of BEM in action. Here's an example of a navigation bar component using BEM:

<nav class="navigation">
  <ul class="navigation__list">
    <li class="navigation__item"><a href="#"                  class="navigation__link">Home</a></li>
    <li class="navigation__item"><a href="#" class="navigation__link">About</a></li>
    <li class="navigation__item navigation__item--active"><a href="#" class="navigation__link">Services</a></li>
    <li class="navigation__item"><a href="#" class="navigation__link">Contact</a></li>
  </ul>
</nav>

In this example, "navigation" is the block, and "list", "item", and "link" are elements. "active" is a modifier that changes the appearance of the active link.

.navigation {
  background-color: #333;
  padding: 10px;
}

.navigation__list {
  list-style: none;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 0;
}

.navigation__item {
  margin: 0 10px;
}

.navigation__link {
  color: #fff;
  text-decoration: none;
}

.navigation__item--active .navigation__link {
  font-weight: bold;
}

By following the BEM approach, we can easily identify and style different components of our website, making our code more organized and readable.

If you're tired of fighting battles in a tangled CSS jungle, it's time to consider embracing BEM. Let the blocks, elements, and modifiers be your guiding light toward cleaner code and more efficient debugging. Trust me, your future self—and your colleagues—will thank you for it.

Happy coding with BEM, and may your bug battles be swift and victorious!

4
Subscribe to my newsletter

Read articles from Chinonye. M. Onuoha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chinonye. M. Onuoha
Chinonye. M. Onuoha

Annyeong 👋🏾 I'm an optimized solution seeker with a passion for building better user experiences. With experience in UX design/research in FinTech & eCommerce, I bring a unique perspective to problem-solving and love finding creative solutions to difficult challenges. My goal is to shape products that have a positive impact on users, whether that means making them more user-friendly, more efficient, or more accessible to a wider audience. When I'm not buried in UX design problems, you can find me fumbling my way through coding projects. But hey, at least I never give up! 😁 Bonus Fun Fact: I am a little obsessed with K-drama 🎭