CSS is a pretty dumb language on it's own. It doesn't offer any features that allow you to keep things DRY (Don't Repeat Yourself) so you end up writing the same things over and over again.
By the time you reach the end of a project you realize your css is 5x as large as it should be and you have 15 versions of the color blue....That's where LESS comes in, you write LESS code (that looks surprisingly like CSS) and then a processor converts that to CSS that you can include into your project.
Before we can begin writing LESS, we need a way of converting the LESS to CSS. Lets install the dependencies that we will need to do that.
Installation on windows is really straightforward, just download the windows .msi installer and you should be good to go. You don't want the .exe as the installer will give you other tools we'll need.
Talk to me if you want to install node on linux, there is a little different process.
Run: npm install -g less
To ensure it installed, run: lessc
The command line should output a lot of text on how to use less from the command line. Lets give it a try...
body {
background-color: #FF2200;
.header {
background-color: #FFFFFF;
}
}
Notice the funky css syntax we have going on there...
> lessc target.less destination.css
This invokes the less command and tells it to convert target.less into destination.css. Use whatever names are appropriate for your css files. The end result will be a new file called destination.css.
Compile as
you type
Should be the path to lessc on your computer, likely similar to this.
You can nest your LESS code so it becomes easier to see how things in your html actually relate to things in your markup. Lets take this markup for example.
<header>
<h1>This is the main heading</h1>
</header>
<header class='sub'>
<h1>This is the sub-heading</h1>
</div>
<header>
<h1>This is the main heading</h1>
</header>
<header class='sub'>
<h1>This is the sub-heading</h1>
</div>
CSS
header {
Color: red;
}
header h1 {
background-color: blue;
}
header.sub {
font-size: 16px;
}
header.sub h1 {
background-color: purple;
}
LESS
header {
color: red;
h1 {
background-color: blue;
}
&.sub {
font-size: 16px;
h1 {
background-color: purple;
}
}
}
.big-red-text {
color: red;
font-size: 5em;
}
h1 {
.big-red-text;
}
Create a style in one place, and use it over and over again.
Create reusable styles and import them into your current css file for use.
/** File name: mixins.less **/
.big-green-text {
font-size: 5em;
color: green;
}
/** File name: style.less **/
@import "mixins.less";
body {
.big-green-text;
}
Create reusable variables and use throughout your project.
@green: #2aff00;
@red: #ff0000;
h1 {
color: @red;
span {
color: @green;
}
}
Create reusable functions.
@green: #10ff00;
.border-with-radius(@borderWidth, @color, @radius) {
border: @borderWidth solid @color;
border-radius: @radius;
}
h1 {
.border-with-radius(10px, @green, 3px);
}
LESS Comes with lots of cool built in functions.
@green: #10ff00;
h1 {
color: lighten(@green, 20%);
}
h2 {
color: darken(@green, 20%);
}
It's actually pretty handy sometimes.
@numCols: 4;
.containers {
float: left;
width: 100% / @numCols;
}