55
The benefits of BEM CSS The benefits of BEM CSS - Bob Donderwinkel, 2014 1

The benefits of BEM CSS

Embed Size (px)

Citation preview

The benefits of BEM CSS

The benefits of BEM CSS - Bob Donderwinkel, 2014 1

Who?

• Bob Donderwinkel

• Front-end Developer at Viewbook

• Freelancer at bd creations

The benefits of BEM CSS - Bob Donderwinkel, 2014 2

Who?

• Bob Donderwinkel

• Front-end Developer at Viewbook

Our hardheaded senior front-end developer, currently specialising in responsive HTML, CSS and mobile

• Freelancer at bd creations

The benefits of BEM CSS - Bob Donderwinkel, 2014 3

Who?

• Bob Donderwinkel

• Front-end Developer at Viewbook

Our hardheaded senior front-end developer, currently specialising in responsive HTML, CSS and mobile

• Freelancer at bd creations

Development with Eye for Design

The benefits of BEM CSS - Bob Donderwinkel, 2014 4

So what's the gist?

• What is BEM?

• What does BEM solve?

• What have I used BEM for?

• Some semi-interesting BEM tidbits

The benefits of BEM CSS - Bob Donderwinkel, 2014 5

So what's the gist?

• What is BEM?

• What does BEM solve?

• What have I used BEM for?

• Some semi-interesting BEM tidbits

Mind you, I have been working with BEM CSS for about half a year now.

The benefits of BEM CSS - Bob Donderwinkel, 2014 6

So what is BEM?

The benefits of BEM CSS - Bob Donderwinkel, 2014 7

Block, Element, Modifier

The benefits of BEM CSS - Bob Donderwinkel, 2014 8

So what is BEM?

You might just as well call it

Module, Object, Modifier

But then you could be saying something like

I am using MOM

Which would be weird.

The benefits of BEM CSS - Bob Donderwinkel, 2014 9

Unified semantic model for markup, styles, code

and UX— Smashing Magazine

The benefits of BEM CSS - Bob Donderwinkel, 2014 10

What is BEM?

• BEM originated at Yandex

• BEM is a methodology, an abstract way of reasoning about your interface.

• BEM captures purpose and meaning through it's CSS classname syntax.

The benefits of BEM CSS - Bob Donderwinkel, 2014 11

What is BEM?

BEM is not just for CSS. It is about communicating between technologies and the people using them.

• Design

• Markup

• Styles

• Code

The benefits of BEM CSS - Bob Donderwinkel, 2014 12

Block, Elements and ModifiersWhat is that about?

The benefits of BEM CSS - Bob Donderwinkel, 2014 13

BlockWhat is BEM?

• An independent, reusable part of your interface.

• Think: Headers, Menus, Images, etc

• Can contain Elements and other Blocks.

• Can contain one or more Modifiers

<header class='b-header'></header>

The benefits of BEM CSS - Bob Donderwinkel, 2014 14

ElementWhat is BEM?

• Belongs to a Block, only has meaning there

• Can contain other Elements and Blocks.

• Think: Header sub-title, Menu item, Image caption, etc

• Can contain one or more Modifiers<header class='b-header'> <h1 class='b-header__title'></h1> <p class='b-header__description'></p></header>

The benefits of BEM CSS - Bob Donderwinkel, 2014 15

ModifiersWhat is BEM?

• Belongs on a Block or Element

• Indicates a state different from the default

• Think: themes, active/inactive state, aligning, etc

• Can also be a key value pair<header class='b-header b-header_theme_awesome'> <h1 class='b-header__title b-header__title_large'></h1> <p class='b-header__description'></p></header>

The benefits of BEM CSS - Bob Donderwinkel, 2014 16

ModifiersWhat is BEM?

And Modifiers are nicely groupable using CSS preprocessors like SASS/LESS.b-header{

&_theme_awesome{ background-color: PapayaWhip; }

&__title { &_large{ font-size: xx-large; } }}

