Ecommerce Developer

An Introduction to the LESS Dynamic Stylesheet Language

LESS is a JavaScript library that extends cascading stylesheets (CSS), allowing the style language to support variables, nested rules, operations, functions, and mixins.

Alexis Sellier — who also goes by Cloudhead — developed LESS to help himself and other coders write, well, less CSS.

Installing LESS

LESS is very simple to use. Just include the JavaScript file in the head section of an HTML document.

  1. <script src="less-1.3.0.min.js"></script>
<script src="less-1.3.0.min.js"></script>

The stylesheet that contains the LESS descriptions, should proceed the library. Notice that the extension is “less” and the “rel” attribute value is “stylesheet/less.”

  1. <link rel="stylesheet/less" media="all" href="style.less"/>
  2. <script src="less-1.3.0.min.js"></script>
<link rel="stylesheet/less" media="all" href="style.less"/>
<script src="less-1.3.0.min.js"></script>

Using Variables with LESS

Variables, which are common in many languages, are not native to CSS, but LESS adds them. Declaring a LESS variable begins with the “@” symbol followed by the variable name, a colon, the value, and a semicolon.

Here are four examples that name font families, colors, and padding values.

  1. @advert: 'Advent Pro', sans-serif;
  2. @red: #9d0000;
  3. @black: #111;
  4. @pad: 5px 0 10px 50px;
@advert: 'Advent Pro', sans-serif;
@red: #9d0000;
@black: #111;
@pad: 5px 0 10px 50px;

These variables may be applied to any style definition. Notice the padding, background, and font-family declarations in the example.

  1. header {
  2.   width: 100%;
  3.   height: 35px;
  4.   margin: 0;
  5.   padding: @pad;
  6.   background: @black * 7;
  7.   color: white;
  8.   font-family: @advert;
  9.   font-weight: normal;
  10. }
header { 
	width: 100%;
	height: 35px;
	margin: 0;
	padding: @pad;
	background: @black * 7;
	color: white; 
	font-family: @advert;
	font-weight: normal;
}

LESS allows operations on variables too. Notice that the value of background declaration in the example above is actually the value of the @black variable times 7.

  1. background: @black * 7;
background: @black * 7;

Since the value of the variable @black is #111, when it is multiplied in LESS its value is changed to #777, which is a medium gray. To get a purer, if you will, black, 111 could have been subtracted from @black.

  1. background: @black - 111;
background: @black - 111;

Nested Rules

After variables, LESS’ nested rules are, perhaps, the most significant and useful tool the library provides, allowing rules to be applied to child elements in line with other style declarations.

As a minimal example, consider the case of adding a hover effect to a particular anchor tag.

  1. a {
  2.   color: white;
  3.   text-decoration: none;
  4.   &:hover { text-decoration: underline; }
  5. }
a { 
	color: white;
	text-decoration: none;
	&:hover { text-decoration: underline; }	
}

Notice how both the standard and hover states are described in the same declaration. Below is how this same style description might look in CSS without LESS.

  1. a {
  2.   color: white;
  3.   text-decoration: none;
  4. }
  5.  
  6. a:hover { text-decoration: underline; }
a {
	color: white;
	text-decoration: none;
}

a:hover { text-decoration: underline; }

When just a single nested declaration is used, the LESS feature may not seem like it is doing much, but as the nested structure becomes more complicated, the benefits start to become obvious. Consider this style declaration for a nav element. Notice that the anchor tags, unordered list, and list items are all nested.

  1. nav{
  2.   width: 100%;
  3.   margin: 10px 0 0 0;
  4.   padding: @pad;
  5.   background: @red;
  6.   font-family: @advert;
  7.   font-size: 20px;
  8.   a {
  9.     color: white;
  10.     text-decoration: none;
  11.     &:hover { text-decoration: underline; }
  12.   }
  13.   ul{
  14.     margin: 0;
  15.     padding: 0;
  16.     list-style: none;
  17.     li {
  18.       margin-right: 25px;
  19.       margin-top: 5px;
  20.       display: inline;
  21.     }
  22.   }
  23. }
