On Github andrewrota / BEM-Methodology-Talk
Interactive Developer at Sapient Global Markets
History
tl;dr: Developed by Yandex, Russia's largest search engine company, in 2006/2007 to bring structure to web pages.
.media {margin:10px;} .media, .bd {overflow:hidden; _overflow:visible; zoom:1;} .media .img {float:left; margin-right: 10px;}
.pod-callout { width: 200px; } .pod-callout input[type=text] { width: 180px; }
.M-1 {margin: 1px;} .M-2 {margin: 2px;} .M-4 {margin: 4px;}OOCSS
block /blɒk/ n : A block is an independent entity, a "building block" of an application. A block can be either simple or compound (containing other blocks).
el•e•ment /ˈɛləmənt/ n : An element is a part of a block that performs a certain function. Elements are context-dependent: they only make sense in the context of the block they belong to.
mod•i•fi•er /ˈmɒdəˌfaɪər/ n : A modifier is a property of a block or an element that alters its look or behavior.
<nav> <ul> <li><a href="#">Breaking</a></li> <li><a href="#">World</a></li> <li><a href="#">U.S.</a></li> <li><a href="#">Business</a></li> <li><a href="#">Politics</a></li> <li><a href="#">Entertainment</a></li> <li><a href="#">Technology</a></li> <li><a href="#">Sports</a></li> </ul> </nav>
<nav class="nav-menu"> <ul> <li><a href="#">Breaking</a></li> <li><a href="#">World</a></li> <li><a href="#">U.S.</a></li> <li><a href="#">Business</a></li> <li><a href="#">Politics</a></li> <li><a href="#">Entertainment</a></li> <li><a href="#">Technology</a></li> <li><a href="#">Sports</a></li> </ul> </nav>
<nav class="nav-menu"> <ul class="nav-menu--items"> <li class="nav-menu--item"><a href="#">Breaking</a></li> <li class="nav-menu--item"><a href="#">World</a></li> <li class="nav-menu--item"><a href="#">U.S.</a></li> <li class="nav-menu--item"><a href="#">Business</a></li> <li class="nav-menu--item"><a href="#">Politics</a></li> <li class="nav-menu--item"><a href="#">Entertainment</a></li> <li class="nav-menu--item"><a href="#">Technology</a></li> <li class="nav-menu--item"><a href="#">Sports</a></li> </ul> </nav>
.nav-menu { display: block; margin: 0; padding: 0; width: 100%; float: right; } .nav-menu--items { margin: 0; padding: 0; } .nav-menu--item { float: left; list-style-type: none; margin-left: 0.5%; text-align: center; width: 12%; & > a { background: $primaryColor; color: white; display: block; font-size: .9em; line-height: 3.2; text-decoration: none; &:hover { background: $primaryColorLighter; -webkit-transform: rotate(10deg); transform: rotate(10deg); -webkit-transition: all .25s linear; -moz-transition: all .25s linear; -o-transition: all .25s linear; transition: all .25s linear; } } }
.nav-menu--item__simple { @extend .nav-menu; display: inline; float: none; & > a { line-height: 1; text-decoration: none; } &:after { content: " | "; } &:last-child:after { content: ""; } }
├── blocks │ ├── navMenu │ │ ├── navMenu.css │ │ ├── navMenuItem.css │ ├── logo │ │ └── logo.css │ ├── header │ │ └── header.css │ └── search │ │ ├── search.css │ │ └── search_autocomplete.css
.my-component-name__title
vs
.my-component .titleNamespacing helps you control the styles of your code. It's poor man's encapsulation. Especially when nesting components. Especially valuable on large sites.
block('navMenu').elem('item').click( ... ); block('navMenu').elem('item').setModifier('current');
There are only two hard things in Computer Science: cache invalidation and naming things.
- software engineer Phil Karlton's frequently quoted saying can apply to CSS - BEM uses intuitive language to describe the UI - Shared language from the beginning.block_name { &__element { color: #f00; &--modifier { font-weight: bold; } } }
Produces
.block_name__element { color: #f00; } .block_name__element--modifier { font-weight: bold; }
$b: ".block_name"; #{$b}__element { color: #f00; } #{$b}__element--modifier { font-weight: bold; }
Produces
.block_name__element { color: #f00; } .block_name__element--modifier { font-weight: bold; }
.block_name &__element color: #f00 &--modifier font-weight: bold
Produces
.block_name__element { color: #f00; } .block_name__element--modifier { font-weight: bold; }
{ block: 'navigation', content: [ { elem: 'item', content: 'News' } ] }BEMHTML assembles your web pages for you in terms of blocks, elements, and modifiers. It's essentially an all in one templating solution for building your client side code. Page structure is in a JSON format: the "BEM Tree". This gets converted to HTML.
.primary-navigation { font-size: 12px; color: red; font-weight: bold; } .secondary-navigation { color: red; font-weight: bold; }
To...
.primary-navigation{ font-size:12px } .primary-navigation, .secondary-navigation{ color:red; font-weight:bold }Yandex's CSS Optimizer is helpful for cleaning up repeated properties. It will handle tasks like merging blocks with identical properties.
Rather than writing
ul.primary-navigation>li.primary-navigation__item*5
Instead write:
ul.primary-navigation>li.-item*5|bem
Results in:
<ul class="primary-navigation"> <li class="primary-navigation__item"></li> <li class="primary-navigation__item"></li> <li class="primary-navigation__item"></li> <li class="primary-navigation__item"></li> <li class="primary-navigation__item"></li> </ul>
Example:
.pagination { text-align:center; letter-spacing:-0.31em; word-spacing:-0.43em; } [...] .pagination__first a:before { content:"\00AB" "\00A0"; } .pagination__last a:after { content:"\00A0" "\00BB"; }Harry Robert's (CSS Wizardry) Inuit is built on the BEM naming convention, but it's not a pure BEM framework.