BEM Methodology — @Frontenders Ticino —17/09/2014
- 2. ME
Enthusiast about Javascript, CSS and Web
technologies
Looking forward to invest into promising
swiss startups
www.vittoriozaccaria.net
vittorio.zaccaria@gmail.com
- 6. a look at the future of front-end design
technologies (web components)
- 13. #a
{
background-color : blue;
}
.big.container > .b.c
{
background-color : red;
}
.big.container
#a.b.c
CSS
- 14. #a
{
background-color : blue;
}
#a.b.c
.big.container > .b.c
{
background-color : red;
}
.big.container
CSS
- 23. it’s a set of formalized guidelines developed by
Yandex, RU in the past few years
it is mainly a naming and structuring
methodology for CSS/HTML
- 24. How to define and
structure modules for
CSS, enabling separation
How to of concerns and reuse
organize files
Tools for manipulating
BEM compliant specs
although it has more…
- 25. BEM defines the concept of “block” for CSS
we will call it also “component” or “module”
- 26. block
• html + style rules
• independent w.r.t. other blocks
• can be used in different parts/projects
- 30. how BEM would define this block
1. use only one class - they flatten specificity
2. no positioning information in it
3. dont introduce relative adjustment
- 31. .art-preview {
width: 30%;
...
}
.art-preview
CSS
no parent blocks here, or
we’d compromise reuse and mobility
- 32. modifier
changes a small amount of properties of a block
it is another class to be added to the block itself
- 34. .art-preview {
width: 30%;
}
!
.art-preview__size_small {
width: 15%;
...
}
!
.art-preview
CSS
.art-preview.art-preview__size_small
- 35. name spacing for modifiers
.art-preview__size_small
Separator
type of modification
value of modification
CSS
- 40. name spacing for elements
.art-preview--title
Separator
name of the element
CSS
- 41. .art-preview {
...
}
!
.art-preview--title {
font-size: 1.5rem;
...
}
.art-preview
CSS
.art-preview--title
- 43. .art-preview--title {
font-size: 1.5rem;
...
}
!
.art-preview__size_small .art-preview—title
{
font-size: 1.2rem;
...
}
.art-preview--title
.art-preview__size_small .art-preview--title
CSS
- 44. .art-preview--title {
font-size: 1.5rem;
...
}
!
.art-preview__size_small .art-preview—title
{
font-size: 1.2rem;
...
}
.art-preview--title
.art-preview__size_small .art-preview--title
CSS
- 51. tab block
.tab {
...
}
!
.card {
...
}
!
.tab .card {
// set margins
}
CSS
card block
- 53. tab block
.tab {
...
}
!
.card {
...
}
!
.card.tab——card {
// set margins
}
CSS
card block
not using generations to express nesting
.card.tab--card
- 54. child changes parent
.form.form——has—modal
.modal
.form {
...
}
!
.modal {
...
}
!
.form.form——has—modal {
// disable input
}
CSS
- 58. use css preprocessors
LESS
.header {
background: white;
&——title {
font: bold 24px/1 sans-serif;
&__featured {
font-size: 30px;
}
}
}
CSS
.header {
background: white;
}
.header——title {
font: bold 24px/1 sans-serif;
}
.header——title__featured {
font-size: 30px;
}
src: http://frontendbabel.info/articles/bem-with-css-preprocessors/
- 59. BEMTO
HTML
src: https://github.com/kizu/bemto
JADE
+b.block1
+e.element1 Foo
+b.block2
+e('a')(href="#bar").element Bar
+e.element2 Baz
<div class="block1">
<div class="block1__element1">
Foo
</div>
<div class="block2">
<a class="block2__element"
href="#bar">Bar</a>
</div>
<div class="block1__element2">
Baz
</div>
</div>
- 62. MVCSS BEM SMACSS
block
element
modifier
Scalable and modular CSS
https://smacss.com/
base
applies to tags and ids
layout Nesting similar to
grids and stuff
modules
subclassing
states
Modular View CSS
http://mvcss.github.io/
foundation reset
structures
not reusable patterns
components reusable patterns
- 65. create your own tags!
HTML
<!-- Polyfill Web Components support for older browsers -->
<script src="components/platform/platform.js"></script>
!
<!-- Import element -->
<link rel="import" href="counter.html">
!
<!-- Use element -->
<my-counter counter="10">Points</my-counter>
- 66. HTML
<!-- Define element -->
<polymer-element name="my-counter" attributes="counter">
<template>
<style> /*...*/ </style>
<div id="label"><content></content></div>
Value: <span id="counterVal">{{counter}}</span><br>
<button on-tap="{{increment}}">Increment</button>
</template>
<script>
Polymer({
counter: 0, // Default value
counterChanged: function() {
this.$.counterVal.classList.add('highlight');
},
increment: function() {
this.counter++;
}
});
</script>
</polymer-element>