Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alternate style (background color ) for div inside div

How to alternate style (background color with jquery) for div inside div with id="container" alternately ( even and odd ) if I have HTML like this

<div id="container">
   <div></div> 
   <div></div>
   <div></div>
   <div></div>
...
</div>

I know with table like

#tbl_users tr:nth-child(even) {background: #CCC;}
#tbl_users tr:nth-child(odd) {background: #FFF;}

but how to div apply something like this ?

like image 855
Danka Avatar asked Dec 16 '22 12:12

Danka


1 Answers

Did you try:

div#container div:nth-child(even) {background: #CCC;}
div#container div:nth-child(odd) {background: #FFF;}

nth-child should work regardless of the tag.

like image 69
Andrej Avatar answered Dec 19 '22 07:12

Andrej