Results 1 to 7 of 7
-
27th Jan 2012, 01:49 PM #1OPMember
LESS CSS Preprocessor - Write Less, Do More
As we know, CSS is a stylesheet language that was introduced to give users control over the visual (presentational) aspects of websites. Although it is still under development and keeps getting new features with every iteration, it can get a bit cumbersome when you are working on big websites, especially because many properties of CSS3 require to be defined separately for each browser with its respective prefix at this time. Fortunately for us, there's something that extends on CSS and makes it much more than a stylesheet language.
LESS is a CSS preprocessor made by Alexis Sellier, also known as cloudhead. It extends on CSS and adds features that are only found in programming languages. With LESS, you can make use of variables, functions, mixins, nesting rules, operations, color functions, etc. and reuse properties of classes and id's. And the most amazing thing about LESS is that it is backwards compatible, i.e., in addition to using LESS, you can fallback to normal CSS whenever you feel like it.
Using LESS is simple, you just need to create your .less file and link it to your website using rel="stylesheet/less" as attribute.
Client Side Usage
Link your .less stylesheets with the rel set to “stylesheet/less”:
Code:<link rel="stylesheet/less" type="text/css" href="styles.less">
Code:<script src="less.js" type="text/javascript"></script>
The Language
Variables
These are pretty self-explanatory:
Code:@nice-blue: #5B83AD; @light-blue: @nice-blue + #111; #header { color: @light-blue; }
Code:#header { color: #6c94be; }
In LESS, it is possible to include a bunch of properties from one ruleset into another ruleset. So say we have the following class:
Code:.bordered { border-top: dotted 1px black; border-bottom: solid 2px black; }
Code:#menu a { color: #111; .bordered; } .post a { color: red; .bordered; }
Code:#menu a { color: #111; border-top: dotted 1px black; border-bottom: solid 2px black; } .post a { color: red; border-top: dotted 1px black; border-bottom: solid 2px black; }
LESS has a special type of ruleset which can be mixed in like classes, but accepts parameters. Here’s the canonical example:
Code:.border-radius (@radius) { border-radius: @radius; -moz-border-radius: @radius; -webkit-border-radius: @radius; }
Code:#header { .border-radius(4px); } .button { .border-radius(6px); }
Code:.border-radius (@radius: 5px) { border-radius: @radius; -moz-border-radius: @radius; -webkit-border-radius: @radius; }
Code:#header { .border-radius; }
You can also use parametric mixins which don’t take parameters. This is useful if you want to hide the ruleset from the CSS output, but want to include its properties in other rulesets:
Code:.wrap () { text-wrap: wrap; white-space: pre-wrap; white-space: -moz-pre-wrap; word-wrap: break-word; } pre { .wrap }
Code:pre { text-wrap: wrap; white-space: pre-wrap; white-space: -moz-pre-wrap; word-wrap: break-word; }
@arguments has a special meaning inside mixins, it contains all the arguments passed, when the mixin was called. This is useful if you don’t want to deal with individual parameters:
Code:.box-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) { box-shadow: @arguments; -moz-box-shadow: @arguments; -webkit-box-shadow: @arguments; } .box-shadow(2px, 5px);
Code:box-shadow: 2px 5px 1px #000; -moz-box-shadow: 2px 5px 1px #000; -webkit-box-shadow: 2px 5px 1px #000;
Nested rules
LESS gives you the ability to use nesting instead of, or in combination with cascading. Lets say we have the following CSS:
Code:#header { color: black; } #header .navigation { font-size: 12px; } #header .logo { width: 300px; } #header .logo:hover { text-decoration: none; }
Code:#header { color: black; .navigation { font-size: 12px; } .logo { width: 300px; &:hover { text-decoration: none } } }
Code:#header { color: black; .navigation { font-size: 12px } .logo { width: 300px; &:hover { text-decoration: none } } }
Notice the & combinator—it’s used when you want a nested selector to be concatinated to its parent selector, instead of acting as a descendent. This is especially important for pseudo-classes like :hover and :focus.
For example:
Code:.bordered { &.float { float: left; } .top { margin: 5px; } }
Code:.bordered.float { float: left; } .bordered .top { margin: 5px; }
Any number, color or variable can be operated on. Here are a couple of examples:
Code:@base: 5%; @filler: @base * 2; @other: @base + @filler; color: #888 / 4; background-color: @base-color + #111; height: 100% / 2 + @filler;
Code:@var: 1px + 5;
Brackets are also authorized in operations:
Code:width: (@var + 5) * 2;
Code:border: (@width * 2) solid black;
LESS provides a variety of functions which transform colors. Colors are first converted to the HSL color-space, and then manipulated at the channel level:
Code:lighten(@color, 10%); // return a color which is 10% *lighter* than @color darken(@color, 10%); // return a color which is 10% *darker* than @color saturate(@color, 10%); // return a color 10% *more* saturated than @color desaturate(@color, 10%); // return a color 10% *less* saturated than @color fadein(@color, 10%); // return a color 10% *less* transparent than @color fadeout(@color, 10%); // return a color 10% *more* transparent than @color fade(@color, 50%); // return @color with 50% transparency spin(@color, 10); // return a color with a 10 degree larger in hue than @color spin(@color, -10); // return a color with a 10 degree smaller hue than @color mix(@color1, @color2); // return a mix of @color1 and @color2
Code:@base: #f04615; .class { color: saturate(@base, 5%); background-color: lighten(spin(@base, 8), 25%); }
Code:hue(@color); // returns the `hue` channel of @color saturation(@color); // returns the `saturation` channel of @color lightness(@color); // returns the 'lightness' channel of @color alpha(@color); // returns the 'alpha' channel of @color
Code:@new: hsl(hue(@old), 45%, 90%);
Math functions
LESS provides a couple of handy math functions you can use on number values:
Code:round(1.67); // returns `2` ceil(2.4); // returns `3` floor(2.6); // returns `2`
Code:percentage(0.5); // returns `50%`
Still not convinced enough to make the transition from classic CSS?
If you like the features of LESS but don't want to use LESS directly then there's a solution for that. SimpLESS is a cross-platform LESS to CSS compiler. You don't have to do any extra work to compile your LESS files to CSS. Just add your LESS stylesheets or project folders to SimpLESS by dragging and dropping and it would automatically create an updated CSS stylesheet every time you makes changes to your LESS file.
Get SimpLESS from here:
http://wearekiss.com/simpless
Other options - online LESS compilers:
http://http://lesstester.com
http://winless.org/online-less-compiler
Most of the information on this thread has been sourced from LESS' official website. For more info visit [Official Website] : http://lesscss.orgAmz Reviewed by Amz on . LESS CSS Preprocessor - Write Less, Do More As we know, CSS is a stylesheet language that was introduced to give users control over the visual (presentational) aspects of websites. Although it is still under development and keeps getting new features with every iteration, it can get a bit cumbersome when you are working on big websites, especially because many properties of CSS3 require to be defined separately for each browser with its respective prefix at this time. Fortunately for us, there's something that extends on CSS and makes it Rating: 5A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing. ? George Bernard Shaw
?
Designer. Check my designs here.
-
27th Jan 2012, 01:54 PM #2Member
thanks Mate
-
27th Jan 2012, 01:54 PM #3Banned
Thanks, try it later on my site.
-
27th Jan 2012, 01:55 PM #4Member
Thanks for this m8!
-
27th Jan 2012, 01:59 PM #5
-
27th Jan 2012, 02:04 PM #6MemberWebsite's:
iFunMaza.comMath functions this part was sweet!
TFS
-
27th Jan 2012, 02:56 PM #7OPMember
Thanks for the comments.
I just added links to two online LESS compilers to the thread so you can start playing with LESS right away!
CheersA life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing. ? George Bernard Shaw
?
Designer. Check my designs here.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Write For Us
By Kw3rLn in forum Webmaster DiscussionReplies: 0Last Post: 12th May 2012, 04:55 PM -
anyone can write this c++ code.
By iamuser_2007 in forum Web Development AreaReplies: 3Last Post: 21st May 2011, 03:14 PM -
Re-write some URLs
By Chris2k in forum Web Application/Script SupportReplies: 4Last Post: 27th Mar 2011, 12:59 AM -
how do i write?
By lig}{tning in forum vBulletinReplies: 10Last Post: 18th Aug 2009, 08:45 AM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...