The benefits of BEM CSS - Bob Donderwinkel, 2014 17

BEM CSS syntaxWhat is BEM?

The official syntax.b-[block-name].b-[block-name]__[element].b-[block-name]__[element]_[modifier]

Pretty simple.- Use only Latin letters, dashes and digits. Not underscores (_)- Use underscores as the Block, Element and Modifier separators.The benefits of BEM CSS - Bob Donderwinkel, 2014 18

BEM CSS syntaxWhat is BEM?

Is this okay?

.b-[block-name]__[element]__[sub-element]

.b-[block-name]_[modifier]__[element]

I have seen some conflicting articles, but probably not.

The benefits of BEM CSS - Bob Donderwinkel, 2014 19

BEM CSS syntaxWhat is BEM?

Or roll your own syntax.

Perhaps because that better suits the CSS code base of a project. In any case:

• Be consistent

• Keep the Block/Element/Modifier separators unique so you can use BEM automation tools.

The benefits of BEM CSS - Bob Donderwinkel, 2014 20

BEM CSS syntaxSo what is BEM?

Most importantly, use meaningful BEM classnames.

If you end up with something like this, it could pretty much defeat the point. .b-awesome-gizmo-three__teaser_latest

The benefits of BEM CSS - Bob Donderwinkel, 2014 21

What is BEM?

BEM also touches on some well known Object Oriented Programming concepts.

Don't Repeat Yourself (DRY) - Blocks are self contained and reusable.

Seperation of Concerns - Using BEM CSS classes decouples your CSS from your HTML.

The benefits of BEM CSS - Bob Donderwinkel, 2014 22

What is BEM?

BEM also touches on some well known Object Oriented Programming concepts.

Single Responsibility Principle - Blocks, Element and Modifiers focus on managing just one thing, themselves.

Open/Closed Principle - Modifiers often only extend on default styles and behaviour

The benefits of BEM CSS - Bob Donderwinkel, 2014 23

So what does BEM CSS solve?

The benefits of BEM CSS - Bob Donderwinkel, 2014 24

Chaos (Greek χάοςm khaos) the formless or

void state preceding the creation of .. (*)

— * = insert title of your awesome project here

The benefits of BEM CSS - Bob Donderwinkel, 2014 25

Seriously, what does using BEM CSS really help with?

CSS can be wonderfully simple and easy to get started with. And even easier to mess up on the long run.

Legacy CSS can be a royal pain.

We have all been there, or have inherited these kind of projects to take care of.

The benefits of BEM CSS - Bob Donderwinkel, 2014 26

Seriously, what does using BEM CSS really help with?

When projects scale, CSS often does not.

CSS code is easy to append, overwrite or reinvent. That can turn in to a mess.

It is a bit like growing a new haircut every month, but only trimming a piece of it.

The benefits of BEM CSS - Bob Donderwinkel, 2014 27

It is fun until you realise you can not walk away from it.

The benefits of BEM CSS - Bob Donderwinkel, 2014 28

So what does BEM CSS solve?

BEM CSS gives you a default aproach for new projects.

• Thinking in Blocks, Elements and Modifiers helps you figure out what your design is made of.

• BEM helps you getting started coding CSS a little bit quicker through a standard syntax.

• BEM is about communicating between technologies and the people using them.

The benefits of BEM CSS - Bob Donderwinkel, 2014 29

So what does BEM CSS solve?

For example- Your design tells you what Blocks, Elements and Modifiers are present.- You can use these to scaffold up a rough prototype using HTML and CSS.- You review the prototype to see what behaviour is needed with Javascript.- The design is tweaked and the prototype is tuned. - Rinse and repeat.

And you're done..

..Well at least you have a better idea what your project needs to complete ;) The benefits of BEM CSS - Bob Donderwinkel, 2014 30

Yes, yes.. Can you give me some pratical reasons to use BEM CSS

The benefits of BEM CSS - Bob Donderwinkel, 2014 31

Is there any practical stuff BEM CSS can help with?What does BEM solve

Single CSS classname perksBetter CSS performance

• Rendering engines evaluate CSS selectors right to left.

• The less they have to evaluate, the faster it renders.

The benefits of BEM CSS - Bob Donderwinkel, 2014 32

Is there any practical stuff BEM CSS can help with?What does BEM solve

My view is that authors should not need to worry about optimizing selectors (and from what I see, they generally don’t), that should be the job of the engine.~ Antti Koivisto

Luckely CSS is the least of your performance worries today.

The benefits of BEM CSS - Bob Donderwinkel, 2014 33

Is there any practical stuff BEM CSS can help with?What does BEM solve

Reduce hell in CSS specificty• Complex CSS selectors get a higher importance. You have

to match up to this selector or use !important to override it.

• Or find the actual CSS you want to change. But the bigger the CSS code base, the nastier that can get ;)

Using single CSS classnames makes it easier to rework your existing CSS code base.The benefits of BEM CSS - Bob Donderwinkel, 2014 34

Is there any practical stuff BEM CSS can help with?What does BEM solve

Global CSS reset/normalize not needed (preferably)

• Remember BEM Blocks should be independent and re-usable. A global CSS reset/normalize does not exactly fit that idea.

Perhaps use something like uncss in your build process to get only the needed normalize/reset CSS into your Blocks and Elements.The benefits of BEM CSS - Bob Donderwinkel, 2014 35

Is there any practical stuff BEM CSS can help with?What does BEM solve

Better HTML/CSS decoupling

• Using HTML element names in your CSS selectors forces you to update both when only one changes.

• Using just single CSS BEM classnames makes them more portable to another HTML structure.

The benefits of BEM CSS - Bob Donderwinkel, 2014 36

Is there any practical stuff BEM CSS can help with?What does BEM solve

Project automation through BEM's predictable CSS classnames

• Yandex has tools if you are using the standard BEM syntax

• CSS preprocessors like SASS and LESS are especially well suited for generating BEM CSS

• And with build tools like Grunt or Gulp you can custom fit BEM CSS automation for your own project.

The benefits of BEM CSS - Bob Donderwinkel, 2014 37

But is it all utter BEM BLISS?

The benefits of BEM CSS - Bob Donderwinkel, 2014 38

Utter BEM BLISS?

What about the CSS cascade?

Yes, using just single CSS classnames can feel a bit too simplistic at times. But you can use regular CSS allong with your BEM CSS. Or you can use SASS or LESS to @include or @extend a CSS library.@import "my-themes";

&_theme_awesome{ @include theme-more-awesome();}

The benefits of BEM CSS - Bob Donderwinkel, 2014 39

Utter BEM BLISS?

But I hate looking at long and ugly-ish CSS classnames

• True.. no way around that. BEM CSS can be a tad challenging for the aesthetically inclinded code fashionista.

• But luckely the visitor of website or application will not look in the source all too often ;)

Doesn't HTML filesize bloat with long CSS classnames?

• Yes, true. But minification and gzipping go a long way.The benefits of BEM CSS - Bob Donderwinkel, 2014 40

Utter BEM CSS BLISS?

And is BEM only useful in larger teams?

• BEM CSS certainly has extra perks in larger teams because of the meaningful BEM CSS classnames.

• But working solo you can also use the Block, Element and Modifier approach on a design to get started prototyping sooner.

The benefits of BEM CSS - Bob Donderwinkel, 2014 41

Utter BEM CSS BLISS?

Is BEM not yet another 'framework/methodology/whatever' to try and get my head around?

• Yes, but it's concept and syntax is pretty straight forward to get started with and use.

• And BEM can play pretty nicely with other CSS approaches out there.

The benefits of BEM CSS - Bob Donderwinkel, 2014 42

Utter BEM BLISS?Other CSS approaches

Object Oriented CSS

• Separation of Structure from Skin

• Separation of Containers and Content

