Given the following BEM tree structure, where five nested levels exist:
collection-main__features-top__story__byline__author
according to BEM's naming convention, where an element is part of a block and has no meaning outside of the block it belongs to, what is the proper way to name the author
class?
Since an author
is semantically tied to the byline
and the story
context, but meaningless under the features-top
and collection-main
blocks, what is the best BEM name?
collection-main__author
features-top__author
story__author (best?)
story__byline__author
byline__author
What happens if a new features
block gets introduced?
collection-main__features-top__story__byline__author (target)
collection-main__features-bottom__story__byline__author
features-top__story__author
story--features-top__author (best?)
Finally, what happens if another collection
block gets added and we want to style the second author element in the list?
collection-main__features-top__story__byline__author
collection-main__features-bottom__story__byline__author (target)
collection-sub__features-top__story__byline__author
collection-sub__features-bottom__story__byline__author
Would we do something like this?
story--collection-main--features-bottom__author
There must be a better option.
BEM prohibits put an elements in the elements in CSS!
You make the most typical error in BEM markup - writing a
block__element__element
. You must create new blocks, rather than copying DOM-tree.For example:
Right HTML:
Right CSS:
If you need to make an element of an element, then you need to make a new block or make your bem-tree with a single nested elements!
WRONG:
RIGHT #1: Make a new block
RIGHT #2: Make your bem-tree with a single nested elements
Pay attention - you can not put elements in a elements in the css, but you can and should put elements in a elements into html! DOM-tree and BEM-tree can be different.
Do not write strange names, putting the element name in the name of the block!
WRONG:
Because you get a problem with odd names when you try to move the block:
Nested html-elements is a DOM-tree.
The names of the classes you write is a BEM-tree.
Feel the difference!
DOM-tree:
BEM-tree:
References:
An element is a part of a block! Not part of element! Read Vitaly Harisov, the author of BEM-methodology: https://twitter.com/harisov/status/403421669974618112
Read also:
There is a report (in Russian) on a web conference WebCamp: Front-end Developers Day about this topic: https://www.youtube.com/watch?v=kBgHdSOj33A + slides: http://ihorzenich.github.io/talks/2015/frontendweekend-bem/