Monday, 21 January 2013

Install and use LESSCSS on your ubuntu server

LESSCSS is an excellent progression in the development of stylesheets, but how do you install and use it on your ubuntu server?
The first thing we do is to install node-less. To do this through the terminal, use the command:
sudo apt-get install node-less
Right, that's the installation done. Yes, that easy.
Now let's create our LESSCSS stylesheet. Here is my simple example, which we'll put into a file named styles.less:

@blue:#00c;
@white:#FFFFFF;
body
{
background:@blue;
color:@white;
}

There are two ways of using LESSCSS.
The first way is to download, the less.js library from http://lesscss.org/, then simply refer to a LESSCSS stylesheet we created and subsequently call the library thus:.

<!DOCTYPE html>
<html lang="en">
<head>
<title>less test</title>
<link rel="stylesheet/less" type="text/css" href="styles.less">
<script src="less.js" type="text/javascript"></script>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
The second way is to compile our LESSCSS stylesheet into a normal CSS. To do this, again go to the terminal, change to the directory containing your page e.g.
cd /var/www/test/less
Then compile the LESSCSS stylesheet into a normal CSS like this:
lessc styles.less > mystyle.css
Now you can just call your newly created CSS in your page, but this time without the need for the less.js library thus:

<!DOCTYPE html>
<html lang="en">
<head>
<title>less test</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<p>Hello World!</p>
</body>
</html>

1 comment:

  1. Thanks - very useful, do you have any idea how to upgrade to less latest, the apt-get gets me 1.3 but I need to upgrade to 1.3.3?

    ReplyDelete