Learning LESS CSS : Variables

  • Avinash
  • 1
  • Jun 11, 2012
  • Tutorial

Here I come with the new article in this series of LESS CSS. If you are not sure about previous article then I would recommend you to read this article about LESS CSS Introduction.

In this previous article we have looked into basics of LESS CSS and how to get started section. In this article we will start with the very basic thing of the LESS CSS which is Variables.

As I have mentioned earlier LESS CSS will make your CSS dynamic. Here dynamic term is used to refer it as a programmable. Just like any programming language LESS CSS have variables with declare once and use anywhere concept.

With LESS CSS you will have to do all your code in .less file instead of .css file. So Let’s see how you can declare and use Variables in LESS CSS.

Declare Variables in LESS CSS

// LESS CSS CODE - DECLARE VARIBALES @bgcolor: #00ccff; @linkcolor: #FC0000; @linkhovercolor: #666666;
// LESS CSS CODE - DECLARE VARIBALES
@bgcolor: #00ccff;
@linkcolor: #FC0000;
@linkhovercolor: #666666;

In above code we have seen that how we can declare variables in LESS CSS. Now let’s see how we can use these variables in LESS CSS code.

Use Variables in LESS CSS

// USE Variables - LESS CSS CODE body { background-color: @bgcolor; } a { color: @linkcolor; } a:hover { color: @linkhovercolor; }
// USE Variables - LESS CSS CODE
body
{
  background-color: @bgcolor;
}
a
{
  color: @linkcolor;
}
a:hover
{
  color: @linkhovercolor;
}

As mentioned above we can create variables and use them. But LESS will create a compiled CSS(traditional) using the less code. Let’s see the compiled code now.

/* Compiled CSS Code */ body { background-color: #00ccff; } a { color: #FC0000; } a:hover { color: #666666; }
/* Compiled CSS Code */

body
{
  background-color: #00ccff;
}
a
{
  color: #FC0000;
}
a:hover
{
  color: #666666;
}

Here I have given example of the color and background color, but in fact you can use these variables for any purpose you need just like font-size?

String Variables in LESS CSS

Upto now we have seen that we can use variables for the CSS property but we can store string also in these variables and use those variables for content property. Let’s see this in action.

// LESS String Variables @authorname:'Avinash'; @authordesc: "Expert Developer"; a.authorlink:before { content: @authorname; } a.authorlink:after { content: @authordesc; }
// LESS String Variables
@authorname:'Avinash';
@authordesc: "Expert Developer";

a.authorlink:before
{
  content: @authorname;
}

a.authorlink:after
{
  content: @authordesc;
}

Conclusion

So we are done with the Variables in LESS CSS. We have found that we can use use variables for any property of CSS. Even we can declare variables with string value and use as content property.

If you don’t want to miss any upcoming article in this series then subscribe to our RSS Feed via mail, Like us on Facebook or Follow us no Twitter. If you do so, I am sure you will never miss any article/freebies or updates from us.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: