views:

57

answers:

2

I am looking to assign variable names to css class dynamically. Here im explaining with small code

#A1, #A2, #A3, #A4{
   float: left;
   clear: left;
   width: 300px;
   margin: 15px;
}

instead of writing " #A1, #A2, #A3, #A4" , i want to write some variable. with for loop i want to define four classes while page is getting loaded.

could you pls explain me how to do that?

A: 

Why not use a css class selector?

You can then use the jQuery addClass method to assign to elements.

//apply class to all divs
$('div').addClass('someclass')

.someclass {
   float: left; 
   clear: left; 
   width: 300px; 
   margin: 15px; 
}
redsquare
Downvoter - please explain why?
redsquare
I didn't down vote, but would have, because you are using JavaScript (jQuery on top of that) to solve a non-JavaScript problem, that should be solved server-side.
RoToRa
A: 

You could always serve a PHP file as CSS.

http://net.tutsplus.com/tutorials/php/supercharge-your-css-with-php-under-the-hood/

Alternatively look at Less (either as rubygem or less.js) or SASS (also ruby)

http://lesscss.org/

http://sass-lang.com/

Kin