bem methodology — @frontenders ticino —17/09/2014

68
BEM METHODOLOGY Vittorio Zaccaria September 17th, 2014

Upload: vzaccaria

Post on 14-Jun-2015

1.419 views

Category:

Engineering


1 download

DESCRIPTION

Introduction to the BEM Methodology

TRANSCRIPT

Page 1: BEM Methodology — @Frontenders Ticino —17/09/2014

BEM METHODOLOGY

Vittorio Zaccaria September 17th, 2014

Page 2: BEM Methodology — @Frontenders Ticino —17/09/2014

Enthusiast about Javascript, CSS and Web technologies

Looking forward to invest into promising swiss startups

www.vittoriozaccaria.net [email protected]

ME

Page 3: BEM Methodology — @Frontenders Ticino —17/09/2014

WHAT YOU WILL GET FROM THIS SEMINAR

Page 4: BEM Methodology — @Frontenders Ticino —17/09/2014

understand how to deal with the complexity of your CSS (BEM)

Page 5: BEM Methodology — @Frontenders Ticino —17/09/2014

know which tools can help you manage your CSS projects

Page 6: BEM Methodology — @Frontenders Ticino —17/09/2014

a look at the future of front-end design technologies (web components)

Page 7: BEM Methodology — @Frontenders Ticino —17/09/2014

GOING CRAZY WITH CSS

Page 8: BEM Methodology — @Frontenders Ticino —17/09/2014

which selectors match a given element?

Page 9: BEM Methodology — @Frontenders Ticino —17/09/2014

.label

.label.left.left

.label.left

CSS

Page 10: BEM Methodology — @Frontenders Ticino —17/09/2014

yup, it can become overwhelming

Page 11: BEM Methodology — @Frontenders Ticino —17/09/2014

.label

.label.left.small

.left

.label.left

.small

.label.small

.label.left.small

.left.small

CSS

Page 12: BEM Methodology — @Frontenders Ticino —17/09/2014

specificity

Page 13: BEM Methodology — @Frontenders Ticino —17/09/2014

#a{ background-color : blue; }

.big.container > .b.c { background-color : red; }

#a.b.c

.big.container

CSS

Page 14: BEM Methodology — @Frontenders Ticino —17/09/2014

#a{ background-color : blue; }

#a.b.c

.big.container > .b.c { background-color : red; }

.big.container

CSS

Page 15: BEM Methodology — @Frontenders Ticino —17/09/2014

adjustment rules

Page 16: BEM Methodology — @Frontenders Ticino —17/09/2014

.header

.header

.logo.logo

.header .logo

Page 17: BEM Methodology — @Frontenders Ticino —17/09/2014

cognitive load might mean waste of time and money in your team

Page 18: BEM Methodology — @Frontenders Ticino —17/09/2014

A MORE STRUCTURED APPROACH

Page 19: BEM Methodology — @Frontenders Ticino —17/09/2014

CSS Design CSS Engineering(Scientific Principles)

Page 20: BEM Methodology — @Frontenders Ticino —17/09/2014

Code independence, predictability

HTML

Page 21: BEM Methodology — @Frontenders Ticino —17/09/2014

Project 2

HTML

Project 1

Code reuse

HTML

Page 22: BEM Methodology — @Frontenders Ticino —17/09/2014

BLOCK ELEMENT MODIFIER

Page 23: BEM Methodology — @Frontenders Ticino —17/09/2014

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

Page 24: BEM Methodology — @Frontenders Ticino —17/09/2014

How to define and structure modules for CSS, enabling separation of concerns and reuseHow to

organize files

Tools for manipulating BEM compliant specs

although it has more…

Page 25: BEM Methodology — @Frontenders Ticino —17/09/2014

BEM defines the concept of “block” for CSS

we will call it also “component” or “module”

Page 26: BEM Methodology — @Frontenders Ticino —17/09/2014

• html + style rules • independent w.r.t. other blocks • can be used in different parts/projects

block

Page 27: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 28: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 29: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 30: BEM Methodology — @Frontenders Ticino —17/09/2014

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

Page 31: BEM Methodology — @Frontenders Ticino —17/09/2014

.art-preview { width: 30%; ... }

.art-previewCSS

no parent blocks here, or we’d compromise reuse and mobility

Page 32: BEM Methodology — @Frontenders Ticino —17/09/2014

changes a small amount of properties of a block

it is another class to be added to the block itself

modifier

Page 33: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 34: BEM Methodology — @Frontenders Ticino —17/09/2014

.art-preview { width: 30%; } !.art-preview__size_small { width: 15%; ... } !

.art-preview

.art-preview.art-preview__size_small

CSS

Page 35: BEM Methodology — @Frontenders Ticino —17/09/2014

