BEM CSS Naming Convention: Practical Examples

Hi! This is my first blog post, and I’ve decided to dedicate it to a handy guide to BEM class naming, complete with practical block examples.

BEM class names

Examples of blocks using BEM classes.

Block + Element

Imagine a block containing a heading, some text, and a button—for example, a popup. Here’s the HTML markup:

<div>
  <h3>Heading</h3>
  <div>Text</div>
  <button>Button</button>
</div>

First, add a class that describes the element’s purpose: .popup.

<div class="popup">
  <h3>Heading</h3>
  <div>Text</div>
  <button>Button</button>
</div>

Now let’s add classes to the nested elements:

<div class="popup">
  <h3 class="title">Heading</h3>
  <div class="text">Text</div>
  <button class="button">Button</button>
</div>

These classes are convenient, but they aren’t unique. If the page has other elements with .title and .text classes, their styles could affect elements in the footer, too. A selector such as .popup .title can also create specificity issues down the road. You could come up with different class names, but the more elements with similar meanings you have, the harder it becomes to keep inventing new ones.

Now let’s apply BEM notation: add the parent block’s class as a prefix to each element inside the block. For example, the heading becomes popup__title:

<div class="popup">
  <h3 class="popup__title">Heading</h3>
  <div class="popup__text">Text</div>
  <button class="popup__button">Button</button>
</div>

These classes solve two problems at once. First, because each class is unique, its styles won’t clash with those of similar elements elsewhere on the page. Second, the class name immediately tells you that the element belongs to the .popup block.

Block + Element + Modifier

Let’s use a notification message as an example. These messages usually come in different variants—for example, a success message or an error message.

<div class="message">
  <h3 class="message__title">Message heading</h3>
  <div class="message__text">Message text</div>
</div>

It makes sense to use the same markup while giving each variant a different color scheme. That’s where modifiers come in.

<div class="message message--success">
  <h3 class="message__title">Message heading</h3>
  <div class="message__text">Message text</div>
</div>

<div class="message message--error">
  <h3 class="message__title">Message heading</h3>
  <div class="message__text">Message text</div>
</div>

You can apply the same styles to both elements using the shared .message class. Then, use each modifier class to add variant-specific styles:

.message {
  border: 1px solid gray;
}
.message--success {
  border-color: green;
}
.message--error {
  border-color: red;
}

Both messages will have a one-pixel border. The success message will have a green border, while the error message will have a red one.

When an element contains another element

For example, a page might have a news block:

<div class="news">
    <h3>News</h3>

    <ul>
      <li><!-- news item --></li>
      <li><!-- news item --></li>
    </ul>
</div>

The block heading can use the .news__title class, the list .news__list, and each news item .news__item:

<div class="news">
    <h3 class="news__title">News</h3>

    <ul class="news__list">
      <li class="news__item"><!-- news item --></li>
      <li class="news__item"><!-- news item --></li>
    </ul>
</div>

So far, so good. Now let’s add the markup for an individual news item:

<div class="news">
    <h3 class="news__title">News</h3>

    <ul class="news__list">
      <li class="news__item">
        <h4>News headline</h4>
        <p>News text</p>
      </li>
      <li class="news__item"><!-- news item --></li>
    </ul>
</div>

We need to add a class to the news headline. The first option that comes to mind is .news__title, but that class is already taken. Suppose the second element uses .subject instead of .title. The CSS would then look like this:

.news__title { ... }
.news__subject { ... }

Without extra comments, it’s impossible to tell which class belongs to the heading for the whole block and which belongs to an individual news item. That’s not ideal.

Another option is .news__item__title, but BEM doesn’t allow elements to be nested inside other elements—and for good reason: the result quickly becomes a tangled mess. .news__item-title isn’t a great choice either, because the relationship between title and item isn’t obvious. So what’s the solution?

It’s simple: at the .news__item level, declare a new block (for example, .news-item) and build nested class names from there. It doesn’t have to be a standalone, reusable block. In this case, declaring a block simply gives the selectors their own namespace. Here’s what that looks like:

<div class="news">
    <h3 class="news__title">News</h3>

    <ul class="news__list">
      <li class="news__item news-item">
        <h4 class="news-item__title">News headline</h4>
        <p class="news-item__text">News text</p>
      </li>
      <li class="news__item"><!-- news item --></li>
    </ul>
</div>

Problem solved: there’s no need for unwieldy class names, and each class still describes its element clearly. The CSS also makes it obvious which class is responsible for what:

.news__title { ... }
.news-item__title { ... }

A simple, practical way out of an awkward situation.

A simple list

<ul class="list">
  <li class="item">First</li>
  <li class="item">Second</li>
  <li class="item">Third</li>
</ul>

User image (userpic)

<div class="user">
  <img class="user__img" src="userpic.png" alt="Mykhailo Petrov">
  <a class="user__link" href="#">Mykhailo Petrov</a>
