tags:

views:

44

answers:

3

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

table tr:nth-child(odd) { background-color: #ccc; }
A: 

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).

David Dorward
+2  A: 

No.

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).

David Thomas
+1. I use jQuery to style mine. Something like `$('table.striped > tbody > tr:odd > td').addClass('odd');`
Nathan Ridley
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
You want a different background-color on alternating *tables*?
David Thomas
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
@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
+1  A: 

If you are using JSTL, a variation of this will work (not straight CSS, I know, but it works with Microsoft Internet Explorer 6.0):


<c:forEach items="${yourList}" var="item" status="status">
    <c:choose>
        <c:when test="${(status / 2) == 0}">
            <c:set var="rowClass" value="evenClass"/>
        </c:when>
        <c:otherwise>
            <c:set var="rowClass" value="oddClass"/>
        </c:otherwise>
    </c:choose>
    <tr class="${rowClass}">
        ...
    </tr>
</c:forEach>

dwb
I can't resist - but I have to put this link in: http://www.saveie6.com/
Duniyadnd
@Duniyadnd, I never knew there were so many crazy people in the world...and I'd sleep much better tonight, if that was still true.. @_@
David Thomas