LESS is a CSS pre-processor that lets you create stylesheets in a much more efficient and intelligent manner than using simple flat CSS. It provides a number of dynamic components that will make your code smaller, more reusable and more scalable. Its syntax is fairly easy to understand and rather adds on top of regular CSS than replaces it.
There are 2 main ways you can use LESS: server-side or client-side. The first one needs Node.js to compile to css, whereas for the second case you need to download a javascript file and include it on your page. This second way is not really recommended for production sites, but has a development oriented watch-mode that you should check out if you are interested.
In this tutorial, we will see how you can install LESS and get started using it on the server-side. For this, it assumes you are already running your own VPS with Ubuntu and a web server installed (if you want to see something in the browser). Additionally, you will have to install Node.js and NPM (Node Package Manager). If you don't have them already, follow the steps outlined in this tutorial to get set up.
So assuming you have Node.js and NPM all set up, we can run the following command to install LESS:
npm install -g less
Once installed on your VPS, you can use the command line to instruct LESS to compile less files to .css. To try it out, go to your web server's document root (defaults to /var/www in Apache) and create a .less file:
nano /var/www/test.less
Inside this file paste the following css declaration (note that LESS language is basically css with some great additions on top):
#box { color:red; }
To have LESS compile this file to css and output the result in the terminal, run the following command:
lessc /var/www/test.less
You should see the results printed in your terminal window. To have this output written to a .css file (more of a common scenario), specify also a file destination:
lessc /var/www/test.less > /var/www/test.css
lessc /var/www/test.less > /var/www/test.css -x
This will create production ready minified css. For more information about the options you can pass to LESS commands, visit this page or run the lessc command without any parameters.
So why is LESS so great and should you give it a try instead of simple css? Below we will look at a couple of reasons why.
Variables:
With LESS, you can use variables in your css. For example:
@white: #fff; #white-box { color: @white; }
#white-box { color: #fff; }
.shape { size: 100%; padding: 20px; } .box { .shape; background-color: red; }
.box { size: 100%; padding: 20px; background-color: red; }
.box { color: red; .box-inner { max-width: 80% } }
.box { color: red; } .box .box-inner { max-width: 80% }
@length: 10px + 20; .box { width: @length / 2; }
.box { width: 15px; }
.box { color: saturate(#398bce, 5%); }
@import "base";
In this article, we’ve seen a bit the power of using LESS for better management and usage of your stylesheets. We’ve seen how to install and use it on the server-side and a glimpse of the language improvements it brings to regular css. You are encouraged to read more from the official LESS website and learn even more about all the things you can do with it.
Additionally, if you are interested, there are available GUI applications that help you work with LESS right on your computer. This includes also compiling the .less files to css. You can find cross platform compilers like Crunch or Mixture that work both on Windows and Mac but also ones for specific platforms. You can check them out here.
Alternatively, I also encourage you to check out Sass, another popular CSS preprocessor. There is a tutorial on DigitalOcean that can get you started with that.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.