My thoughts on LESS

CSS, Tools | July 19th, 2010
My thoughts on LESS Photo by magnus*

I’ve started using LESS a few months ago on a few projects. LESS allows you to extend the way you write CSS, letting you use variables, nested selectors, operations and mixins. It sounds great — and it is great — but there are a few things that can make it work against you. These are some of my thoughts on LESS.

What is LESS?

LESS — Leaner CSS — is, in the authors’ own words, a “new version of CSS” (better yet, of writing CSS), that is then compiled into “traditional” CSS using the LESS compiler.

There are 4 main things you can do with LESS that you can’t with normal CSS:

1. Variables

You can define a variable, such as “@text-color”, and use it throughout your stylesheet. If the colour of your text changes, you only need to change it once in your CSS — where the variable is initially defined.

2. Nested selectors

With nested selectors, instead of doing this and repeating yourself:

aside h1 { font-size: 24px; }
aside h2 { font-size: 18px; }

You can have:

aside {
	h1 { font-size: 24px; }
	h2 { font-size: 18px; }
}

3. Operations

You can add, subtract, divide and multiply using operations. Here’s a quick example:

@wrappers: 600px;

aside {
    width: @wrappers / 2;
}

This is definitely not the best example, but it should be enough for you to get the point.

4. Mixins

Mixins work a lot like variables, but you can specify a whole class in one. For example:

.b-radius {
	-moz-border-radius: 5px;
	-webkit-border-radius: 5px;
	border-radius: 5px;
}

.box {
	.b-radius;
}

From these examples, I suppose you can say that LESS is actually more. (I’m sorry, I couldn’t help myself.)

This is not a “how to” guide

The official LESS documentation is very clear on how to install it. I have to admit I usually stop reading instructions whenever “Terminal” is mentioned (and I know more people do), but I guess most CSS authors will be OK with it — if not, ask for help to your nearest web dev (or email me and I’ll give you the phone number of one).

After it’s installed, you’ll have two files: the .less file — this is the file you’ll be working with — and the compiled .css file.

There’s also LESS app, that makes it easier to use LESS. And I suppose that some CMS and servers let you use LESS directly, but the point of this article is not to teach anyone how to install it — it’s to go through its good and bad points.

On to the important bit…

How LESS can make your life easier

This is a list of things that, in my opinion, make LESS worthwhile and can really simplify your CSS. These are the things that I usually miss the most when I’m not working on LESS files.

1. Variables

Surely when you are writing your CSS there are colours, measurements, etc. that you keep using over and over. For example, let’s say your brand colour is jade (very fashionable for Spring/Summer 2010). You may use it on various elements in your design, for example, the main text:

body {
    …
    color: #77b59d;
    …
}

Or the borders of your posts:

article {
    …
    border-bottom: 1px solid #77b59d;
    …
}

Or as the background of your footer:

footer {
    …
    background-color: #77b59d;
    …
}

If someone has to edit the CSS file and doesn’t know exactly which colour he or she is supposed to be using — it may even be you a few months later —, what happens is that either you’ll spend some time scanning through the document until you find the colour, you’ll use Firebug or, if you like to complicate things, use xScope or take a screenshot and use Photoshop to detect the colour.

This is silly. You’ll end up with many similar colours instead of just one. (As a note, Dreamweaver lists all the colours that a site is using — you’d be surprised at how inconsistent some CSS authors can be when applying colour.)

Another thing that may happen is when you actually want to change this colour throughout your stylesheet: you’ll have to edit every instance of it — that’s painful.

With a variable, you can have this:

@main-color: #77b59d;

body {
    …
    color: @main-color;
    …
}

article {
    …
    border-bottom: 1px solid @main-color;
    …
}

footer {
    …
    background-color: @main-color;
    …
}

Beware though: you can easily go overboard with variables. I have to confess I was inebriated in the beginning by the thought of never having to declare a colour, a padding value, etc. again, and started adding too many variables to my LESS files. It gets messy and complicated quickly. You’ll have to find a balance.

2. Accessors

If you only need to access something once, you probably don’t need a variable. If you’re pretty sure that that thing you need to access is, and will always be, directly linked to whichever thing you’re linking it to, you can use an accessor.

Let’s say you have a block with a heading and some text. You want the border of that box to always be the same colour as the heading. You can have this:

#sales {
    border-color: #ae7b2a;
}

#sales h2 {
    color: #sales['border-color']; 
}

Using accessors may be going a step too far in some cases, but in other cases it makes a lot of sense — I think they’re quite useful.

3. Mixins

One example: gradients. Let’s face it, they are an absolute pain to write. Take a look at how LESS makes it easier for you:

The mixin:

.grad-linear (@start:"", @end:"") {
    background: -webkit-gradient(linear, left top, left bottom, from(@start), to(@end));
    background: -moz-linear-gradient(-90deg,@start,@end);
}

Using the mixin in your .less file:

