--- comments: true date: 2014-05-21 16:40:12 layout: post slug: grouping-related-classes-in-your-markup title: Grouping related classes in your markup categories: - Web Development tag: meta: "An interesting way of visually and ‘physically’ grouping multiple class attributes" --- **N.B. 2016-02-23** I no longer advise this technique—it proved far too problematic on teams where people we unfamiliar with the syntax. If you do wish to clearly delimit classes in your markup, perhaps opt for something a little less alien:
- - - A few weeks back, [I put out a Tweet](https://twitter.com/csswizardry/status/439383832920604672) to [a jsFiddle](http://jsfiddle.net/csswizardry/m2qa9/) describing an idea I’d had in which I grouped two or more related class attributes to make them easier to notice when scanning an HTML file, and—coincidentally—trivial to manipulate very effectively with Vim, my text editor of choice. I was grouping them by enclosing them in square brackets, and that looks something like this:
Since sharing the demo, I’ve used this method on a few builds, and people on [Twitter](https://twitter.com/csswizardry) have asked me about the presence of the brackets, so I thought I’d write it up into a full article. Before we get into things too far though, there are a couple of things I’d like to say up front. Firstly, after I shared the link to the jsFiddle, I got a few replies about a much earlier method [along similar lines](http://beneverard.co.uk/blog/using-slashes-within-the-html-class-attribute/) from [Ben Everard](https://twitter.com/_beneverard), so the idea of visually _separating_ classes isn’t all that new. Where this method differs, however, is the concept of _grouping_ them, as opposed to just delimiting them. More on that later. Secondly, and more importantly, **this article is not a direct endorsement of, or recommendation for, this method**, this is just a writeup of an idea. It has benefits and drawbacks—as do most things—so your mileage _may_ vary. I will discuss relative merits and pitfalls later in the article so that you can decide whether this is a good thing _for you_, rather than it being a good or bad thing _in and of itself_. ## How it works There is no hard and fast rule as to how and when to begin grouping your classes, but the guidelines I’ve set for myself are: * There must be more than one ‘set’ of classes. * One ‘set’ must contain more than one class. This basically just ringfences any groups that _need_ it, for example:
<!-- Only one set. Nothing needs grouping. -->
<div class="foo  foo--bar">

<!-- Two sets, but only one class in each. Nothing needs grouping. -->
<div class="foo  bar">

<!-- Two sets, one of which contains more than one class. This set needs grouping. -->
<div class="[ foo  foo--bar ]  baz">

<!-- Two sets, both of which contain more than one class. These sets needs grouping. -->
<div class="[ foo  foo--bar ]  [ baz  baz--foo ]">
If these rules seem a little convoluted—and they might—feel free to experiment with your own. You could simplify it right down to ‘any groups always need enclosing’, like so:
How you group them can be entirely your choice, the concept here just deals with the fact that we’re grouping things _at all_. It’s important to note here that I’m using [BEM-style naming](/2013/01/mindbemding-getting-your-head-round-bem-syntax/), which already starts to group classes anyway. If you’re not using BEM, your HTML might look like this: