tags:

views:

47

answers:

3

I've seen a lot of ways of adding variables in CSS (e.g. SASS and LESS).

What are the pro and cons of changing from this:

#div-one, #div-two {
    color: blue;
}

to this:

@default: blue;

#div-one {
   color: @default;
}

#div-two {
   color: @default;
}
+1  A: 

Using variables in CSS like this definitely makes the code more readable and maintainable. Also, it is a nice example of the DRY principle. Although in this case combining the two into one might be better (assuming @default is used more than once).

@default: blue;
#div-one, #div-two {
    color: @default;
}
Martin Wickman
I mean #div-one and #div-two would share the color (blue), but may not share other properties like width, padding and margin.
janoChen
+1  A: 

AFAIK, CSS variables are not part of the standard. It's a feature request
I've read somewhere that the latest versions of WebKit may understand that.

But in short, don't use this. Even I it's pretty cool, as you can define global values, most of the currently-used browser won't understand this.

I don't know about SASS or LESS, but I can see this is Ruby stuff, that need to be installed.
So I don't think it's a compatible solution either.

Macmade
LESS can be used just by including a js file: http://lesscss.googlecode.com/files/less-1.0.21.min.js
janoChen
@janoChen: but that would require that your users have Javascript enabled just to see your correctly styled web page. *Won't somebody think of the NoScript users out there!?*
Andy E
@Andy E's head are there a lot of people disabling their Javascript? Why do they do that?
janoChen
@janoChen: There are plenty of good reasons for it. The biggest reason is security, most browser exploits are found and implemented via javascript. Another reason is to speed up web pages that are slow due to inefficient Javascript. The benefit of NoScript is being able to enable scripting at the click of an icon. FYI, I don't have Javascript disabled (I use Chrome anyway), but plenty of web users do.
Andy E
A: 

Generally pretty useful, for example, they are used with SASS: http://sass-lang.com/

Evan Trimboli