.art-preview__size_small

name spacing for modifiers

Separator

type of modification value of modification

CSS

Page 36: BEM Methodology — @Frontenders Ticino —17/09/2014

block(‘art-preview’).toggle(‘size_small’)

modifiable through JS

adds/removes class

JS

Page 37: BEM Methodology — @Frontenders Ticino —17/09/2014

styles only a piece of a block

this piece can’t live outside the block

element

Page 38: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 39: BEM Methodology — @Frontenders Ticino —17/09/2014

.art-preview

element of .art-preview

Page 40: BEM Methodology — @Frontenders Ticino —17/09/2014

.art-preview--title

name spacing for elements

Separator

name of the elementCSS

Page 41: BEM Methodology — @Frontenders Ticino —17/09/2014

.art-preview { ... } !.art-preview--title { font-size: 1.5rem; ... }

.art-previewCSS

.art-preview--title

Page 42: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 43: BEM Methodology — @Frontenders Ticino —17/09/2014

.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

Page 44: BEM Methodology — @Frontenders Ticino —17/09/2014

.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

Page 45: BEM Methodology — @Frontenders Ticino —17/09/2014

NESTING

Page 46: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 47: BEM Methodology — @Frontenders Ticino —17/09/2014
Page 48: BEM Methodology — @Frontenders Ticino —17/09/2014

tab block

.tab { ... }

CSS

Page 49: BEM Methodology — @Frontenders Ticino —17/09/2014

tab block

.tab { ... } !.card { ... }

CSS

card block

Page 50: BEM Methodology — @Frontenders Ticino —17/09/2014

NON BEM

Page 51: BEM Methodology — @Frontenders Ticino —17/09/2014

tab block

.tab { ... } !.card { ... } !.tab .card { // set margins }

CSS

card block

Page 52: BEM Methodology — @Frontenders Ticino —17/09/2014

BEM

Page 53: BEM Methodology — @Frontenders Ticino —17/09/2014

tab block

.tab { ... } !.card { ... } !.card.tab——card { // set margins }

CSS

card block

not using generations to express nesting

.card.tab--card

Page 54: BEM Methodology — @Frontenders Ticino —17/09/2014

.form.form——has—modal

.modal.form { ... } !.modal { ... } !.form.form——has—modal { // disable input }

CSS

child changes parent

Page 55: BEM Methodology — @Frontenders Ticino —17/09/2014

DRAWBACKS

Page 56: BEM Methodology — @Frontenders Ticino —17/09/2014

long names

.article-preview——title.article-preview——title__highlighted

CSS

Page 57: BEM Methodology — @Frontenders Ticino —17/09/2014

abbreviate

.artp——title.artp——title__hl

CSS

Page 58: BEM Methodology — @Frontenders Ticino —17/09/2014

use css preprocessors

.header { background: white; &——title { font: bold 24px/1 sans-serif; &__featured { font-size: 30px; } } }

LESS

src: http://frontendbabel.info/articles/bem-with-css-preprocessors/

.header { background: white; } .header——title { font: bold 24px/1 sans-serif; } .header——title__featured { font-size: 30px; }

CSS

Page 59: BEM Methodology — @Frontenders Ticino —17/09/2014

BEMTO

src: https://github.com/kizu/bemto

+b.block1 +e.element1 Foo +b.block2 +e('a')(href="#bar").element Bar +e.element2 Baz

JADE

<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>

HTML

Page 60: BEM Methodology — @Frontenders Ticino —17/09/2014

seriously, try jade, even without BEM

http://jade-lang.com/

Page 61: BEM Methodology — @Frontenders Ticino —17/09/2014

WHAT ABOUT SMACSS - MVCSS?

Page 62: BEM Methodology — @Frontenders Ticino —17/09/2014

BEM SMACSSMVCSS

block

element

modifier

Scalable and modular CSS https://smacss.com/

base

layoutNesting similar to

applies to tags and ids

grids and stuff

modules

subclassing

states

Modular View CSS http://mvcss.github.io/

foundationreset

componentsreusable patterns

structuresnot reusable patterns

Page 63: BEM Methodology — @Frontenders Ticino —17/09/2014

THE FUTURE

Page 64: BEM Methodology — @Frontenders Ticino —17/09/2014

Polymerhttp://www.polymer-project.org/

Page 65: BEM Methodology — @Frontenders Ticino —17/09/2014

create your own tags!

<!-- 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>

HTML

Page 66: BEM Methodology — @Frontenders Ticino —17/09/2014

<!-- 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>

HTML

Page 67: BEM Methodology — @Frontenders Ticino —17/09/2014

encapsulation with the Shadow DOM

Page 68: BEM Methodology — @Frontenders Ticino —17/09/2014

THANKS!