</div>
<div class="gallery gallery--slider">
  <ul class="gallery__list">
    <li class="gallery__item">
      <img class="gallery__img" src="flowers.jpg" alt="Beautiful flowers">
    </li>
    <li class="gallery__item">
      <img class="gallery__img" src="trees.jpg" alt="A beautiful park">
    </li>
  </ul>
</div>
<nav class="nav">
  <a class="nav__link nav__link--active">Home</a>
  <a class="nav__link" href="#">Second</a>
  <a class="nav__link" href="#">Third</a>
  <a class="nav__link" href="#">Fourth</a>
  <a class="nav__link" href="#">Last</a>
</nav>
<nav class="nav">
  <ul class="nav__list">
    <li class="nav__item nav__item--current">
      <a class="nav__link">Home</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#">Articles</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#">Photo gallery</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#">Contact</a>
    </li>
  </ul>
</nav>
<div class="widget">
  <h4 class="widget__tilte">How to grow jelly</h4>

  <div class="widget__content">
    <p>To grow a sociable, friendly jelly, you’ll need a roll of foam,
     two kilos of sugar, three eggs, and half a teacup of acetone.</p>

    <a class="widget__link" href="#">Don’t read more...</a>
  </div>
</div>

News block

<div class="news">
    <h3 class="news__title">Yesterday’s news</h3>

    <ul class="news__list">
        <!-- Add the block class to the element class to create a new namespace -->
        <li class="news__item item-news">
            <h4 class="item-news__title">
              Swimming competition among fish
            </h4>
            <div class="item-news__text">
              <p>The pike team from Kyiv took first place.</p>

              <a href="#" class="item-news__link">Read more</a>
            </div>
        </li>

        <li class="news__item item-news">
            <h4 class="item-news__title">
              Scientists discover a UFO
            </h4>
            <div class="item-news__text">
              <p>Ukrainian scientists unearthed an unidentified flying object made of plastic in the center of Lviv.</p>

              <a href="#" class="item-news__link">Don’t read more</a>
            </div>
        </li>
    </ul>
</div>

Article or blog post (simple version)

<article class="article">
  <h3 class="article__title">
    Finding the chakras in a bunch of parsley
  </h3>
  <time class="article__datetime">May 32, 10:87</time>

  <div class="article__author author-article">
    <img class="author-article__img" src="userpic.png" alt="Claw Andriivna">
    <a class="author-article__link" href="#">Claw Andriivna Longarm</a>
    <div class="author-article__desc">Our chakra expert</div>
  </div>

  <div class="article__content">
    Go to the market and buy a 100-gram bunch of parsley from one of the vendors.
    Sort it carefully and remove any beetles and caterpillars. Give the beetles to the cat,
    and move the caterpillars into a pot with cacti. Name one John and the other Billy,
    and now you’ve got the Wild West in your flowerpot. Return to the parsley.
    Give it a kind look and scratch behind its ear—or your own, or the cat’s.
    Tie it with a satin ribbon and, naturally, finish with a bow. Congratulations!
    You now have a fully domesticated bunch of parsley that will happily follow you
    around and sprout seeds in your slippers.
  </div>
</article>

Article or blog post (complex version)

<article class="entry">
  <header class="entry__header">
    <h3 class="entry__title title-entry">
      <a class="title-entry__link" href="#">
        Rubber ducks as a path to self-discovery
      </a>
    </h3>

    <time class="entry__datetime">May 32, 10:87</time>
  </header>

  <div class="entry__author author-entry">
    <img class="author-entry__img" src="userpic.png" alt="Vasyl Petrovych">

    <a class="author-entry__link" href="#">Vasyl Petrovych Holoborodko</a>
  </div>

  <div class="entry__content">
    Take the box of fifty rubber ducks left over from New Year’s Eve down from the attic.
     Arrange the ducks and candles on the floor in the shape of a pentagram.
     Sit in the middle in the lotus position, take a German–Ukrainian dictionary
     in each hand, clear your throat, take a deep breath, and say “Quack!”
     loudly, confidently, and with complete commitment.
  </div>

  <div class="entry__tags tags-entry">
    <h4 class="tags-entry__tilte">Tags</h4>

    <ul class="tags-entry__list">
      <li class="tags-entry__item">
        <a class="tags-entry__link" href="#">DIY round dance</a>
      </li>
      <li class="tags-entry__item">
        <a class="tags-entry__link" href="#">porcelain slippers</a>
      </li>
      <li class="tags-entry__item">
        <a class="tags-entry__link" href="#">shoe polish in cooking</a>
      </li>
    </ul>
  </div>

  <menu class="entry__menu menu-entry">
    <ul class="menu-entry__list">
      <li class="menu-entry__item menu-entry__item--read">
        <a class="menu-entry__link" href="#">238 replies</a>
      </li>
      <li class="menu-entry__item menu-entry__item--write">
        <a class="menu-entry__link" href="#">Write to the lottery</a>
      </li>
      <li class="menu-entry__item menu-entry__item--share">
        <a class="menu-entry__link" href="#">Share</a>
      </li>
    </ul>
  </menu>
