Girish Mahajan (Editor)

Less (stylesheet language)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Designed by
  
Alexis Sellier

Typing discipline
  
dynamic

First appeared
  
2009

Implementation language
  
JavaScript

Less (stylesheet language)

Developer
  
Alexis Sellier, Dmitry Fadeyev

Stable release
  
2.7.1 / May 9, 2016; 9 months ago (2016-05-09)

Less (sometimes stylized as LESS) is a dynamic style sheet language that can be compiled into Cascading Style Sheets (CSS) and run on the client side or server side. Designed by Alexis Sellier, Less is influenced by Sass and has influenced the newer "SCSS" syntax of Sass, which adapted its CSS-like block formatting syntax. Less is open source. Its first version was written in Ruby; however, in the later versions, use of Ruby has been deprecated and replaced by JavaScript. The indented syntax of Less is a nested metalanguage, as valid CSS is valid Less code with the same semantics. Less provides the following mechanisms: variables, nesting, mixins, operators and functions; the main difference between Less and other CSS precompilers being that Less allows real-time compilation via less.js by the browser.

Contents

Variables

Less allows variables to be defined. Variables in Less are defined with an at sign (@). Variable assignment is done with a colon (:).

During translation, the values of the variables are inserted into the output CSS document.

The code above in Less would compile to the following CSS code.

Mixins

Mixins allow embedding all the properties of a class into another class by including the class name as one of its properties, thus behaving as a sort of constant or variable. They can also behave like functions, and take arguments. CSS does not support Mixins. Any repeated code must be repeated in each location. Mixins allow for more efficient and clean code repetitions, as well as easier alteration of code.

The above code in Less would compile to the following CSS code:

Less has a special type of ruleset called parametric mixins which can be mixed in like classes, but accepts parameters.

The above code in Less would compile to the following CSS code:

Functions and operations

Less allows operations and functions. Operations allow addition, subtraction, division and multiplication of property values and colors, which can be used to create complex relationships between properties. Functions map one-to-one with JavaScript code, allowing manipulation of values.

The above code in Less would compile to the following CSS code:

Sass

Both Sass and Less are CSS preprocessors, which allow writing clean CSS in a programming construct instead of static rules.

As of Less 1.4, Less supports nested, inherited rules via the &:extends and @extends pseudo-selector. Prior to this, a main difference between Less and other preprocessors like Sass was the lack of an @extends directive to support inheritance of rules across classes, leading to cleaner CSS with less duplication.

Less is inspired by Sass. Sass was designed to both simplify and extend CSS, so things like curly braces were removed from the syntax. Less was designed to be as close to CSS as possible, and as a result existing CSS can be used as valid Less code.

The newer versions of Sass also introduced a CSS-like syntax called SCSS (Sassy CSS).

For more syntax comparisons, see https://gist.github.com/674726.

Use on sites

Less can be applied to sites in a number of ways. One option is to include the less.js JavaScript file to convert the code on-the-fly. The browser then renders the output CSS. Another option is to render the Less code into pure CSS and upload the CSS to a site. With this option no .less files are uploaded and the site does not need the less.js JavaScript converter.

References

Less (stylesheet language) Wikipedia