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 8
rgranlundAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

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

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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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...
rgranlundAuthor Commented:
I did not see his post!  wow.  How do I give the points to him?
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
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.