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
- Block + Element
- Block + Element + Modifier
- When an element contains another element
- A simple list
- User image (userpic)
- Gallery
- Navigation (simple version)
- Navigation (complex version)
- Sidebar widget
- News block
- Article or blog post (simple version)
- Article or blog post (complex version)
- Page layout
- Common words used in CSS class names
- Images
- Text
- Lists
- Blocks
- Layout
- Controls
- Media queries
- Sizes
- Miscellaneous
- States
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>
Gallery
<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>
Navigation (simple version)
<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>
Navigation (complex version)
<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>
Sidebar widget
<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— imageicon— iconlogo— logouserpic,avatar— userpic, a small user imagethumbnail,thumb— thumbnail, a smaller version of an image
Text
title,subject,heading,headline,caption— headingsubtitle— subheadingslogan— sloganlead,tagline— introductory text or taglinetext— text contentdesc— description; another option for text contentexcerpt— a short extract, usually placed before a “Read more...” linkquote,blockquote— quotationsnippet— code examplelink— linkcopyright,copy— copyright notice
Lists
list,items— listitem— list item
Blocks
page— root page elementheader— 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 contentsidebar— sidebar (of a page or element)aside— block with additional informationwidget— widget, for example, in a sidebar
Layout
wrapper,wrap— wrapper, usually an outer oneinner— inner wrappercontainer,holder,box— containergrid— a grid-based page or element layout (usually containingrowandcol)row— row containercol,column— column container
Controls
button,btn— button, for example, a form submit buttoncontrol— a control, such as the “Next/Previous” arrows in a photo gallery or slider controlsdropdown— dropdown list
Media queries
phone,mobile— mobile devicesphablet— phones with large screens (6–7 in.)tablet— tabletsnotebook,laptop— laptopsdesktop— desktop computers
Sizes
tiny,xs— tinysmall,sm— smallmedium,base— mediumbig,large,lg— largehuge,xl— hugenarrow— narrowwide— wide
Miscellaneous
search— searchsocials— social media icon blockadvertisement,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 featuresslider,carousel— slider, an interactive element that scrolls through contentpagination— page navigationuser,author— user, post author, or comment authormeta— block with additional information, such as post tags and datecart,basket— shopping cartbreadcrumbs— breadcrumb navigationmore,all— link to the full informationmodal— modal (dialog) windowpopup— popup windowtooltip,tip— popup hintpreview— preview or excerpt, such as a news item or post; it may include a heading, description, and image, and usually links to the full versionoverlay— overlay layer, for example, to darken images or create modal windows
States
active,current— active element, such as the current menu itemvisible— visible elementhidden— hidden elementerror— error statewarning— warning statesuccess— task completed successfullypending— waiting state, for example, before switching toerrororsuccess
