A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > jQuery > jQuery Zebra Stripe a Table Submit one!

jQuery Zebra Stripe a Table

$("tr:odd").addClass("odd");

Then use some CSS:

.odd {
   background: #ccc;
}

8 Responses

  1. Dor says:

    Very nice! I didn’t know about :odd. You learn something new every day :D

    (However, I would use :even (assuming it exists), becuase I would like the first row as the “normal” color).

  2. Dyllon says:

    Well, you can just do
    tr {background-color: #FFF;}
    and i think the .odd will override it, and if not you can always have the background color for the :odd to !important.

  3. nate says:

    You are awesome Chris, thanks for posting all this stuff!

  4. fludotlove says:

    It’s better to use:

    $('tr:nth-child(odd)').addClass('odd');

    Since your example will count all table rows without consideration of what table they are in. The above code will always start with an even row, even if it’s essentially the 7th table row in your document.

  5. dani says:

    i’m newbie. i try the code and not working. my i have full example for the using of the code ? thanks

  6. mike ilz says:

    @dani, here is a simple demo explaining exactly how to use this code, and a live example of it being used.

    http://webdesignandsuch.com/2010/08/add-zebra-striping-to-a-table-with-jquery/

  7. fingerlicking says:

    Why do you wanna use jQuery?? CSS is capable of this.

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.