** writing multiple block on same element is right approach in BEM ? *
<div class="block-1 block-2 block-1_modifier block-2_modifier">
<div class="block-1__element1">
<div class="block-1__element3 block-2__element2">
</div>
</div>
<div class="block-1__element2">
<div class="block-1__element4">
</div>
</div>
</div>
Yes, this is partially correct, although your naming convention above should be more like:
block-1 SHOULD NOT HAVE block-2
Your single nest level for block-1__element3 is good.
Don't do block-1__element2__element3
because that creates messy nesting.
Only nest one level deep - like the example you indicated.
This is a tidier approach to BEM which was overlooked by those who used it.
See corrections:
<div class="block-1 block-1--modifier-1 block-1--modifier-2">
<div class="block-1__element1">
</div>
<div class="block-1__element2">
<div class="block-1__element3"> </div>
</div>
</div>
</div>
Related
I am structuring the CSS in my web application according to the BEM convention.
I have a block called item and 3 elements: item__section, item__title and item__description.
I am using these BEM classes as follows:
<div class="item">
<div class="item__section item__title"> ... </div>
<div class="item__section item__description"> ... </div>
</div>
The item__section element class contains style that we reuse between elements.
Is this valid BEM or should I create a modifier for item__section for each kind of section(title and description)?
That's absolutely valid and is called mix:
https://en.bem.info/methodology/key-concepts/#mix
https://en.bem.info/methodology/css/#mixes
I don't think you should.
You could add mixin like:
<div class="item">
<div class="item__section u-title"> ... </div>
<div class="item__section u-description"> ... </div>
</div>
but don't use two elements of the same block in the same tag because you can end up with specificity problems
I would like to know if, according to BEM methodology, I can have the following structure:
.block1
.block1__element1
.block2
.block1__element2 <-- ??
Am I allowed to use an element from a parent block, inside a children block?
Thanks.
UPDATE:
This is the actual DOM structure:
<div class="head">
<div class="head__user"></div>
<div class="head__nav">
<div class="menu">
// <-- ???
</div>
</div>
</div>
According to best practices of BEM methodology: am I allowed to move the element with head__user inside the menu block? Or all elements inside the menu block need to start with the menu__ prefix?
I hope this clears out the problem.
I been using BEM for sometime and from what I got it's not recommended nor intended to be used like that. You can nest different BEM elements to each other like menu-blockintohead-block, but menu-block items should not go outside its parent menu-block, like you should not put menu-block__item at the top of head-block. Does it makes sense? :)
To illustrate there are two ways to go. What should be noted here is that depending on the scale of your project and how you build things (component based?). If you don't have a large project and are not doing or reusing the menu else where you can do it both ways. Lets say your menu is huge amount of html/css I would do it like #1
This is not correct
<div class="head">
<div class="head__user"></div>
<div class="head__nav">
<div class="menu">
<div class="head__something"></div>
</div>
</div>
</div>
Recommended solution
Based on this part of the documentation. Now you can chop your own header design into blocks, does this below match?
<div class="head">
<div class="head__user"></div>
<div class="head__nav">
<div class="menu">
<div class="menu__something"><img src="" class="menu__image" /></div>
</div>
</div>
</div>
I think this variant is allowed:
<div class="head">
<div class="head__nav">
<div class="menu">
<div class="head__user"></div>
</div>
</div>
</div>
I haven't found the current part in the official BEM documentation, but I've found this part:
The block name defines the namespace, which guarantees that the elements are dependent on the block (block__elem).
A block can have a nested structure of elements in the DOM tree:
Example
<div class="block">
<div class="block__elem1">
<div class="block__elem2">
<div class="block__elem3"></div>
</div>
</div>
</div>
However, this block structure is always represented as a flat list of elements in the BEM methodology:
Example
.block {}
.block__elem1 {}
.block__elem2 {}
.block__elem3 {}
This allows you to change a block's DOM structure without making changes in the code for each separate element:
Example
<div class="block">
<div class="block__elem1">
<div class="block__elem2"></div>
</div>
<div class="block__elem3"></div>
</div>
The block's structure changes, but the rules for the elements and their names remain the same.
I understand it as there is only one rule about HTML structure for elements in BEM: an element has to be inside its block (it doesn't matter how deep).
One possible problem that I can imagine for this case is using some of BEM tree formats. But if you don't need it, I think there's no problem.
I would consider making the potential head__something into simply something, and then to provide multiple modifications of it. e.g. something--head and something--menu.
<div class="head">
<div class="head__user"></div>
<div class="head__nav">
<div class="menu">
<div class="something--menu" />
</div>
</div>
<div class="something--head" />
</div>
Also, refactoring further, I would consider getting rid of head__nav as it probably does not add any richer semantics than menu.
<div class="head">
<div class="head__user"></div>
<div class="menu">
<div class="something--menu" />
</div>
<div class="something--head">for those cases where you want <code>something</code> directly descending from <code>head</code></div>
</div>
I have a doubts about this HTML structure. Is it correct according to BEM approach?
<div class="boxWithBorder">
<div class="header">
<h2 class="boxWithBorder__element"></h2>
</div>
</div>
To my mind it should look like that
<div class="boxWithBorder">
<div class="header">
<h2 class="header__element"></h2>
</div>
</div>
What keeps elements encapsulated.
Generally we do components and structures, that means structures are compositions of components. It will require nesting so that part is ok. As far as your first approach that is not ok by our standards and not used. block1 should not live inside block2 but block2 has to live inside block1 as it's a nested component. Makes sense? BTW BEM is perfectly fine to use and a lot of frontend devs do it, heavyweights as well, check out csswizardry.com for instance, he got some great articles about BEM
Also I would suggest the following using BEM (or any html/css for that matter) is that skip the camleCase and use "-" instead
<div class="box-with-border">
<div class="header">
<h2 class="header__element"></h2>
</div>
</div>
<div class="hero hero--red-with-border">
<h1 class="hero__title>Title...</h1>
<p class="hero__body-text">Text...</p>
</div>
Let's say I have a product within a collection. Is it appropriate to call the product "feature-collection__product" so it's still an element within the block of "feature-collection" or call it "feature-collection-product" so it becomes it's own block, as it has other elements within it, or something different.
<div class="feature-collection">
<div class="feature-collection__product">
<h2 class="feature-collection__product-title"></h2>
<h2 class="feature-collection__product-price"></h2>
</div>
</div>
OR
<div class="feature-collection">
<div class="feature-collection-product">
<h2 class="feature-collection-product__title"></h2>
<h2 class="feature-collection-product__price"></h2>
</div>
</div>
Most likely the correct answer is both:
<div class="feature-collection">
<div class="feature-collection__product product">
<h2 class="product__title"></h2>
<h2 class="product__price"></h2>
</div>
</div>
The situation when you have different entities on the same DOM node is called mix. In this case it's reasonable to have independent block product and also an element of feature-collection to set some styling for production inside feature-collection.
For more info about mixes please take a look at https://en.bem.info/methodology/key-concepts/#mix and https://en.bem.info/methodology/faq/#mixes
I need a little advice on nesting HTML elements. I have a general structure where X contains a New section (where I'll add form elements for creating an X) and a List section that displays whatever Xs already exist:
<div id="X">
<div id="New">
...
</div>
<div id="List">
...
</div>
</div>
now it turns out I need to have two on the page:
<div id="X">
...
</div>
<div id="Y">
...
</div>
so whilst I can differentiate the 2 News like this:
#X #New {} /* in CSS */
$('#Y #New') /* in jQuery */
I'm left with the queasy feeling that #New should be unique in the document... so one alternative might be:
<div id="X">
<div id="XNew">
...
</div>
<div id="XList">
...
</div>
</div>
but then the notion of using X for nesting (as a namespace) seems moot since I've flattened the names... or perhaps another:
<div id="X">
<div class="New">
...
</div>
<div class="List">
...
</div>
</div>
but here I'm using a class where I really mean to designate a single element. How do you all do this sort of thing?
IDs should always be unique. That's why they're called identifiers. Using a class is the correct solution in this case. Then you can easily reference the elements via #X .New and #x .List for example. This also makes it easier to apply common styles and behaviour to all List and New divs.