nav {
	.grad-linear(#b3cdcc,#718a89);
}

What your compiled .css file will show:

nav {
	background: -webkit-gradient(linear, left top, left bottom, from(#b3cdcc), to(#718a89));
	background: -moz-linear-gradient(-90deg, #b3cdcc, #718a89);
}

Mixins are great for properties that still require that we add some vendor-specific code (like border-radius) or for the latest CSS3 gradients, transitions, etc.

I have to say mixins, especially when used for gradients and border-radius, are my favourite part of LESS.

4. C-style comments

Some people prefer these to typical CSS inline comments. I’m OK with either, but if you’re using LESS, you can have either this:

/* A normal CSS comment */

Or this:

// A C-style CSS comment

5. Nesting (in some cases)

Nesting is one of those things that makes you think LESS is amazing at first. Then you start using LESS and start nesting selectors, and change your mind.

The rare instance where I think nesting is useful is with link states in simple selectors. In LESS you can have this:

a {
    :link, :visited { text-decoration:none; color:#edd273; font-weight:bold; }
    :hover, :active, :focus { color:#edd273; border-bottom:1px solid #edd273; }
}

And the output will be this:

a:link {
  text-decoration: none;
  color: #edd273;
  font-weight: bold;
}
a:visited {
  text-decoration: none;
  color: #edd273;
  font-weight: bold;
}
a:hover {
  color: #edd273;
  border-bottom: 1px solid #edd273;
}
a:active {
  color: #edd273;
  border-bottom: 1px solid #edd273;
}
a:focus {
  color: #edd273;
  border-bottom: 1px solid #edd273;
}

There’s a lot of repetition in here, I know (I’ll mention that on the next section), but it’s nice nonetheless.

How LESS can easily become a nightmare

This is a list of things that I would either avoid doing in LESS or that just don’t work the way you’d expect them to (so, annoyances).

1. Nesting (in most cases)

As I just mentioned in the previous section, nesting looks nice at first, but it doesn’t work flawlessly.

One of the things that I’ve noticed for example, is that, even though you can have this:

nav {
  ul {
    li a {
      :link { color: red; }
    }
  }
}

This won’t work (notice that now I’m using a direct child selector instead):

nav {
  ul {
    li > a {
      :link { color: red; }
    }
  }
}

For this last bit of CSS to work, you’ll have to do it like this (notice the extra “&”):

nav {
  ul {
    li > a {
      &:link { color: red; }
    }
  }
}

This not a problem with LESS, I guess it’s just not a very intuitive way of doing it just for using a different type of selector.

The main thing, however, that I think makes nesting a poor choice in most cases, is how it can make a mess out of specificity [here's a post I've written previously on specificity and other matters].

Your first instinct is to nest everything inside the main blocks, like aside, #sidebar, #news, etc. But then you realise you need to reutilise a piece of the CSS for another area on the site. You make another nested selector for that new block, because you don’t want to go over that first nested block of CSS that you created in the first place. You’ll be left out with numerous redundant selectors, with very long selectors (because you don’t notice this until the compiler phase), and with the need to create overly specific selectors to override your initial ones (that shouldn’t have been so specific to begin with).

Nesting selectors is one of those cases where the phrase “with great power comes great responsibility” makes a lot of sense. Use it wisely and carefully.

2. Changes made to the compiled file

This isn’t a problem with LESS per se, but rather with your working process.

Even if you are the only person in charge of the CSS on a given project, it’s possible that someday you will leave, or hand the project over to someone else, so it’s a good thing to make sure that no-one is going to edit the compiled .css file directly instead of the .less one.

If this happens, and then after a while someone does use the .less file, when it compiles, it will override the changes made to the .css file. Then you’ll have to go back and fix it.

This is just a general problem with people: if there’s a chance that we can make a mistake, we will make it.

3. Formatting

The .css file that results from the compiler gives you back your CSS with no comments and single-line declarations (that have more than one property). It would be useful if you could have some control over this. For example, you may want to minify the CSS (although you could argue that this should be a completely separate step), keep comments in, have multi-line CSS, group selectors, etc.

I’m not entirely sure if this has been resolved, so if someone knows better, do let me know.

4. Using Firebug

I can’t work without Firebug, and one of the best things about it is knowing exactly which line you’re looking at. If you’re using LESS, that’s broken: Firebug shows you the line of the compiled .css file, not the original .less file you’re editing, which is basically useless.

Conclusion

I love LESS and I think it’s a pleasure to use, until you start trying to be too clever and things start getting messy. My advice is to use it with care, take advantage of its niceties but don’t go overboard.

I know there are lot more problems (and probably good things too) that I haven’t referred. These are the ones that I’ve personally came across with; I try not to use LESS on larger projects and I usually don’t use @import on my stylesheets (especially smaller projects) — which I know can cause some problems.

I’d love to know what your thoughts are on this: have you used it? Why, or why not? Which problems have you encountered? Do you find it helpful or just plain silly?

Your Comments:

Have your say!

Note: * Denotes a required field (don't worry: your email will not be published)

Smashing Magazine Network