1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Education
  6. >
  7. What is BEM naming convention for CSS: A Quick Explaination

What is BEM naming convention for CSS: A Quick Explaination

Available In Following Subtitles
English
Variant 1
Posted on:
Video by: Kevin Powell
Learn why BEM (Block Element Modifier) is a popular CSS naming convention for organizing code. Understand the use of double underscores and hyphens within the BEM structure in under 5 minutes.
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:00
So I get asked a lot about when I'm writing my CSS what the deal is with the double underscores and the double hyphens and stuff like that.
0:07
It's part of something called BEM which is Block Element Modifier and that's what this video is going to be all about.
0:13
But it's a Friday so that means we're going to be doing it in under 5 minutes.
0:22
So BEM stands for Block Element Modifier and it's a CSS naming convention that has a really high adoption rate.
0:28
So BEM again, Block Element Modifier, so in this case we're starting with the block and that would be my card.
0:33
So my class is just card just like that and I have two different cards. I have a light card and a dark card
0:39
So we have block and so my block is the card then we have elements inside of that
0:45
So in this case, I have an image. I have a title. I have my body text
0:48
So the double underscore here is when we're going into the elements that are part of that card
0:53
So my card double underscore image double underscore title double underscore body and I'm repeating those down here as well
0:59
So those are
1:00
are the elements that are part of my card block.
1:04
And then we also have modifiers,
1:06
so block element modifier.
1:08
And a modifier is something that instead of being
1:10
inside of my block is modifying that thing.
1:14
So this one would probably have the box shadow
1:16
that we see on both of these as well as the padding.
1:19
But that's about all that's similar.
1:21
Display flex or something like that maybe.
1:23
And then I'm gonna use my modifier to set the colors
1:25
that are going to be showing up on the two of them.
1:27
So I'm setting that that's going to give my background color and the color of my text. So this modifier is modifying the original
1:35
CSS so double underscore
1:38
Denotes the element that lives inside of my block and a double hyphen denotes a modification to that block
1:44
So to see why this is even worth bothering about let's go look at some other examples here
1:49
So in this first example, this is how most people start writing CSS and it leads to a ton of extra work on their part
1:56
I have a simple class for each card, I have a card light and a card dark here.
2:00
one completely just gets its own style. So card light and
2:04
card dark. So here if I wanted to change something on it, so
2:06
say I change the padding on this one, it's changing here, but
2:10
that card is not changing. So then I have to come over onto
2:12
this card and I have to change this one as well. And that's
2:15
just really annoying to have to go back and forth between the
2:18
two is a really basic example. We don't want to have to modify
2:21
it twice with BEM. I have a block class giving me the shared
2:25
styles for all my cards and a modifier class. So that's what
2:28
I've done here on this example I have my card and my card light and then my card
2:33
and my card dark. So if we look at the CSS we can see here my card has the
2:37
margin has a width padding and box shadow so if I change the padding here
2:42
both cards are going to get the change at the same time and if I look at my
2:45
modifiers this is where my background colors are coming in don't really need
2:50
that margin there but my modifier is changing the background color and the
2:54
color of my text inside of it.
2:57
The element classes, some people, I have div in my image.
3:00
my h2, my paragraph, but it can be worth having your element classes as well. So
3:04
if I come and I look over at example 3 here, they're useful for preventing
3:11
specificity wars, and I can never say that word, I know I did too many
3:13
specificities, but that's just how I say it because I can't pronounce it, but to
3:18
try and avoid running into wars like that, it's useful to give everything
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome