Home  »  Blog  »  www  »  Do LESS with Less.js (CSS pre-processor)  

Do LESS with Less.js (CSS pre-processor)

Some of us may have heard of such services as LESS (Leaner CSS) and Sass (Syntactically Awesome Stylesheets), but not many would have had a chance to use them. LESS was originally built with Ruby and allowed us to use variables, operators, mix-inx and nested selectors to construct our CSS.

LESS has recently been completely rewritten in JavaScript and will, eventually, become LESS 2.0. It will be able to run directly in the browser, as well as on the server, with node.js (which I am not going to cover here).

LESS.js is not yet a complete product, but does work great and is something we can already utilise for building our CSS.

Setup

Step 1. Import a Stylesheet
1:<link rel="stylesheet/less" href="styles.less" />

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

Step 2. Reference LESS.js
1:<script src="less.min.js"></script>

Current version of LESS.js is at http://lesscss.googlecode.com/files/less-1.0.18.min.js, so you can use that in scr= above.

Usage

Now that we have LESS.js enabled, we have access to all the cool features such as variables, operators and nested selectors.

Here is a quick example of what we can do with LESS.js.
 1:/* Variables! */  
 2:@primary_color: green;  
 3:  
 4:/* Mix-ins are like functions for commonly used 
 5:operations, such as apply borders. We create 
 6:variables by prepending 
 7:the @ symbol.*/  
 8:.rounded(@radius: 5px) {
 9:	-moz-border-radius: @radius;
10:	-webkit-border-radius: @radius;
11:	border-radius: @radius;  
12:}  
13:  
14:#container {  
15:/* References the variable we created above. */  
16:    background: @primary_color;  
17:  
18:/* Calls the .rounded mix-in (function) that we created, 
19:and overrides the default value. */  
20:    .rounded(20px);   
21:  
22:/* Nested selectors inherit their parent's selector 
23:as well. This allows for shorter code. */  
24:	a {
25:		color: red;  
26:    }  
27:}
This example demonstrates how we can centre content:
1:.centered(@width: 500px, @height: 500px) {
2:	position: absolute;
3:	width: @width;
4:	height: @height;
5:	top: 50%;
6:	left: 50%;
7:	margin-left: @width/2 * -1;
8:	margin-top: @height/2 * -1;
9:}

Obviously, none of the above will work if JavaScript is disabled, since it is parsed with LESS.js. But if you implement and preview the examples above, you will see that the result is pure CSS, which you can then copy and paste into a CSS file. So it would be best to use LESS.js during a cutup/development stage.

What if I don't want to use JavaScript

LESS.app (Mac)

If you are concerned about having to use JavaScript within your HTML you can use LESS.app to compile your LESS files into CSS files automatically, as you save them.

less.tmbundle (TextMate for Mac)

Alternatively, instead of using LESS.app, which will need to be running in order to compile and save .less files as .css from TextMate, we can install the LESS TextMate Bundle, which will also add CSS syntax highlighting to .less files and parse them to .css files on save (command + S).

To install the bundle follow these guidelines:

1. Open Terminal.app and run the following:
1:sudo gem install less

2. Download the Bundle from here (Download Source button in the top right corner). UnZip or UnTar the downloaded file.

3. Open the "appden-less.tmbundle" folder where you will find two folders "Commands" and "Syntaxes". Each folder contains a file which installs a bundle. Run both. TextMate will be started if it wasn't already running. Once both bundles are installed, in TextMate click on Bundles -> Bundle Editor -> Reload Bundles. DONE!

From now on, if you create a .less file and edit it in TextMate, saving it will compile and generate the .css file for you automatically.

 

 
 

Comments:

By Tom on 27 Sep 2011 9:59pm

Anton,

im sorry I didnt mention that in the first post.

Actually Im on Snow Leopard.

Thanks,

Tom
By Anton P. on 27 Sep 2011 1:48pm

Tom,

Are you also on Tiger?

Thanks,

Anton.
By Tom Piestansky on 27 Sep 2011 12:23pm

Hi, thanks for the info - I have followed your guide however Im having some issues with saving .less file and being converted to .css file while saving.

The screenshot below displays the problem that occurs once I hit cmd+s

https://skitch.com/tehkemo/fh8wb/untitled-6

If you have any idea, please respond to my email address as well.

Thank you!

Tom
By rogaroga on 10 Sep 2011 5:25pm

hello anton
thanks for your answer. i tried this :
gem update --system

it gave back the following error :

Updating RubyGems
Updating rubygems-update
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /Library/Ruby/Gems/1.8 directory.

the other one gives back error too. it's a little bit confusing for me because I am new to the terminal.
By Anton P. on 09 Sep 2011 11:47am

Can you try the following:

1) gem update --system

if that doesn't help

1) gem uninstall rails

2) gem install rails

Let me know if that helps.
By rogaroga on 09 Sep 2011 11:42am

hello anton
did you had time to have a look ?
By Anton P. on 06 Sep 2011 3:59pm

Hi rogaroga,

I'll have a look around and will get back to you sometime tomorrow. Obviously when I wrote this tutorial, 10.7 hadn't even been announced.

Thanks,

Anton.
By rogaroga on 06 Sep 2011 3:28pm

and here is the end of the error message :

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/libv8-3.3.10.2 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/libv8-3.3.10.2/ext/libv8/gem_make.out
By rogaroga on 06 Sep 2011 3:26pm

thanks for your answer. I am using 10.7.1
By Anton P. on 06 Sep 2011 3:19pm

Hi rogaroga,

I would love to help and might be able to in the next couple of days, but I don't have a mac right now.

What version of OS are you running?

Thanks,

Anton.

Previous | 1 2 | Next

Security key
Enter characters from the image on the left