DEV Community

Cover image for Web Theory - Part 6 : BEM, The Secret Formula for Mastering CSS Organization and Clarity
Mohammadreza Emamyari
Mohammadreza Emamyari

Posted on

Web Theory - Part 6 : BEM, The Secret Formula for Mastering CSS Organization and Clarity

The BEM Chronicles: Unraveling the Mysteries of CSS Naming with Flair

Welcome, adventurous coder, to the wondrous world of BEM (Block, Element, Modifier)! If you've ever felt like your CSS is a tangled mess of cryptic class names and unpredictable styles, BEM is here to rescue you with its clear, consistent naming conventions. Think of BEM as the secret decoder ring for your CSS, turning chaos into order with just a sprinkle of systematic magic. So, grab your magnifying glass and your favorite snack, and let’s dive into the thrilling saga of BEM!

What is BEM?

Picture a magical kingdom where every element of a webpage has a well-defined role and relationship. BEM, short for Block, Element, Modifier, is like the royal decree that dictates how to name and organize these elements to ensure harmony and clarity. Developed by the wizards at Yandex, BEM helps you structure your CSS in a way that's scalable and easy to understand.

The Three Key Components of BEM

In the land of BEM, there are three main characters you need to know:

  1. Block: The hero of the story, a standalone entity that can be reused anywhere. Think of it as a castle that stands on its own.
  2. Element: The trusty sidekick of the block, dependent on it and always working in tandem. Elements are like the rooms or towers within the castle.
  3. Modifier: The costume or special abilities that alter the block or element. Modifiers can make the castle look grander or give it a different color, just like magical enhancements.

Image description
(Image from e-accent)

The Block: The Hero of Our Tale

In the BEM universe, a block is a self-contained component with its own style and functionality. It’s like the main character in a story—a block can exist independently and can be placed anywhere in your webpage.

Example: The Brave Button Block

Imagine we’re creating a block for a button. The block class name should represent the button's core purpose, so we’ll call it .button.

<!-- HTML -->
<button class="button">Click Me!</button>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.button {
    background-color: #007bff;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
    transition: background-color 0.3s;
}

.button:hover {
    background-color: #0056b3;
}
Enter fullscreen mode Exit fullscreen mode

In this example, .button is a block that stands alone. It defines the core styles for our button, ensuring it looks good and functions properly on its own.

The Element: The Loyal Sidekick

Elements are the sidekicks that can only exist within their block. They are dependent on the block and cannot stand alone. Elements help define the internal structure of the block, like the components of a complex machine.

Example: The Button's Text Element

Let’s add a text element inside our button block. We’ll call this element .button__text.

<!-- HTML -->
<button class="button">
    <span class="button__text">Click Me!</span>
