I recently heard about BEM methodology.
BEM names intentionally use double underscores and double hyphens instead of single to separate Block-Element-Modifier. The reason is so that single hyphens can be used as word separators. Class names should be very readable, so abbreviation isn't always desirable unless the abbreviations are universally recognizable.
BEM stands for Block Element Modifier. The main idea behind it is to speed up the development process, and ease the teamwork of developers by arranging CSS classes into independent modules.
BEM — is a methodology that helps you to create reusable components and code sharing in front‑end development.
BEM stands for Block, Element, Modifier and is a popular semantic method to create naming classes for your HTML DOM objects. It takes some of the work out of selecting appropriate class names (one of the most difficult tasks a developer faces). BEM is a modular-component-based approach to building websites.
BEM is a naming methodology. It stands for Block Element Modifier and it's aim is to facilitate modularity and make styles and classes a lot easier to maintain.
It's not easy to explain it in a reply, but I can give you the basic idea.
So if you're thinking of a menu, then the whole element / component structure would be:
Block: menu Element: item Modifier(s): horizontal, vertical
And by following standard BEM conventions, you would write something like this in CSS:
html,
body {
height: 100%;
}
.menu {
display: block;
}
.menu__item {
display: inline-block;
line-height: 30px;
width: 100px;
}
.menu--horizontal {
width: 100%;
height: 30px;
}
.menu--vertical {
width: 100px;
height: 100%;
}
.menu--horizontal .menu__item {
border-right: 1px solid #e5e5e5;
text-align: center;
}
.menu--vertical .menu__item {
border-bottom: 1px solid #e5e5e5;
text-align: left;
}
<div class="menu menu--horizontal">
<div class="menu__item">Home</div>
<div class="menu__item">About</div>
<div class="menu__item">Contact</div>
</div>
As you see from the code, the elements are separated from the block with 2 underscores and modifiers are separated with 2 dashes.
Now if you change the modifier on the menu from --horizontal
to --vertical
, the specific modifier styles will be picked up for both the block and the element inside.
This should be a basic example, but it should give you an idea of how powerful this methodology is, because it allows splitting any component into these basic block-element-modifier structures, making everything a breeze to maintain.
Also writing this using a precompiler like SASS makes it even easier (I won't go into details, but to give you an idea):
.menu {
display: block;
&__item {
display: inline-block;
line-height: 30px;
width: 100px;
}
&--horizontal {
width: 100%;
height: 30px;
}
&--vertical {
width: 100px;
height: 100%;
}
}
It's a methodology that's scalable, so it can be used for either small projects or large projects.
It takes a bit of getting used to, so doing some research and playing with it on a small scale project would be a good starting point.
Here's a use case, if you want to see it in used in a real project https://m.alphasights.com/how-we-use-bem-to-modularise-our-css-82a0c39463b0#.2uvxts71f
Hope it helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With