I am trying to wrap my head around BEM naming convention. I am stuck at this. I may misunderstanding something, lets see.
I have a sidebar nav and a content nav.
<div class="sidebar"> <ul class="sidebar__nav"> <li class="nav__item"><a href="#" class="nav__link">LINK</a></li> <li class="nav__item"><a href="#" class="nav__link">LINK</a></li> </ul> </div>
<div class="content"> <ul class="content__nav"> <li class="nav__item"><a href="#" class="nav__link">LINK</a></li> <li class="nav__item"><a href="#" class="nav__link">LINK</a></li> </ul> </div>
Now I will run into a problem if I style .nav__item, they occur in both my navigations and should not have the same styling. Should I do some nesting here, or am I naming my blocks and elements wrong?
Nesting example in CSS:
.content__nav .nav__item { background: Red; }
Or should I name it like this:
<li class="content__nav__item"><a href="#" class="content__nav__link">LINK</a></li>
Can you help?
BEM names intentionally use double underscores and double hyphens instead of single to separate Block-Element-Modifier. The reason is so that single hyphens can be used as word separators. Class names should be very readable, so abbreviation isn't always desirable unless the abbreviations are universally recognizable.
The BEM approach ensures that everyone who participates in the development of a website works with a single codebase and speaks the same language. Using proper naming will prepare you for the changes in design of the website.
What is BEM? BEM is a front-end naming method for organizing and naming CSS classes. The Block, Element, Modifier methodology is a popular naming convention for class names in HTML and CSS. It helps to write clean CSS by following some simple rules.
In CDD (component driven development) BEM might be considered obsolete, but a naming convention is still recommended. This counts for any language, including CSS.
There are a number of variants on how to write BEM classes, so be aware that are multiple competing standards. It started as a set of loose guidelines. Since posting this answer, Yandex has significantly overhauled their official standard (it's quite an improvement). The version of BEM I use is based heavily from an article by Nicolas Gallagher.
At this point I use "Atomic OOBEMITLESS", which is really just a way of saying that classes are modular and namespaced, selectors have low specificity, and states may be toggled with classes which allows CSS to be scaled, on top of a CSS preprocessor to make the code more concise and expressive.
All that said, I will be using the following BEM standard:
foo-bar
__
followed by hyphenated class names for elements:foo-bar__fizz-buzz
--
followed by hyphenated class names for modifiers:foo-bar--baz
, foo-bar--baz__fizz-buzz
, foo-bar__fizz-buzz--qux
BEM short form: block-name__element-name--modifier-name
You have three different blocks in your examples:
sidebar
, which has a sidebar__nav
elementcontent
, which has a content__nav
elementnav
, which has nav__item
and nav__link
elementsThe sidebar
and content
blocks appear to be variations on the same block, and could be represented as .region.region--sidebar
and .region.region--content
.
The nav
block is implicit on the ul
element, and you should make it explicit:
<ul class="nav"><!-- could be content__nav or sidebar__nav as well if you choose --> <li class="nav__item"><a href="#" class="nav__link">LINK</a></li> <li class="nav__item"><a href="#" class="nav__link">LINK</a></li> </ul>
Once you've specified that the ul
element is a nav
block, it makes sense to have the nav
block be modified:
<ul class="nav nav--content"> <li class="nav__item nav--content__item"><a href="#" class="nav__link nav--content__link">LINK</a></li> <li class="nav__item nav--content__item"><a href="#" class="nav__link nav--content__link">LINK</a></li> </ul>
Once you've set up the CSS classes, styling all nav__item
elements is simply:
.nav__item { ...styles here... }
and overriding those styles for the content nav__item
elements is:
.nav--content__item { ...styles here... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With