</button>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.button__text {
    font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

In this snippet, .button__text is an element of the .button block. It styles the text inside the button, ensuring that the text always looks bold and distinctive.

The Modifier: The Magical Enhancements

Modifiers are like magical enhancements or special costumes that change the appearance or behavior of a block or element. They help in creating variations of a block or element without altering the core structure.

Example: The Button’s Size Modifier

Let’s create a modifier for our button to make it larger. We’ll call this modifier .button--large.

<!-- HTML -->
<button class="button button--large">
    <span class="button__text">Click Me!</span>
</button>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.button--large {
    padding: 15px 30px;
    font-size: 20px;
}
Enter fullscreen mode Exit fullscreen mode

Here, .button--large is a modifier that adds extra padding and increases the font size of the button. By adding this modifier class to our button, we can easily create a larger version without changing the core .button block styles.

Putting It All Together: A BEM Adventure

Let’s go on an adventure and build a small component using BEM principles. Imagine we’re creating a card component for displaying user profiles. Our card will have a title, a profile picture, and a description.

The Card Block

We start with the .card block:

<!-- HTML -->
<div class="card">
    <img src="profile.jpg" alt="Profile Picture" class="card__image">
    <h2 class="card__title">John Doe</h2>
    <p class="card__description">Web Developer & Designer</p>
</div>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.card {
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    padding: 20px;
    background-color: white;
    max-width: 300px;
}

.card__image {
    border-radius: 50%;
    width: 100px;
    height: 100px;
    object-fit: cover;
    margin-bottom: 15px;
}

.card__title {
    font-size: 1.5em;
    margin: 0;
}

.card__description {
    font-size: 1em;
    color: #666;
}
Enter fullscreen mode Exit fullscreen mode

In this example, .card is our block, and .card__image, .card__title, and .card__description are elements within the block. The styles ensure that each part of the card is properly styled and laid out.

Adding Modifiers: The Personal Touch

Now let’s add a modifier to our card to create a featured version. We’ll call this modifier .card--featured.

<!-- HTML -->
<div class="card card--featured">
    <img src="profile.jpg" alt="Profile Picture" class="card__image">
    <h2 class="card__title">John Doe</h2>
    <p class="card__description">Web Developer & Designer</p>
</div>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.card--featured {
    border-color: #007bff;
    background-color: #f9f9f9;
}

.card--featured .card__title {
    color: #007bff;
}
Enter fullscreen mode Exit fullscreen mode

The .card--featured modifier adds a special border color and background color to the card, and it changes the title color. This modifier allows us to create a standout version of the card while keeping the core .card styles intact.

BEM in Action: Real-World Examples

To see BEM in action, let’s look at a real-world scenario. Suppose we’re building a navigation menu with multiple states.

The Navigation Menu Block

<!-- HTML -->
<nav class="nav">
    <ul class="nav__list">
        <li class="nav__item"><a href="#" class="nav__link">Home</a></li>
        <li class="nav__item"><a href="#" class="nav__link">About</a></li>
        <li class="nav__item"><a href="#" class="nav__link">Services</a></li>
        <li class="nav__item"><a href="#" class="nav__link">Contact</a></li>
    </ul>
</nav>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.nav {
    background-color: #333;
    padding: 10px;
}

.nav__list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
}

.nav__item {
    margin-right: 20px;
}

.nav__link {
    color: white;
    text-decoration: none;
    font-size: 16px;
}

.nav__link:hover {
    text-decoration: underline;
}
Enter fullscreen mode Exit fullscreen mode

Adding State Modifiers

Let’s add a modifier to indicate the active state of a menu item.

<!-- HTML -->
<li class="nav__item nav__item--active"><a href="#" class="nav__link">Home</a></li>
Enter fullscreen mode Exit fullscreen mode
/* CSS */
.nav__item--active .nav__link {
    font-weight: bold;
    color: #007bff;
}
Enter fullscreen mode Exit fullscreen mode

In this example, .nav__item--active is a modifier that changes the appearance of the active menu item, making it bold and blue. This helps users easily identify their current location in the navigation menu.

The BEM Benefits: Why You’ll Love It

Why should you embrace BEM with open arms? Here’s why:

  1. Clarity: BEM provides a clear and consistent naming convention, making your CSS easier to read and understand.
  2. Scalability: As your project grows, BEM’s modular approach ensures that your styles remain organized and manageable.
  3. Reusability: BEM’s blocks and elements can be reused across different parts of your site, reducing redundancy and simplifying maintenance.
  4. Flexibility: Modifiers allow you to create variations of blocks and elements without altering the core styles.

Conclusion: The BEM Odyssey

Congratulations, intrepid coder! You’ve journeyed through the thrilling world of BEM, mastering the art of naming and organizing CSS with finesse. By understanding the roles of

blocks, elements, and modifiers, you can transform your stylesheets into well-structured, scalable, and maintainable works of art.

With BEM by your side, you’re equipped to tackle even the most complex CSS challenges with confidence and style. So go forth, build amazing web designs, and let the magic of BEM guide you on your coding adventures!

Happy styling, and may your CSS always be clear and organized! 🌟🚀

🔗 You can also check ITCSS Methodology article for your next project!

🔗 SMACSS methodology is another great option!

🔗 if you are interested in CSS magical methodologies , You check my article about all CSS architectures.

Top comments (1)

Collapse
 
teclearn profile image
Mohammadreza Emamyari

hello guys, Thanks a lot for you support and attention
if you liked this article please give it a thumbs up and leave a comment!