LESS CSS – Beginner’s Guide

Like we said in our previous post on CSS Style Priority Level; CSS is very simple and straight forward. However, it also has its limitations, particularly when it comes to its maintenance. When we have thousands of lines of styles and some separated CSS files to maintain, things will problematic.

To solve the problem, a CSS pre-preprocessor is created to allow us to compose styles in a more dynamic way. There are a few solutions, but the one that we are going to cover this time is LESS.

LESS Editor

In the time of the writing, the only editor that was intended specifically to support LESS file editing is the ChrunchApp. This app is cross-platform, built upon Adobe Air, so you can run it on Windows, Mac OSX and Linux.

As for the other code editors that we may be familiar with, like Dreamweaver, Coda, NotePad++ and SublimeText 2, there is a sort of “plugin” or package we can add to enable code highlighting for LESS file format.

And here are a few links to do that:

Using LESS

Using LESS is really easy. We only need this two lines inside the head tag of your HTML document. First, the .less file then the less.js library at the bottom (which you can download it from the official site).

<link rel=”stylesheet/less” type=”text/css” href=”style.less”>
<script src=”less.js” type=”text/javascript”></script>

However, this time we will show you how to do it the other way. We are going to use a LESS compiler, you can use WinLess for Windows or LESS.app if you are running on Mac OSX. Go to the site, download and install it in your machine.

Then, create a .less file in your working directory.

Open the compiler (I’m referring to the WinLESS or LESS.app), and import your working directory into it, the compiler will then find any .less file inside the directory.

That’s it. We are all set up now, and whenever we make a change and save the .less file, the compiler will automatically generate the regular CSS in .css format which the browser can read in the directory.

Now, we only need to link the CSS file to our HTML document, as follows;

<link rel=”stylesheet/css” type=”text/css” href=”style.css”>

LESS Syntax

Unlike regular CSS as we know it, LESS works much more like a programming language. It’s dynamic, so you’ll expect to find some terminologies like Variables, Operation and Scope along the way.


First of all, let’s take a look at the Variables.

If you’ve been working quite long with CSS, you probably often written something like this, where we have repetitive values assigned in some declaration blocks in the entire stylesheet.

  1. .class1 {
  2.     background-color: #2d5e8b;
  3. }
  4. .class2 {
  5.     background-color: #fff;
  6.     color: #2d5e8b;
  7. }
  8. .class3 {
  9.     border: 1px solid #2d5e8b;
  10. }

This practice is actually fine, until we find ourselves having to sift through more than 1000 more similar snippets throughout the stylesheet. This could happen when we build a large scale website, and then things become so tedious when we begin to modify it.

If we are using a CSS pre-procesor like LESS, the instance above would not be a problem, as we can use Variables. The variables will allow us to store a constant value that later can be reused in the entire stylesheet.

  1. @color-base: #2d5e8b;
  2. .class1 {
  3.     background-color: @color-base;
  4. }
  5. .class2 {
  6.     background-color: #fff;
  7.     color: @color-base;
  8. }
  9. .class3 {
  10.     border: 1px solid @color-base;
  11. }

In the example above, we store the color #2d5e8b in @color-base variable. Then. just in case you want to change the color, we only need to change the value in the variable.

You can also put other values in the variables like the examples below:

  1. @font-family: Georgia
  2. @dot-border: dotted
  3. @transition: linear
  4. @opacity: 0.5


