10 April 2007

Dynamic variables in CSS via JavaScript

There seems to be a need for dynamic variables in css . All the solutions require some server side code … is JavaScript an option ?

I personally don't have a need for dynamic variables but I can see how it could be useful. I just never understand why JavaScript is not an option

why javascript …

examples …

Enough talk, I did two quick examples ... these are files are identical except they set different variables for colors and size:
white background »
orange background »

how …

I am not going to give a tutorial on JavaScript ... but just want to show the basic concept...

//variable in css
background-color: DVCJ_BGCOLOR;

//javascript setting
DVCJ_BGCOLOR	= '#fff';

dvjc_css = dvjc_css.replace(/DVCJ_BGCOLOR/g, DVCJ_BGCOLOR);

//write css
document.write('<style>'+ dvjc_css +'</style>');

the source …



i want to use a variable of jquery
in css how can i do it please give me


bhagwat sharma

You’re a genius!!!!!!!!! this is by far the best method, thanks for sharing.

Mr WordPress


Site has been moved

nuff-respec.com is no longer being updated. Please head over to dbulli.com for new content