CSS Background Color Every other div

I have a div that is displayed on a page X amount of times depending upon the amount of returns.  Is the a pure CSS way to give the div style an alternate background color every other iteration of the div?
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brandon LyonSenior Frontend DeveloperCommented:
Most browsers will support:

#wrapper div:nth-child(odd) {
  background: red;
}
#wrapper div:nth-child(even) {
  background: blue;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
AFAIK, with pure CSS you can't. You should use jquery to iterate throught the array of element changing its color at each iteration. Supposing your element are 3:

$(document).ready(function(){
  var array_of_colors('#ff0000', '#00ff00', '#0000ff');
  var i = 0;
  $('p').each(function(e){
    e.css('color', array_of_colors[i];
    i++;
  });
});

Open in new window

0
rgranlundAuthor Commented:
Thank you for that cool piece of jQuery.  However, I found out how to do it with CSS:
.application-menu-box:nth-child(even) {
	background-color:#f1f1f1;
}
.application-menu-box:nth-child(odd) {
	background-color:#fbfbfb;
}

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Marco GasiFreelancerCommented:
Oh, I misunderstood your needs: I thought you want to give a different color to each element, not to change the color for even and odd.
Thanks for points, but don't you think you should award them to Brandon? You chose his solution, after all...
0
rgranlundAuthor Commented:
I did not see his post!  wow.  How do I give the points to him?
0
Marco GasiFreelancerCommented:
Below the question box, where your question appears, you see a Request attention link: click it and explain the problem. An administrator will ressign the points or will reopen the question allowing you to re-award points.
Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.