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:
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):
BEM stands for Blocks, Elements and Modifiers. It's a methodology initially conceived by Russian tech company Yandex. As the same implies, BEM metholology is all about componentization of your HTML and CSS code into three types of components:
Blocks: standalone entities that are meaningful on their own. Examples are header, container, menu, checkbox & textbox
Elements: Part of blocks that have no standalone meaning and are semantically tied to their blocks. Examples are menu item, list item, checkbox caption & header title
Modifiers: Flags on a block or element, used to change appearance or behavior. Examples are disabled, highlighted, checked, fixed, size big & color yellow
The goal of BEM is to keep optimize the readability, maintainability and flexibility of your CSS code. The way to achieve this, is to apply the following rules.
Block styles are never dependent on other elements on a page
Blocks should have a simple, short name and avoid _ or - characters
When styling elements, use selectors of format blockname__elementname
When styling modifiers, use selectors of format blockname--modifiername and blockname__elementname--modifiername
Elements or blocks that have modifiers should inherit everything from the block or element it is modifying except the properties the modifier is supposed to modify
Code example
If you apply BEM to your form elements, your CSS selectors should look something like this:
BEM gained quite some traction among American & Western-European web developers after having been popularized by influential CSS gurus like Harry Roberts. It is especially popular in combination with OOCSS and SMACSS (which are all really just variations on the same principles of how to write modular CSS). And modular CSS does come with quite a few advantages. However, modular CSS can be quite verbose, and BEM in particular is often considered quite ugly.
In the end, it all depends on what your code is being used for and who else will be reading and modifying your code in the future. I would definitely recommend modular CSS for any project with a large codebase and multiple maintainers, but that doesn't mean that you have to go with BEM naming conventions or strictly apply OOCSS/SMACSS principles for componentization.
If the likelyhood of your code ever being maintained by developers who already have experience with BEM/OOCSS/SMACSS is significant, you might want to pick one or more of these methodologies and be strict about their application, just because it would make collaboration with those developers a heck of a lot easier. If not, there is little advantage to strictly sticking with any of those particular conventions and you should feel free to pick your own naming conventions and your own variation on BEM/OOCSS/SMACSS. In the end, the usefulness of any standard largely depends on who is actually using it.
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:
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):
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 :)
What is BEM?
BEM stands for
Blocks, Elements and Modifiers
. It's a methodology initially conceived by Russian tech company Yandex. As the same implies, BEM metholology is all about componentization of your HTML and CSS code into three types of components:Blocks: standalone entities that are meaningful on their own. Examples are
header
,container
,menu
,checkbox
&textbox
Elements: Part of blocks that have no standalone meaning and are semantically tied to their blocks. Examples are
menu item
,list item
,checkbox caption
&header title
Modifiers: Flags on a block or element, used to change appearance or behavior. Examples are
disabled
,highlighted
,checked
,fixed
,size big
&color yellow
The goal of BEM is to keep optimize the readability, maintainability and flexibility of your CSS code. The way to achieve this, is to apply the following rules.
_
or-
charactersblockname__elementname
blockname--modifiername
andblockname__elementname--modifiername
Code example
If you apply BEM to your form elements, your CSS selectors should look something like this:
The corresponding HTML should look something like this:
Should YOU use BEM?
BEM gained quite some traction among American & Western-European web developers after having been popularized by influential CSS gurus like Harry Roberts. It is especially popular in combination with OOCSS and SMACSS (which are all really just variations on the same principles of how to write modular CSS). And modular CSS does come with quite a few advantages. However, modular CSS can be quite verbose, and BEM in particular is often considered quite ugly.
In the end, it all depends on what your code is being used for and who else will be reading and modifying your code in the future. I would definitely recommend modular CSS for any project with a large codebase and multiple maintainers, but that doesn't mean that you have to go with BEM naming conventions or strictly apply OOCSS/SMACSS principles for componentization.
If the likelyhood of your code ever being maintained by developers who already have experience with BEM/OOCSS/SMACSS is significant, you might want to pick one or more of these methodologies and be strict about their application, just because it would make collaboration with those developers a heck of a lot easier. If not, there is little advantage to strictly sticking with any of those particular conventions and you should feel free to pick your own naming conventions and your own variation on BEM/OOCSS/SMACSS. In the end, the usefulness of any standard largely depends on who is actually using it.