Code Snippets Gallery
jQuery Zebra Stripe a Table
$("tr:odd").addClass("odd");Then use some CSS:
.odd {
background: #ccc;
}A Web Design Community curated by Chris Coyier
$("tr:odd").addClass("odd");Then use some CSS:
.odd {
background: #ccc;
}
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).
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.
You are awesome Chris, thanks for posting all this stuff!
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.
i’m newbie. i try the code and not working. my i have full example for the using of the code ? thanks
@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/
Why do you wanna use jQuery?? CSS is capable of this.