Advertisement
  1. Code
  2. JavaScript

Quick Tip: You Need to Check out LESS.js

Scroll to top
Read Time: 1 min

You might be familiar with such services as LESS and Sass. They allow for far more flexibility when creating your stylesheets, including the use of variables, operators, mix-ins, even nested selectors. However, because LESS was originally built with Ruby, a lot of PHP developers, despite the fact that there are PHP versions available, never used it.


Full Screencast



Step 1. Reference LESS.js

1
 
2
<script src="http://lesscss.googlecode.com/files/less-1.0.18.min.js"></script>

Step 2. Import a Stylesheet

1
 
2
<link rel="stylesheet/less" href="style.less" />

Note that we've set the rel attribute to "stylesheet/less" and that our actual stylesheets has an extension of .less, not .css. Also, we must link to this stylesheet before Less.js.


Step 3. Have Fun!

With this minimal amount of work, you now have access to everything from variables to mix-ins. Be sure to watch the four minute video tutorial above for full examples, but here are a few quickies.

1
  
2
/* 
3
Variables! 
4
*/ 
5
@primary_color: green; 
6
  
7
/* 
8
Mix-ins are like functions for commonly used operations, 
9
such as apply borders. We create variables by prepending 
10
the @ symbol. 
11
*/ 
12
.rounded(@radius: 5px) { 
13
	-moz-border-radius: @radius; 
14
	-webkit-border-radius: @radius; 
15
	border-radius: @radius; 
16
} 
17
  
18
#container { 
19
/* References the variable we created above. */ 
20
	background: @primary_color; 
21
  
22
/* Calls the .rounded mix-in (function) that we created, and overrides the default value. */ 
23
	.rounded(20px);  
24
  
25
/* Nested selectors inherit their parent's selector as well. This allows for shorter code. */ 
26
	a { 
27
		color: red; 
28
	} 
29
}

It's important to remember that LESS.js isn't finished; hopefully, it will be soon. Nonetheless, it's working wonderfully so far. What do you think?

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.