8

I've started using BEM methodology to decouple my HTML and CSS ... and it works pretty well most of the time. Even if its only your personal opinion, i would still like to know how others deal with this:

Let's assume we need to build a simple navigation. The HTML looks similar to

<nav class="nav">
    <ul class="nav__list">
        <li class="nav__item">
            <a class="nav__link" href=""></a>
        </li>
        <li class="nav__item">
            <a class="nav__link" href=""></a>
        </li>
    </ul>
</nav>

I'm not sure if i need the ".nav_item" and ".nav_link" or if it's better pratice to use this instead

.nav__list > li { CODE }

But my real issue is how to deal with "active" classes (not just for navigations, but in general). Is it better to use specific "active" classes like ".nav_item--active", so you can just use a single class inside your CSS file or if using more general class names like ".is-active" works better? But then you need to specify your classes inside your CSS file like ".nav_item.is-active" or (which looks even worse to me) ".nav__list > .is-active".

Every method has its downsides. To me the second way looks wrong if using BEM, but if you are going for the first way you run into "troubles" with your JS, because you need to "hard-code" the specific class name into your JS

someElement.addClass(".nav__item--active");

That way your JS relies too much on your HTML structure (or doesn't this matter too much?), which might change... And this leads to the second question. I heard that it's good to decouple not only your HTML and CSS but also your HTML and JS. So you could for example use those ".js-" classes to add click events and all that kind of stuff to elements instead of using your "styling" classes to trigger those kind of events. So instead of using

<button class="btn btn--large"></button> // $(".btn--large") in jQuery

you would have

<button class="btn btn--large js-dostuff"></button> // $(".js-dostuff") in jQuery

I think this in combination with HTML5 data-attributes works for pretty much for anything, but i'm asking myself what happens to navigation or accordions or stuff like that. Is it better for maintainability to use those ".js-" classes as well (for every item)

<nav class="nav">
    <ul class="nav__list">
        <li class="nav__item js-open-subnav">
            <a class="nav__link" href=""></a>
            <ul class="nav__sub">
                <!-- ... -->
            </ul>
        </li>
    </ul>
</nav>

or should i use $(".nav__item")... in my JS in this case? But that way you don't really decouple your HTML and JS (at least as far i understood this topic). It's not just about navigations, but about all those kind of javascript interactions, like accordions, sliders and so on.

I'll hope you guys can share some best practices for those questions and help me out.

Thanks

mrksbnch
  • 1,792
  • 2
  • 28
  • 46
  • 1
    Isn't it redundant to have both `nav` and `nav__list`. Applying `nav` to the ul would be better. Also I don't think `nav__link` and `nav__item` provide much help, because our navigations are a list anyway. Using `nav > li` is just fine imo. – kleinfreund Jan 01 '14 at 15:21
  • @kleinfreund Thanks, i think you are right with nav__link and nav__item being too much. But if i don't use classes for the
  • I'm still not sure how to name the active classes (as nav__item--active wouldn't make sense anymore). And i don't really like not specific active classes like "active" either, because then i would need to have something like ".nav > li.active > a" in my CSS and all of my different .active classes would probably get mixed up someday.
  • – mrksbnch Jan 01 '14 at 19:07
  • 1
    I don't see a big problem in using `nav__item--active`. It's loose enough to bei either a `li` or a `span`, what so ever and but tells you exactly what's going on. Sure, it suggest there eventually is a `nav__item` as well and there is, but not as a assigned class. __Edit:__ Also I was talking about a `nav`-class for ul's, not the `nav`-tag. – kleinfreund Jan 01 '14 at 20:53
  • @kleinfreund Thanks again, now i got your point. I agree that specific classes for the
  • and don't provide much help. Totally overseen your comment on the .nav-class for the ul though. I'll need to think about that since even the
  • – mrksbnch Jan 02 '14 at 09:24
  • 1
    Seeing it this way the `ul` would be the abstraction of the navigational concept and `nav` the wrapper used for styling: ` – kleinfreund Jan 02 '14 at 09:33
  • 4
    `nav > li` isn't very OOCSS because you're implying the elements that will be used. The CSS should work whether the HTML uses `nav` and `li`, `div` and `a`, `div` and `div` and so on. That's why `nav__item` and `nav__link` would be preferable. – howard10 Apr 03 '14 at 16:46
  • @kleinfreund "I don't think nav__link and nav__item provide much help, because our navigations are a list anyway" : as pointed out by howard10, the main advantage of this technic is that you decouple whatever HTML tag you use from the style. Makes you code easier to maintain & refactor. – Adriano Sep 08 '14 at 11:11
  • 1
    I agree with @howard10, the same would be true for headings. For example: `.banner__h2` should be `.banner__heading` as heading structures can often change to aide the document outline or some SEO benefit. – nickspiel Jan 16 '15 at 23:11