I started using the BEM methodology to separate my HTML and CSS ... and it works pretty well most of the time. Even if this is only your personal opinion, I would still like to know how others handle this:
Suppose we need to build a simple navigation. HTML looks just like
<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 .nav_item and .nav_link, or if it is better to use this instead
.nav__list > li { CODE }
But my real problem is how to deal with “active” classes (not only for navigation, but in general). Is it better to use special "active" classes like ".nav_item - active", so you can just use one class inside your CSS file or use more common class names like ".is-active" to work better? But then you need to specify your classes inside your CSS file, for example, ".nav_item.is-active" or (even worse) ".nav__list> .is-active".
Each method has its drawbacks. For me, the second method looks wrong if you use BEM, but if you are going to encounter “problems” with your JS in the first way, because you need to “hard code” the name of a particular class in JS
someElement.addClass(".nav__item--active");
So your JS relies too much on your HTML structure (or is it not too much?), Which could change ... And that leads to the second question. I heard that it’s nice to separate not only your HTML and CSS, but also your HTML and JS. Thus, you could, for example, use these .js- classes to add click events and all such things to elements, instead of using your “styling” classes to trigger such events. Therefore, instead of using
<button class="btn btn--large"></button>
you will have
<button class="btn btn--large js-dostuff"></button>
I think this, combined with HTML5 data attributes, works for almost anything, but I ask myself what happens to navigation or accordions or the like. Is it better to use these ".js-" classes (for each element) for convenience
<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 this way you really do not separate your HTML and JS (at least as far as I understand this topic). This is not only about navigation, but also about all these javascript interactions, such as accordions, sliders, etc.
I hope you guys can share some recommendations on these issues and help me.
thanks