Tom Ray
Published: January 14th 2020
Updated: January 14th 2020
This is a quick start guide to learning BEM, the component-driven CSS methodology.
If you want to start practicing and applying BEM to your projects, this guide will help you get started.
Ready? Let’s dive in:
BEM Overview
BEM (Block-Element-Modifier) is a CSS naming convention developed by the team at Yandex to improve scalability and maintainability in web development.
Put simply, the idea of BEM is to “divide the user interface into independent blocks” by naming CSS classes in the following methodology:
/* Block component */
.card {}
/* Elements are dependent on their parent block */
.card__img {}
/* Modifiers are for incremental style changes */
.card--dark {}
.card__img--large {}
- Block: an independent component that can be reused (e.g. with class name
.nav
) - Element: a child within a block that cannot be used separately from that block (e.g. with class name
.nav__item
) - Modifier: a variation in the style of either a block or modifier (e.g. with class name
.nav--dark
)
Let’s dive into some real CSS examples to get a hang of this thing.
Blocks
Blocks are reusable components. Like buttons, cards or form fields.
When naming your blocks, focus on describing its purpose (i.e. what it is) rather than its state (i.e. what it looks like).
For example, .btn
or .nav
follows the correct naming convention for a block.
.big
or .bright-pink
describes how it looks, so doesn’t scale well when you want to change the design later on.
If you’re wondering how to place blocks within blocks (for example, a button inside a nav), here’s a short article to help you with that.
Elements
Inside blocks are where elements live. Elements are dependent on their parent block, and so cannot be used without them.
Elements also have a unique CSS class naming convention which works like this:
.block__element
For example, using the .card
component, an element inside the card component (like an image) would have a class name like .card__img
.
The element name always appends the block name, separated by a double underscore __
.
It’s important to note that the second code snippet avoids using more than 1 selector to target the styles (e.g. like .card img {}
).
It’s considered best practice to use a BEM element class and use that directly instead (like .card__img {}
).
Following this approach reduces the chance of cascade issues down the line.
Modifiers
When you have varying styles in blocks (or elements), that’s where modifiers come in.
For example, your ‘card’ block might have a light and dark version. Or you might have primary and secondary buttons.
Modifiers have a unique CSS naming convention which works like this:
block--modifier
or block__element--modifier
.
That’s right- BEM modifiers can be applied to both blocks and elements.
Let’s dive into some bad and good practices:
It’s considered bad practice to use a modifier class in isolation (i.e. without the block or element class).
That’s because the modifier is meant to add incremental style changes to the block.
Therefore, whenever using a modifier, ensure it’s used with the base class:
And that’s it!
Those are the fundamentals to get you off and running with BEM.
If you’re interested to learn more about the ‘why’ behind BEM, I recommend checking out this CSS Tricks article.
Like learning anything new, practicing is key. Give BEM a shot in your next project and see where it takes you!
Download The Free BEM Cheat Sheet
Want to start practicing BEM and looking for a no-nonsense, quick start action guide?
Download a free cheat sheet covering BEM basics so you can dive in and start practicing today.
38 comments
Thank you for the auspicious writeup. It in reality was a enjoyment account it.
Glance advanced to more introduced agreeable from you! By the
way, how could we be in contact?
It’s an remarkable article in favor of all the internet people; they will obtain advantage from it I am sure.
Greetings! I know this is kind of off topic but I was wondering if you knew where I could locate a captcha plugin for
my comment form? I’m using the same blog platform as yours and I’m having difficulty finding
one? Thanks a lot!
Its like you read my mind! You seem to know a lot about this, like you
wrote the e book in it or something. I feel that you could do with some p.c.
to drive the message house a bit, however instead of
that, this is magnificent blog. A fantastic read.
I will definitely be back.
I was wondering if you ever thought of changing the layout of your website?
Its very well written; I love what youve got to say. But maybe you could
a little more in the way of content so people could
connect with it better. Youve got an awful lot of text for only having 1 or 2 pictures.
Maybe you could space it out better?
I’ll right away grasp your rss as I can not in finding your
e-mail subscription link or newsletter service.
Do you’ve any? Please let me understand in order that I may subscribe.
Thanks.
It’s not my first time to go to see this website, i am browsing this website dailly and take nice information from here every day.
Good web site you have got here.. It’s difficult to find high quality
writing like yours nowadays. I really appreciate people like you!
Take care!!
I was able to find good information from your content.
What a information of un-ambiguity and preserveness of precious
familiarity on the topic of unexpected feelings. quest
bars http://bit.ly/3jZgEA2 quest bars
Marvelous, what a webpage it is! This webpage gives valuable facts to us, keep it up.
asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
whoah this blog is excellent i really like reading your
articles. Keep up the good work! You know, lots of people are
looking round for this information, you can aid them greatly.
quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
Hello to all, how is the whole thing, I think every one is getting more
from this web site, and your views are pleasant designed for
new users. scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
whoah this blog is great i love reading your posts.
Stay up the great work! You understand, a lot of individuals are hunting round for
this info, you could help them greatly. cheap flights http://1704milesapart.tumblr.com/ cheap flights
This post offers clear idea in support of the new people of blogging,
that actually how to do blogging and site-building. ps4 https://bit.ly/3nkdKIi ps4 games
I’m not sure where you’re getting your info, but good topic.
I needs to spend some time learning more or understanding more.
Thanks for excellent info I was looking for this information for
my mission. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
It’s nearly impossible to find experienced people on this
subject, however, you seem like you know what you’re talking about!
Thanks
Heya i am for the primary time here. I found this board and I find It really useful & it helped me out a lot.
I hope to provide something back and help others like
you aided me.
I was recommended this website through my cousin. I’m now not
certain whether or not this submit is written by way of him as nobody else recognize such special approximately
my problem. You are wonderful! Thank you!
Hello, yeah this paragraph is truly fastidious and
I have learned lot of things from it about blogging.
thanks.
Hey there! I just wanted to ask if you ever have
any trouble with hackers? My last blog (wordpress) was hacked
and I ended up losing months of hard work due
to no data backup. Do you have any solutions to protect against hackers?
Hello there, I discovered your blog by means of Google at the same
time as searching for a related subject, your site came up, it appears
great. I have bookmarked it in my google bookmarks.
Hello there, simply changed into aware of your blog via Google, and found that it’s truly informative.
I am gonna be careful for brussels. I’ll be grateful if you happen to continue this
in future. Lots of other folks shall be benefited from
your writing. Cheers!
Your blog is one of a kind, i love the way you organize the topics.”:*,,
very nice post, i certainly enjoy this web site, persist with it
Hi there just wanted to give you a quick heads up.
The text in your article seem to be running off the screen in Safari.
I’m not sure if this is a formatting issue or something to do with
browser compatibility but I thought I’d post to let you know.
The design and style look great though! Hope you get the issue solved soon. Thanks
Hi to every one, it’s really a good for me to pay a quick visit this web site, it contains useful Information.
I love what you guys are up too. This type of clever work and reporting!
Keep up the excellent works guys I’ve included
you guys to my personal blogroll.
Simply want to say your article is as amazing.
The clearness on your submit is just spectacular and that i can think you’re
knowledgeable on this subject. Fine with your permission let me to seize
your RSS feed to stay updated with approaching post. Thank you
a million and please carry on the rewarding work.
Greetings from Colorado! I’m bored at work so I decided
to browse your site on my iphone during lunch break.
I love the info you provide here and can’t wait to take a look when I get
home. I’m surprised at how quick your blog loaded on my cell phone ..
I’m not even using WIFI, just 3G .. Anyways, fantastic blog!
hey there and thank you for your info – I’ve certainly picked up
anything new from right here. I did however expertise several technical issues using
this website, as I experienced to reload the site many times previous to I could get it to load correctly.
I had been wondering if your web host is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your quality score
if ads and marketing with Adwords. Well I’m adding this RSS
to my e-mail and could look out for a lot more of your respective exciting content.
Make sure you update this again very soon.
You made some really good points there. I looked on the net to find out more about the issue and found most individuals will go along with
your views on this website.
I think the admin of this site is really working hard in favor of his web page,
for the reason that here every material is quality based
material.