Some years ago I started using SASS and LESS to
facilitate my CSS live.
I like the idea of variables, calculations of colors (15% brighter, please!)
of sizes (15px less than the default teaser div, please!), (parametric) mixins,
nested rules and a lot of other usefull stuff.
So back to the initial question: Why to learn a more complicated meta language
when CSS is such a simple thing?
That’s a simple one! It’s all about saving time, money and keep the code
quality high. Yes I’m talking about code quality for css files!
Have you ever tried to make a simple, crossbrowser opacity declaration?
So next time you want to set the opacity of an element you simply copy these
7 lines and make some minor modifications manually, right?
Really? I mean … really?
Wouldn’t it be nice to declare some sort of crossbrowser function with just one
argument for opacity? Something like:
2. Nested code
Quiz question: Do you see the repetitive code?
Just kidding ;) But wouldn’t it be nice to simply nest all these rules? Something
You are tired of looking for this already defined dark red, right? "Ahh,
there it is. No, that’s a bit brighter than the color I’m looking for. Ah, here
it is .. no. Maybe I should check this other file". Does this sound familiar?
Welcome to the club! It could be so much easier by defining a simple color variable!
Maybe something like:
4. Calculations / Operations
Want to use the same background color as the given text color, but – let’s say
40% brighter and the border around it – #222 darker? Here it comes:
I’m talking about many things, Alberto already mentioned in an earlier blog
post some weeks ago. What about some best practises,
some fresh ideas to convince me learning less or sass?
OK. So, let’s continue with more pretentious LESS / SASS examples.
5. Conditions (SASS only)
Ever wanted to switch the background color depending on the text color?
Tingly already? Let’s continue!
As in many programming languages you can work with scopes. This means, there can
exist a global "red" and a red, which is only used in an element with
the id "page".
Let"s look at the following example taken from less’
7. Loops (SASS only)
Ever thought about creating your own individual css grid layout. 14 Columns, 1450
pixels width with a 11px gap? Or what if you change to an 16px gap?
You can create your grid file on the fly. Change some variables and that’s it.
8. Config files
What about reading the style guide and just writing down the guidelines line
by line into a configuration file?
This config file would be included once in every new CSS file and that’s it.
There’s no need to seek for the border-radius, the standard margin from top,…
An really good example is the boilerplate
of twitter.com. Let’s look at their configuration file:
9. Partial Files beeing combined into one large css file
A good practice is to split your less / sass files into parts. There can be the
This is an easy to maintain structure even if you’re not the creator. "Reducing
the size of the gap between the photos. Hmmm, let’s open the photoalbum.less
file and change it!"
One single file including all needed parts is the trick here:
This will create one single file, containing all partials. You even can tell the
sass or less compiler to reduce the file size of this single file by removing all
unnecessary comments, whitespaces, and semicolons.
Hm. I’m not sure if this scares you or if this is the point you open a text editor
in the background to start writing your own sass or less files :)
evil eval things by simply
wrapping the expression with back-ticks. Did I mention you don’t have to compile
Combine these information and here it comes. The style from hell:
The paragraph inside the #infobox now uses the color entered by the user. Well well
- not a real live example…