nav{ 
	width: 100%;
	margin: 10px 0 0 0;
	padding: @pad;
	background: @red;
	font-family: @advert; 
	font-size: 20px;
	a { 
		color: white;
		text-decoration: none;
		&:hover { text-decoration: underline; }	
	}
	ul{
		margin: 0;
		padding: 0;
		list-style: none;	
		li { 
			margin-right: 25px; 
			margin-top: 5px;
			display: inline; 
		}
	}
}

Applying Mixins

Another popular LESS feature is the ability to include mixins. Simply put, a mixin applies the style declarations from one class to another.

  1. .science-fiction { margin: 50px 0 0 50px; }
  2. #books { .science-fiction; }
.science-fiction { margin: 50px 0 0 50px; }
#books { .science-fiction; }

In this example, both the science-fiction class and the books element would have the same margin value.

Mixins may also include variable values, similar to functions in other languages. In the example, below the science-fiction class would have a margin of 50px all around while the books element would have a margin of 25px all around.

  1. .science-fiction ( @margin: 50px ) { margin: @margin}
  2. #books { .science-fiction( 25px ); }
.science-fiction ( @margin: 50px ) { margin: @margin}
#books { .science-fiction( 25px ); }

Summing Up

LESS is a JavaScript library built to extend CSS and allow coders to write effective and more concise style declarations. The library adds many features to CSS, including the support for variables, nested rules, and mixins, as described here.

This is just scratching the surface of what can be done with LESS, but it is a good start.


Comments ( 10 )

Have Something To Say ?

  1. Paul October 15, 2012

    LESS sound like MORE…more to waste of time on new technologies that are not standard yet.

    How much are you really saving on CSS size of the website if the minimized version of LESS is 46KB

    Sounds like more pain than gain.

    • David Pavlicko October 16, 2012

      totally disagree. LESS and SASS are both extremely helpful tools! Besides, they both can be run server-side without much hassle at all, so there’s no impact to the client.

      I’d recommend using SASS with Compass to get the full benefit. It actually promotes cleaner, more understandable css through inheritance, and being able to use variables and math functions within your css is like frosting on a cake:

      e.g. $blue: #3bbfce;

      .content-navigation {
      border-color: $blue;
      color:
      darken($blue, 9%);
      }

    • Kevin October 17, 2012

      If you’re dealing with relatively small stylesheets than the extra footprint of using LESS may not be advantageous, however if you use a LESS compiler and only use the LESS library in local development, then you can easily create a minified CSS file for production which in and of itself can save a hell of a lot of time. The best example I can give of this is having to type the same hex color code over and over again instead of @bgcolor or the same font instead of @font1.

  2. Armando Roggio October 15, 2012

    Paul, I can understand your concerns. I have found LESS to be a huge time saver and code saver, especially when updating or maintaining a site.
    A.

  3. Annie October 16, 2012

    I’m pretty useless at coding of any sort, but this is too good not to have a go at.
    Thanks a Lot.

  4. Vitaly Izmailova October 20, 2012

    Thanks for the informative article, it was a good read and I hope its ok that I share this with some facebook friends. Thanks.

  5. RasmiKanta October 21, 2012

    Hi Guys would u tell me why not .less file not work on my website, whatever i write in less file it did not reflect on my front page…would any one help me

  6. kuldeep October 26, 2012

    Awesome tutorial.. Learnt in few minutes..

    Gotta use it right now in my next project…

  7. Brian Getting November 19, 2012

    LESS has been extremely helpful to me as well, and is the basis for Twitter Boostrap, which can be a huge timesaver.

    There are two ways to implement it. The way described above compiles the CSS “on the fly” using the browser, but you can also use LESS in development and precompile. Most people use a watcher so that whenever they make changes to any of the LESS files in a project, the CSS is automatically compiled.

  8. Toby November 21, 2012

    Including the LESS.js file in this way is not really production safe, you should use LESS more like a preprocessor offline during the development process.

    If JS fails for any reason and you are using LESS.js your stylesheets will not work and your site will not function.

    Also, using it offline during development means you can compile and minify your CSS all in one go, which a couple of commenters seem to have an issue with.

    Finally the link to lesscss.org is broke.