My mind is on CSS quite a bit these days. At Shopify, I’m jumping into projects that already well under way. As a result, it’s been a great way to look at what I wrote in SMACSS and see how applicable it is to yet another project. (As if Yahoo! wasn’t already enough of a testing ground.)
With Yahoo!, I (and a team of people) were writing the CSS from scratch and creating our mental map of the project as we went along. Jumping into the middle of a project as I am at Shopify, I have to try and figure out why things are done the way they are.
Here’s an example of something that I ran into in the CSS:
#loading-header .loading { background: url(spinner.gif) no-repeat 0 0; }
[...separated by a few pages of code...]
#content {
[...separated by more code...]
#loading-header { display:none; }
.row { display:block; }
&.loading {
#loading-header { display:block; }
.row { display:none; }
}
}
The loading class has a spinner. Got it. But wait, there’s a loading class inside an element with an ID of loading-header and another loading class that gets added to the element with an ID of content. Are they the same somehow? Is there some inheritence that is taking place?
As it turns out, no. These two loading classes were named exactly the same thing but applied in two different contexts. They really had no relation at all except that the #loading-header (and thus, #loading-header .loading) is only visible with #content.loading. (The &.loading is part of SASS and adds #content in place of the ampersand.)
One class is a state. Because it’s a state, I prefer to prepend all my states with “is-“. In this case, it would be “is-loading”. The question asked when I first mentioned this is, “isn’t the ‘is-‘ redundant?” But as you can see from the example, it isn’t. The naming convention clarifies its intent.
In this case, I had to determine intent based on the CSS. Other times, you’re looking at the HTML first.
<div id="content" class="loading">
<div id="loading-header">
<div class="loading">
Where do you begin to look in the CSS to find what you’re looking for? You’d probably start with a search for “.loading”. Now you’re left trying to figure out which loading class applies to what element. The swaths of CSS that separated the relevant parts made it harder to see what was going on.
The simple answer to this, of course, is not to naming two different things with the same name. However, I believe a solid naming convention clarifies intent and makes the project easier to understand. We’ve long applied naming conventions to programmery things like JavaScript and PHP. The same should go for CSS.