Take the 2-minute tour ×

is there a version of css3 code below that will work in all browers?

table tr:nth-child(odd) { background-color: #ccc; }
share|improve this question
I wouldn't bother too much about this. It works on all major browsers excluding IE (IE9 supports it though) and not having alternating colours isn't a big price to pay for using IE in the first place. –  casablanca Oct 23 '10 at 19:14

2 Answers 2

up vote 5 down vote accepted


You have at least two options, though:

  1. Manually apply a style to the odd (and/or) even rows and then style those appropriately: tr.odd {background-color: #ccc; }

  2. Use JS (I'd suggest jQuery): $('tr:nth-child(odd)').css('background-color','#ccc'); or $('tr:nth-child(odd)').addClass('odd') (either of these would need to be in a $(document).ready(); (or equivalent) though, obviously).

share|improve this answer
+1. I use jQuery to style mine. Something like $('table.striped > tbody > tr:odd > td').addClass('odd'); –  Nathan Ridley Oct 23 '10 at 19:07
actually let me correct that, my php script is generating separate tables So how would I have to do something in the PHP code to tell it every other table set a background color? –  acctman Oct 23 '10 at 19:29
You want a different background-color on alternating tables? –  David Thomas Oct 23 '10 at 19:31
yes, sir. After testing out the code above I notice it was colorizing all rows. After looking at my code I noticed that its output a new table for every items. So I would have to alternating the table background color. –  acctman Oct 23 '10 at 21:33
@acctman, I'd seriously suggest changing your php script to output one table containing all the rows; outputting a lot of one-row tables is (probably) semantically meaningless and structurally over-complicated. –  David Thomas Oct 23 '10 at 22:05

Not unless you modify the DOM (e.g. to add a class to every other row), either in the original markup or with JS after the content has loaded. (Even then it still won't support all browsers, as not all browsers support CSS, and not all that do have it turned on).

share|improve this answer

Your Answer


