-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
LESS WEB DEVELOPMENT COOKBOOK
By :
For server-side compilation, Less comes with a command-line compiler for Node.js. The node package manager (npm) can be used to install the Less command-line compiler.
Node is a platform built on Chrome's JavaScript runtime called V8, allowing you to easily create fast and scalable network applications.
If you have not installed Node.js and npm on your system yet, you will have to do this first. You can do this by following these steps:
Node.js source code or a prebuilt installer for your platform from http://nodejs.org/download/.Node.js, which includes npm, on your system.In the Installing Node and Grunt recipe in Chapter 11, Compiling Less Real Time for Development Using Grunt, you can read about installing Node.js and npm on your system in more detail. After installing npm, you can simply run the following command:
npm install --global less
example.less. You can try the following code in your example file:@color: red;
.paint() {
color: @color;
}
p {
.paint();
}example.less or whatever filename you have chosen). If you have chosen example.less, you can run the following command in your command prompt:lessc example.less
lessc command, you will see it output the following CSS code in the console:p {
color: #ff0000;
}If you are new to Less, the example Less code used inside example.less may contain some syntax that is completely alien to you. The code defines a @color variable and a paint() mixin. The Declaring variables with Less for commonly used values recipe explains the basics of variables in Less, while the Setting the properties of CSS styles with mixins recipe does the same for mixins.
By default, the lessc compiler outputs to stdout. You can redirect the output to a CSS file with the following command:
lessc example.less > example.css
Running the lessc compiler without any parameters will give you a list of options for the compiler.
You can use the -x option to compress your output as follows:
lessc -x example.less > example.css
In a similar manner, you can use either the --clean-css option for a more involved minification, or the --source-map option to create a v3 CSS source map. In the Using CSS source maps to debug your code recipe in Chapter 2, Debugging and Documenting your Less Code, you can read more about CSS source maps and Less. Note that in version 2 of Less, the --clean-css option has been moved into a plugin. The usage is similar: just install the plugin (npm install -g less-plugin-clean-css), then make use of the --clean-css argument.
There are many other third-party compilers for Less with a compressive list available at http://lesscss.org/usage.
With grunt-contrib-less, you can compile your code with Grunt. For Gulp, you can use gulp-less. The Compiling style guides with Grunt recipe in Chapter 11, Compiling Less Real Time for Development Using Grunt, shows you how to build a development workflow with the Grunt task runner.
In this recipe, you read about Grunt and Gulp, which are JavaScript task runners or build systems. Comparing with Grunt's build system, Gulp's build system is relatively new. Gulp uses streams and code over configuration, which makes it more simple and intuitive.
Change the font size
Change margin width
Change background colour