Is it possible to use variables in CSS? I have an html layout. I want to use one main css for structure and just decide on the colors based on a variable if possible. Can it be done?
Variables in CSS?
Collapse
X
-
Tags: None
-
I'm not sure what do you exactly need, but if I'm getting you right, of course you can, and it's quite easy, check http://interfacelab.com/variables-in-css-via-php/
in action (from http://sperling.com/examples/pcss/ )
in action (from http://sperling.com/examples/pcss/)
Code:<?php header("Content-type: text/css"); $color = "green"; // <--- define the variable echo <<<CSS /* --- start of css --- */ h2 { color: $color; /* <--- use the variable */ font-weight: bold; font-size: 1.2em; text-align: left; } /* --- end of css --- */ CSS; ?>
I already made a few projects using this method and works like a charm, in conjunction with XSL/XML environments is a real help
then again, not sure if this is what you're looking for
This post is endorsed by CIA, KGB, MI6, the Mafia, Illuminati, Kim Jong Il, Worldwide Ninjas Association, Klingon Empire and lolcats. Don't mess around with it, just accept it and embrace the truthComment
-
aaah, now I see you want html and not php... well, there are ways, but don't remember exactly how since I didn't pay much attention because it was too much work for the resultThis post is endorsed by CIA, KGB, MI6, the Mafia, Illuminati, Kim Jong Il, Worldwide Ninjas Association, Klingon Empire and lolcats. Don't mess around with it, just accept it and embrace the truthComment
-
Strangely enough, you can do it in IE but not any other browser. Go figure, the one browser that does CSS the worst....
In IE... you can do this:
<style>
#column1 {
width: expression(document.getElementsByTagName('body')[0]) / 2);
}
</style>
In IE, you can use javascript within the expression brackets. But every other browser will ignore it.
You canComment
-
Wow, gfy makes posting slow...
Anyway, I was going to finish with:
You can do it that way to avoid using php if you don't know it or have it readily available but php will give you way more control.Comment
-
Comment
-
If you use a dynamic language like PHP to generate it. It's more difficult if the file ends in .css because you would have to configure apache to treat it as PHP (which also slows the request down), but if you do it as an embedded CSS in a PHP file this is definitely possible.
There was some talk of CSS variables recently but until it becomes standard in all browsers I don't see a point in looking into it.Comment
-
Some good suggestions, and I can definitely work with these. Thanks.- Free Premium Domain Lists and Tools at Clickmojo.com
- For Sale: Obscenity.com
Comment


Comment