less is, once again, more.
Web development is full of challenges. That’s my nice way of saying writing CSS blows. CSS is powerful, but at the cost of being too fine grained and low level for easy development. It’s like the assembly
of web design. Other developers are all-too-aware of the situation and have come up with a few solutions, including CSS frameworks, which reduce the amount of from-scratch code and provide a system (e.g., Blueprint
or the 960 grid system
), versus the freeform mess of raw CSS, and CSS extensions, like LESS
, which is the topic of the day.
Act One: L-E-S-S spells bliss
If you aren’t using something like this yet, you might as well be punching yourself in the crotch every time you code.
I have also messed with Sass, which was not as “Syntactically Awesome” as LESS, and xCSS, which was overkill (but I might revisit it later). LESS is good because:
- Any standard CSS file is a valid LESS file – easy to only use the features you need
- Just as powerful as Sass, offering variables, functions, nesting, CSS-specialized math operations
- Aptana CSS highlighting works great:
- Go: Window->Preferences
- General->Editors->File Associations
- Add file type: *.less
- Add editor: Aptana CSS Editor (right at the top)
I mean, look at this syntax:
/** Palette **/
/** Colors **/
border-bottom:1px solid @dark_blue - #111;
/***** Two column layout a la *********
****** http://matthewjamestaylor.com/blog/ultimate-2-column-left-menu-pixels.htm *****/
/* column container */
/* 2 column left menu settings */
left: @left_column_width + (2 * @column_margin_width);
right: @left_column_width + (3 * @column_margin_width);
margin:0 @column_margin_width 0 (@left_column_width + (4 * @column_margin_width));
width: @left_column_width ;
right: @left_column_width + @column_margin_width;
Now you have an easily customizable two column layout and color scheme. Change your values in one place and they gracefully propagate. If you wanted to change the colors or column width before, you would have to change dozens of values. Really, stop what you’re doing, change your CSS file’s extension to .less, and become a happier person.
So the only problem that I ran into is the bump in the workflow: compiling from LESS to CSS. This is where incron comes into play. Incron monitors files for changes and can trigger actions as specified by you with a cron-like syntax we can all love. I have a little Gentoo development box I use; I put incron on it and set it to run
lessc whenever my LESS file changed. Just install/emerge incron, run
incrontab -e and add one line:
/path/to/less/files/mystyles.less IN_MODIFY lessc $@
Now you’ll have a file called my_styles.css and everything will be hunky dory. Of course this could easily be extended to do more powerful things with your styles, like move them into the right place or give them fancy names or version them or whatever. The potential here is also not limited to LESS, so consider it an investment. If this intrigues you, I think this tutorial should be all you need for now.