Reference
Example of BEM
.opinions_box {
margin: 0 0 8px 0;
text-align: center;
&__view-more {
text-decoration: underline;
}
&__text-input {
border: 1px solid #ccc;
}
&--is-inactive {
color: gray;
}
}
Blocks, Elements and Modifiers
Block
Standalone entity that is meaningful on its own.
Examples
header
, container
, menu
, checkbox
, input
Element
A part of a block that has no standalone meaning and is semantically tied to its block.
Examples
menu item
, list item
, checkbox caption
, header title
Modifier
A flag on a block or element. Use them to change appearance or behavior.
Examples
disabled
, highlighted
, checked
, fixed
, size big
, color yellow
HTML
<button class="button">
Normal button
</button>
<button class="button button--state-success">
Success button
</button>
<button class="button button--state-danger">
Danger button
</button>
CSS
.button {
display: inline-block;
border-radius: 3px;
padding: 7px 12px;
border: 1px solid #D5D5D5;
background-image: linear-gradient(#EEE, #DDD);
font: 700 13px/18px Helvetica, arial;
}
.button--state-success {
color: #FFF;
background: #569E3D linear-gradient(#79D858, #569E3D) repeat-x;
border-color: #4A993E;
}
.button--state-danger {
color: #900;
}
Benefits
Modularity
Block styles are never dependent on other elements on a page, so you will never experience problems from cascading.
You also get the ability to transfer blocks from your finished projects to new ones.
Reusability
Composing independent blocks in different ways, and reusing them intelligently, reduces the amount of CSS code that you will have to maintain.
With a set of style guidelines in place, you can build a library of blocks, making your CSS super effective.
Structure
BEM methodology gives your CSS code a solid structure that remains simple and easy to understand.