Try our mobile site!

LESS - CSS Preprocessor

By Daniel Taylor on March 30, 2011 07:11

Everyone who calls themselves a web designer has quite a bit of experience with Cascading Style Sheets. CSS is extremely simple, including only rulesets and declarations. The only reason CSS exists is to encapsulate the elements to style and how to style them. For the newcomer, it does everything it is supposed to do, but to an experienced developer managing a dynamic website, CSS is a massive limitation on how easily values can be changed and related to one another.

Everyone who calls themselves a web designer has quite a bit of experience with Cascading Style Sheets. CSS is extremely simple, including only rulesets and declarations. The only reason CSS exists is to encapsulate the elements to style and how to style them. For the newcomer, it does everything it is supposed to do, but to an experienced developer managing a dynamic website, CSS is a massive limitation on how easily values can be changed and related to one another.

The feature with the most support from the public is undoubtedly the ability to declare global variables for use throughout the stylesheet. And whenever you have variables, you must also be able to operate on those variables. How many times do you write the same CSS to center a div or apply a font or round the corners of a div? WAY TOO MUCH! So what can we do to fix this?

Web browsers are programmed to only understand the rules and selectors of CSS as it is currently and there are absolutely no plans to integrate these fundamental ideas of any programming language because CSS is markup, not a programming language. So no matter what we do, we must still output CSS. We have to use a preprocessor.

There are several preprocessors available on the market, but SASS and LESS are the most notable. The purpose of these two preprocessors is to compile modified CSS into true CSS readable by browsers. This is often accomplished by either Javascript or PHP, but we will get to that later. This compiling process does take time, but can easily be well worth the few hundred milliseconds to improve both the readability and maintainability of your CSS.

So which one is right for you? Well, there is really no right answer. SASS was designed to minify current CSS by removing unnecessary markup like curly braces. LESS was designed to provide a very quick migration by keeping an extremely similar syntax to CSS. While SASS modifies CSS, LESS extends CSS. So if you are looking for a quick turnover for your website, I would recommend LESS. Because I believe LESS is more applicable to beginners, this article will focus solely on the integration of LESS into your website.

Installation

So how do you install LESS into your website? Well, there are actually several different ways to install a preprocessor like LESS. The two most frequently used variations are through PHP and Javascript. Unless you are maintaining your own server, you will most likely use Javascript. All you have to do to install LESS into any webpage is use a simple link in your head tag:

<link type="text/css" rel="sylesheet/less" href="main.less" />
<script src="scripts/less.js"></script>

You can even use the version of LESS on the Google Code Server by replacing "scripts/less" with http://lesscss.googlecode.com/files/less-1.0.30.min.js. And that's all there is to it. Now we can convert our CSS files into LESS files by simply changing the extension to .less.

So, now that we have LESS installed, let's take a look at how LESS makes our lives simpler.

Variables

Variables are used in every single programming language ever made for a reason. They eliminate repeated values by storing them in a single place. To change a value throughout the code, you only have to change the value of that variable. In CSS, you will often see complex and length selectors just to set the color of the text.

#header, #navbar h3, #footer h3, .aside h3, #content h3 { color: red; }

This is incredibly difficult to follow, especially when you have multiple places where these long selectors occur. But this can be made much more readable using a simple variable in LESS.

@text-color: #ff0000;
#header { color: @text-color; }
#footer { color: @text-color; }
h3 { color: @text-color; }

Now, not only can you easily separate this into readable selectors, but you can now add more styles to each selector individually.

Operators

Anytime you have variables, you can bet you will also see operators or another way to relate variables to one another. Instead of adding more variables for simple variations on a single value, we can operate on that value.

@main-margin: 10px;
body { margin: @main-margin; margin-top: @main-margin + 10px; }

You can also subtract, multiply and divide values as well as colors.

Nested Rules

Nested rules can be useful for small styles, but it can easily get messy. Some people prefer to see all styles applying to a selector at once, while others would rather have everything separated out. It is all a matter of preference. Using nested rules also makes inheritance very clear and there is no scrolling up and down to find parent selectors.

#header
{
    h1
    {
        font-size: 26px;
        font-weight: bold;
    }
    p
    {
        font-size: 12px;
        a
        {
            text-decoration: none;
            &:hover { border-width: 1px }
        }
    }
}

This LESS code compiles to:

#header h1
{
    font-size: 26px;
    font-weight: bold;
}
#header p
{
    font-size: 12px;
}
#header p a
{
    text-decoration: none;
}
#header p a:hover
{
    border-width: 1px;
}

Mixins

Mixins are just a fancy way of reusing a process. They can be related closely to functions or methods in most programming languages. One of the most repeated snippets of code in any CSS3 webpage is the three lines required for rounded corners. Because not every box has corners of the same radius, these three lines are often duplicated into several classes with just the radius changed.

#header
{
    -border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
}
#content
{
    -border-radius: 15px;
    -webkit-border-radius: 15px;
    -moz-border-radius: 15px;
}

This example is extremely inefficient when it is duplicated just because the radius changed, but it can be improved with a little bit of LESS magic.

.rounded-corners (@radius)
{
    -border-radius: @radius;
    -webkit-border-radius: @radius;
    -moz-border-radius: @radius;
}
#header { .rounded-corners(5px); }
#content { .rounded-corners(15px); }

And this format can be applied to any set of values that refer to many different selectors.

Summary

Now you should be off and running with the new knowledge you have gathered. Hopefully you learned what preprocessors are and what they do. The Javascript version of LESS is by far the easiest version to implement. You learned how to implement variables, functions, operators, and nested rules in LESS. Take a bit of time to practice what I have shown you, then move on to the advanced topics of LESS.