Hi there,
I know one of the benefits of BEM is not having to nest styles but I was wondering when is it ok to do so? Sometimes it's unavoidable and sometimes maybe more practical to do so?
In the example I'm working on I have bands/stripes than contain blocks of content, these bands can be different colours (black, white, blue). I achieve this by having a base class of <div class="band">
, which the band is black the following class is added: <div class="band band--black">
. For obvious readability issues the content of a darker block will need to be coloured differently, the headings white, paragraphs/lists a lighter grey and maybe even a subtle lightening of the anchor colour. It'd be a pain to apply classes to everything, especially if different content mark-up appears on different pages. So is it acceptable to write the CSS like this (using SCSS):
.band--black {
h1,
h2,
h3,
h4 {
color: rgb(255,255,255);
}
p,
ul,
ol {
color: color: rgb(200,200,200);
}
}
or even this, which might be able to keep track of, keeping the colour changes grouped together:
h1,
h2,
h3,
h4 {
.band--dark & {
color: rgb(255,255,255);
}
}
p,
ul,
ol {
.band--dark & {
color: rgb(200,200,200);
}
}
This is some (basic) sample HTML:
<div class="band band--dark masthead">
<div class="wrap">
<h1 class="masthead__title">Page heading</h1>
<ul class="breadcrumbs">
<li class="breadcrumbs__item"><a href="#" class="breadcrumbs__link" title="TITLE">Home</a></li>
<li class="breadcrumbs__item"><a href="#" class="breadcrumbs__link" title="TITLE">Section Name</a></li>
</ul>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut volutpat accumsan purus, in posuere nibh aliquam faucibus. Nam varius lectus id nunc ullamcorper, at euismod enim placerat.</p>
<h3>Related Categories</h3>
<ul>
<li><a href="#" title="TITLE TEXT">Category 1</a></li>
<li><a href="#" title="TITLE TEXT">Category 2</a></li>
<li><a href="#" title="TITLE TEXT">Category 3</a></li>
<li><a href="#" title="TITLE TEXT">Category 4</a></li>
</ul>
</div>
</div>
Another question I have is the class of masthead
, part of me thinks this should really be band--masthead
following the BEM spec?
Thanks in advance, just starting off with BEM so I keep find myself questioning most of my implementations of it!
Hi, @moymadethis!
You are right, nesting should be avoided wherever possible but still can be quite useful when inner parts of block should be styled by parent modifier. See https://en.bem.info/faq/#why-should-i-avoid-using-nested-selectors for some examples.
<div class="band band--dark masthead">
is absolutely fine. It's calledmixes
in BEM. See https://en.bem.info/method/key-concepts/#mixThanks, that's great.
I have another question, not really related but I don't want to pollute the forum with loads of my little queries!
My default button styling is done using a class of
.btn
, using BEM I'll specify different button styling usingbtn btn--dark
. I guess if a button has a unique graphic/icon on it (Shopping cart for example) I would use the classbtn btn--basket
? Or would a class ofbtn-basket
be more appropriate?In the code below, I have a series of buttons which are included in a listing for a product:
These buttons have no text and are square icon based buttons, so I've not used the
btn
class. As each button has a totally different icon, would it be better to use a class ofproduct__share
instead ofproduct__btn--share
- or do you think the above is acceptable? I suppose the same could able to list-items where unique classes are needed for styling.There are actually 5-6 buttons in totally, I've just truncated the code for this example. There are also more
product__item
elements, hence theproduct__item--options
class - maybe I'd be better usingproduct__options
on that too?Thanks again!
Feel free to do so ;)
You may consider using universal
icon
block with proper modifiers for each icon. And reuse it everywhere (insidebtn
as well). Don't be afraid of extra DOM nodes or classes — they all will be gziped well.That sounds like an idea, so something separate from the
btn
class. I use some icon fonts, so maybebtn-icon
might work better? Then modifiers would bebtn-icon btn-icon--basket
,btn-icon btn-icon--delete
,btn-icon btn-icon--close
for example?When you modify and element, should the children be modified as well or is that a bit over the top? It seems like this is where nesting is perfectly fine?
For example on this pagination it would be unnecessary to add a class of
pag__link--inactive
onto the anchor?Also, if SCSS is used, I dunno if it would be better to write this nested like this:
The issue being I guess the parent
current
/inactive
has to attachment to thepag__item
when it's converted to CSS. Unlike.pag__item.current {}
/.pag__item.inactive {}
?Thanks again!
It's just fine but in the future you may also need an icon somewhere else not only inside a button. So it's also possible to create
icon
block beforehand and use it as<a class="btn icon icon--close">
. Which gives you possibility to keep button styles separate from styles about icons but still apply them on the same DOM node.You are right, nesting is just ok for such cases. But shouldn't it be
pag__item pag__item--current
instead of justpag__item current
ascurrent
looks just like a modifier of an item (and same forinactive
)?I'd sacrifice brevity for obviousness and keep parent first (children shouldn't know about parent but for parent it's ok to know about children):
I used fontello to create @font-face, so when I add a class of
icon-tick
,icon-mail
,icon-trash
etc and icon is applied to the pseudo:before
class and displayed alongside the element. Primarily I use this on headings, lists and buttons when text is also visible alongside the icon.The icons I originally mentioned would be square buttons without any text and applied via a background-image rather than the icon font method. So I don't know if the
icon
class would clash with this (I suppose there's no --, so they would be deemed separate).I'm not sure if this is right but I'm always a bit cautious using icon fonts on important form elements or elements that are vital to the functionality of the page - maybe I shouldn't be. But I always worry that if the icon isn't displayed in a certain browser and there's no text visible the user has no idea what the button does. So a background image always seemed like a safer approach? As no text would be visible, I guess the only thing the icon would need to inherit from a
btn
would be the background-color and maybe the height. Also as the background-image they 'icons' would use is a sprite, so I don't think they'd ever be applied to an item with a flexible width, incase another part of the sprite became visible.I thought that too and actually used
pag__item--current
andpag__item--inactive
to begin with. Then I thought of them more as 'states', so decided to simplify it. This is probably breaking the rules though and for consistency it's probably best to use the method you mentioned. I guess this would be suitable fornav__item--has-dropdown
as well?Modifiers are applied to states as well.
Yes, sure.