In essence OOCSS is about keeping CSS modular so it can be reused and extended. This is similar to the Block, Element and Modifier approach.

The benefits of BEM CSS - Bob Donderwinkel, 2014 43

Utter BEM BLISS?Other CSS approaches

Scalable and Modular Architecture for CSS

• Guidelines how you can write and organize your CSS

SMACCS has five types of CSS categories Base, Layout, Module, State, Theme.

The Module and State categories are simular analogue for the Block, Element and Modifier approach.

The benefits of BEM CSS - Bob Donderwinkel, 2014 44

Utter BEM BLISS?Other CSS approaches

Web components - Polymer and Brick

Web Components are a collection of web standards focussed on delivering self-contained and reusable custom blocks of HTML, Javascript & CSS.

BEM CSS can be used 'inside' a Web Component just like in any other project, and with roughly the same benefits.The benefits of BEM CSS - Bob Donderwinkel, 2014 45

Do you have some interesting BEM CSS tidbits?

The benefits of BEM CSS - Bob Donderwinkel, 2014 46

Multi modifer CSS classesBEM tidbits

Multi modifer CSS classes- Some Gulp.js which will rework a bit of BEM CSS so you can use multiple Modifiers in a single BEM class

__block__element_theme-light_size-large

Wow, even longer CSS classnames...!

The benefits of BEM CSS - Bob Donderwinkel, 2014 47

Multi modifer CSS classesBEM tidbits

@bobdonderwinkel we suggest to use classic approach and write as many classes as needed in DOM tree@bobdonderwinkel and avoid copypaste with the help of template engines— Twitter #b_Yeah, perhaps not that great an idea afterall. Fun experiment though.

The benefits of BEM CSS - Bob Donderwinkel, 2014 48

kaBEMBEM tidbits

BEM CSS scaffolding on some steroids

kaBEM is a grunt.js environment for quick scaffolding and tweaking of HTML with BEM CSS (Block, Element, Modifier).- See a BEM classname in your CSS and know where to use it in your HTML- See a BEM classname in your HTML and know where to find it in your CSS files- See a BEM classname and know which purpose it has

But.. Disclaimer

The benefits of BEM CSS - Bob Donderwinkel, 2014 49

kaBEMBEM tidbits

Disclaimer: I created kaBEM just starting with BEM CSS. So be mindful of some little hickups that trickeled in. For example kaBEM uses multi element/modifier BEM CSS classnames.

.b-block__element-1__element-2 .b-block__element_modifiers-1_modifier-2

In hindsight these are a bit of a BEM CSS no-no ;) But no worries, a new version is on the way.The benefits of BEM CSS - Bob Donderwinkel, 2014 50

BEM User StoriesBEM tidbits

User stories seem like a nice way to get going with BEM even before you get busy with a design or prototype.

• Sum up what parts should be present (Blocks and Elements)

• Describe how these parts should look & behave (Modifiers)

• Translate these User Stories in BEM CSS classnames

• Get some coffee and start coding

The benefits of BEM CSS - Bob Donderwinkel, 2014 51

Okay, round up already

The benefits of BEM CSS - Bob Donderwinkel, 2014 52

Rounding up

• Thinking in Block, Elements and Modifiers helps you interpretate a design quicker and get you started coding sooner.

• BEM CSS classnames are about communicating, keep them meaningful.

• BEM CSS classnames are well suited for automation because of it's default syntax.

The benefits of BEM CSS - Bob Donderwinkel, 2014 53

Good readsRounding up

• Scaling Down The BEM Methodology For Small Projects

• MindBEMding – getting your head ’round BEM syntax

• Organizing CSS: OOCSS, SMACSS, and BEM

• Generating BEM selectors with CSS preprocessors

The benefits of BEM CSS - Bob Donderwinkel, 2014 54

Contact me!Rounding up

• www.bdcreations.nl

[email protected]

• Twitter

• Linkedin

The benefits of BEM CSS - Bob Donderwinkel, 2014 55