</article>

Page layout

<!-- Site header -->
<header class="site-header">
  <h1 class="site-header__title">Site name</h1>

  <nav class="site-header__nav site-nav">
    <a href="#" class="site-nav__link">About the product</a>
    <a href="#" class="site-nav__link">About the weather</a>
    <a href="#" class="site-nav__link">What the reeds are rustling about</a>
  </nav>
</header>

<!-- Main page content -->
<main class="site-main">
  <!-- Main content sections -->
  <section class="features">
    <h2 class="features__title">Features</h2>

    <ul class="features__list">
      <li class="features__item">High ceilings</li>
      [...]
    </ul>
  </section>

  <section class="prices">
    <h2 class="prices__title">Pricing</h2>

    <ul class="prices__list">
      <li class="prices__item">30 umbrellas per minute</li>
      [...]
    </ul>
  </section>
</main>

<!-- Sidebar -->
<aside class="site-aside">
  <div class="widget">
    <h3 class="widget__title">How do we relax?</h3>
    <div class="widget__text">Find out in our new book, “Not at All”</div>
  </div>
</aside>

<!-- Site footer -->
<footer class="site-footer">
  <div class="site-footer__socials socials">
    <a href="#" class="socials__link socials__link--tw">Twitter</a>
    <a href="#" class="socials__link socials__link--fb">Facebook</a>
    <a href="#" class="socials__link socials__link--tl">Telegram</a>
  </div>

  <div class="site-footer__copyright copyright ">
    Made with a <a href="#" class="copyright__link">neighbor’s hammer</a>
  </div>
</footer>

Common words used in CSS class names

A good practice is to name CSS classes in English.

Images

  • image, img, picture, pic — image
  • icon — icon
  • logo — logo
  • userpic, avatar — userpic, a small user image
  • thumbnail, thumb — thumbnail, a smaller version of an image

Text

  • title, subject, heading, headline, caption — heading
  • subtitle — subheading
  • slogan — slogan
  • lead, tagline — introductory text or tagline
  • text — text content
  • desc — description; another option for text content
  • excerpt — a short extract, usually placed before a “Read more...” link
  • quote, blockquote — quotation
  • snippet — code example
  • link — link
  • copyright, copy — copyright notice

Lists

  • list, items — list
  • item — list item

Blocks

  • page — root page element
  • header — header (of a page or element)
  • footer — footer (of a page or element)
  • section — content section (one or more)
  • main, body — main part (of a page or element)
  • content — element content
  • sidebar — sidebar (of a page or element)
  • aside — block with additional information
  • widget — widget, for example, in a sidebar

Layout

  • wrapper, wrap — wrapper, usually an outer one
  • inner — inner wrapper
  • container, holder, box — container
  • grid — a grid-based page or element layout (usually containing row and col)
  • row — row container
  • col, column — column container

Controls

  • button, btn — button, for example, a form submit button
  • control — a control, such as the “Next/Previous” arrows in a photo gallery or slider controls
  • dropdown — dropdown list

Media queries

  • phone, mobile — mobile devices
  • phablet — phones with large screens (6–7 in.)
  • tablet — tablets
  • notebook, laptop — laptops
  • desktop — desktop computers

Sizes

  • tiny, xs — tiny
  • small, sm — small
  • medium, base — medium
  • big, large, lg — large
  • huge, xl — huge
  • narrow — narrow
  • wide — wide

Miscellaneous

  • search — search
  • socials — social media icon block
  • advertisement, adv, commercial, promo — advertising block (AdBlock may hide it, so avoid using these class names for in-house ads)
  • features, benefits — list of a product’s or service’s key features
  • slider, carousel — slider, an interactive element that scrolls through content
  • pagination — page navigation
  • user, author — user, post author, or comment author
  • meta — block with additional information, such as post tags and date
  • cart, basket — shopping cart
  • breadcrumbs — breadcrumb navigation
  • more, all — link to the full information
  • modal — modal (dialog) window
  • popup — popup window
  • tooltip, tip — popup hint
  • preview — preview or excerpt, such as a news item or post; it may include a heading, description, and image, and usually links to the full version
  • overlay — overlay layer, for example, to darken images or create modal windows

States

  • active, current — active element, such as the current menu item
  • visible — visible element
  • hidden — hidden element
  • error — error state
  • warning — warning state
  • success — task completed successfully
  • pending — waiting state, for example, before switching to error or success
Mykhailo Petrov
Mykhailo Petrov

Full-cycle WordPress website development, improvements, new features, bug fixes, maintenance, updates and migration to another domain or hosting.