In LESS, we can use Mixins to reuse whole declarations in a CSS rule set to another rule set. Here is an example;

  1. .gradients {
  2.     background: #eaeaea;
  3.     background: linear-gradient(top, #eaeaea, #cccccc);
  4.     background: -o-linear-gradient(top, #eaeaea, #cccccc);
  5.     background: -ms-linear-gradient(top, #eaeaea, #cccccc);
  6.     background: -moz-linear-gradient(top, #eaeaea, #cccccc);
  7.     background: -webkit-linear-gradient(top, #eaeaea, #cccccc);
  8. }

In the above snippet, we have preset a default gradients color inside .gradients class. Then, whenever we want to add the gradients we simply insert the .gradients this way:

  1. div {
  2.     .gradients;
  3.     border: 1px solid #555;
  4.     border-radius: 3px;
  5. }

The .box will inherit all the declaration block inside the .gradients. So, the CSS rule above is equal to the following regular CSS;

  1. div {
  2.     background: #eaeaea;
  3.     background: linear-gradient(top, #eaeaea, #cccccc);
  4.     background: -o-linear-gradient(top, #eaeaea, #cccccc);
  5.     background: -ms-linear-gradient(top, #eaeaea, #cccccc);
  6.     background: -moz-linear-gradient(top, #eaeaea, #cccccc);
  7.     background: -webkit-linear-gradient(top, #eaeaea, #cccccc);
  8.     border: 1px solid #555;
  9.     border-radius: 3px;
  10. }

Furthermore, if you are using CSS3 a lot in your website, you can use LESS ELements to make your job much easier. LESS Elements is a collection of common CSS3 Mixins that we may use often in stylesheets, such as border-radius, gradients, drop-shadow and so on.

To use LESS Elements, simply add the @import rule in your LESS stylesheet, but don’t forget to download it first and add it into your working directory.

  1. @import “elements.less”;

We can now reuse all the classes provided from the elements.less, for example, to add 3px border radius to a div, we can write

  1. div {
  2.     .rounded(3px);
  3. }

For further usage, please refer to the official documentation.

Nested Rules

When you write styles in plain CSS, you may also have come through these typical code structures.

  1. nav {
  2.     height: 40px;
  3.     width: 100%;
  4.     background: #455868;
  5.     border-bottom: 2px solid #283744;
  6. }
  7. nav li {
  8.     width: 600px;
  9.     height: 40px;
  10. }
  11. nav li a {
  12.     color: #fff;
  13.     line-height: 40px;
  14.     text-shadow: 1px 1px 0px #283744;
  15. }

In plain CSS, we select child elements by first targeting the parent in every rule set, which is considerably redundant if we follow the “best practices” principle.

In LESS CSS, we can simplify the above rule sets by nesting the child elements inside the parents, as follows;

  1. nav {
  2.     height: 40px;
  3.     width: 100%;
  4.     background: #455868;
  5.     border-bottom: 2px solid #283744;
  6.     li {
  7.         width: 600px;
  8.         height: 40px;
  9.         a {
  10.             color: #fff;
  11.             line-height: 40px;
  12.             text-shadow: 1px 1px 0px #283744;
  13.         }
  14.     }
  15. }

You can also assign pseudo-classes, like the :hover, to the selector using ampersand (&) symbol. Let’s say we want to add :hover to the anchor tag above, we can write it this way:

  1. a {
  2.     color: #fff;
  3.     line-height: 40px;
  4.     text-shadow: 1px 1px 0px #283744;
  5.     &:hover {
  6.         background-color: #000;
  7.         color: #fff;
  8.     }
  9. }


We can also perform Operation in LESS, we can do something like addition, subtraction, multiplication and division to number, color and variable in the style sheet.

Let’s say we want the element B two times higher than element A, in that case, we can write it this way;

  1. @height: 100px
  2. .element-A {
  3.     height: @height;
  4. }
  5. .element-B {
  6.     height: @height * 2;
  7. }

As you can see above, we first store the value in the @height variable, then assign the value to element A.

In the element B, rather than calculate the height ourselves, we can simply multiply the height by 2 using the asterisk operator (*), so whenever we change the value in the @height variable, element B will always have twice the height.


LESS applies the Scope concept, where variables will be inherited first from the local scope, and when it is not available locally it will search up a wider scope.

  1. header {
  2.     @color: black;
  3.     background-color: @color;
  4.     nav {
  5.         @color: blue;
  6.         background-color: @color;
  7.         a {
  8.             color: @color;
  9.         }
  10.     }
  11. }

In the example above, the header has a black background color, but nav‘s background color will be blue as it has @color variable in its local scope, while the a will also have blue that is inherited from its nearer parent, nav.


LESS is only one of a few solutions of CSS pre-processor, you can also try SASS and the Stylus, which we also hope to cover in our future posts.

Ultimately, we hope this post can give you a basic understanding on how we can write CSS in a better way using LESS. If this is your first time using LESS, you maybe feel a little clumsy, but as you try it more often, it will surely become a lot easier.

Related Posts: