BEM stands for Blocks, Elements and Modifiers
. It's a methodology initially conceived by Russian tech company Yandex, but which gained quite some traction among American & Western-European web developers as well.
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
and blockname__elementname--modifiername
If you apply BEM to your form elements, your CSS selectors should look something like this:
.form { } // Block
.form--theme-xmas { } // Block + modifier
.form--simple { } // Block + modifier
.form__input { } // Block > element
.form__submit { } // Block > element
.form__submit--disabled { } // Block > element + modifier
The corresponding HTML should look something like this:
<form class="form form--theme-xmas form--simple">
<input class="form__input" type="text" />
<input class="form__submit form__submit--disabled" type="submit" />
</form>