I've been working through this article to organize my front-end code. It references articles on BEM / SMACSS, which in turn reference other articles.
I'm really trying to understand what the best practices are ... I have a few days to lay down this "standard", and then have to burn through a high-priority / high-visibility / limited timeline project. As such, I'd like to ensure I'm starting with a foundation that will scale as my app does.
Given a component named segment
... I'm creating pages that will have ~10 segments
per page. Each segment
will share the same base class. However, certain segments
will have modifiers on them (different background colors). Further, elements within each block (in the BEM approach) will have modifiers as well. To demonstrate, here is a simple implementation (with only a single element arrow
, while my full site will have 4-5 elements in each segment
):
.segment__arrow {
position: absolute;
bottom: -24px;
left: 50%;
margin-left: -11px;
background-position: center no-repeat;
height: 21px;
width: 21px;
z-index: 2;
}
.segment__arrow--orange {
@extend .segment__arrow;
background-image: url('/images/arrow_orange.png');
}
.segment__arrow--white {
@extend .segment__arrow;
background-image: url('/images/arrow_white.png');
}
So this is the single-class approach:
<div class="segment__arrow--white"> ... </div>
Alternatively, I could go with the multi-class approach:
.segment__arrow {
position: absolute;
bottom: -24px;
left: 50%;
margin-left: -11px;
background-position: center no-repeat;
height: 21px;
width: 21px;
z-index: 2;
}
.segment__arrow--orange {
background-image: url('/images/arrow_orange.png');
}
.segment__arrow--white {
background-image: url('/images/arrow_white.png');
}
Where my markup would be:
<div class="segment__arrow segment__arrow--white"> ... </div>
Third, I could do:
.segment__arrow {
position: absolute;
bottom: -24px;
left: 50%;
margin-left: -11px;
background-position: center no-repeat;
height: 21px;
width: 21px;
z-index: 2;
}
.segment__arrow.orange {
background-image: url('/images/arrow_orange.png');
}
.segment__arrow.white {
background-image: url('/images/arrow_white.png');
}
And then my class def would be
<div class="segment__arrow orange"> ... </div>
Perhaps even namespacing orange with something like sa_orange
to avoid loose selectors.
The end result I'm looking for is less code / easier to maintain (which probably rules out the single class approach (option 1), as described in this great article on scalable architecture. The multi-class approach (option 2) results in a lot of verbosity (especially in my HTML), but is extremely explicit. The third uses a multi-class approach, but doesn't use namespaced modifiers (like .segment__arrow), so is slightly less verbose (but also less explicit & higher potential for collisions).
Does anybody with real world experience with this stuff have any feedback on the best way to approach this?
.orange
has no meaning as a modifier alone. I don't see a big problem in using.segment__arrow--orange
along with your multiclass approach. Of course in bigger projects your HTML could get a little bit out of control (lots of classes).