506

I am using a table with alternate row color with this.

tr.d0 td {
  background-color: #CC9999;
  color: black;
}
tr.d1 td {
  background-color: #9999CC;
  color: black;
}
<table>
  <tr class="d0">
    <td>One</td>
    <td>one</td>
  </tr>
  <tr class="d1">
    <td>Two</td>
    <td>two</td>
  </tr>
</table>

Here I am using class for tr, but I want to use only for table. When I use class for table than this apply on tr alternative.

Can I write my HTML like this using CSS?

<table class="alternate_color">
    <tr><td>One</td><td>one</td></tr>
    <tr><td>Two</td><td>two</td></tr>
    </table>

How can I make the rows have "zebra stripes" using CSS?

Laurel
  • 5,771
  • 12
  • 29
  • 54
Kali Charan Rajput
  • 11,146
  • 9
  • 34
  • 46

10 Answers10

793

$(document).ready(function()
{
  $("tr:odd").css({
    "background-color":"#000",
    "color":"#fff"});
});
tbody td{
  padding: 30px;
}

tbody tr:nth-child(odd){
  background-color: #4C8BF5;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>13</td>
</tr>
</tbody>
</table>

There is a CSS selector, really a pseudo-selector, called nth-child. In pure CSS you can do the following:

tr:nth-child(even)
    background-color: #000000;
}

Note: No support in IE 8.

Or, if you have jQuery:

$(document).ready(function()
{
    $("tr:even").css("background-color", "#000000");
});
mybrave
  • 1,477
  • 3
  • 17
  • 34
Russell Dias
  • 67,406
  • 5
  • 48
  • 71
207

You have the :nth-child() pseudo-class:

table tr:nth-child(odd) td{
    ...
}
table tr:nth-child(even) td{
    ...
}

In the early days of :nth-child() its browser support was kind of poor. That's why setting class="odd" became such a common technique. In late 2013 I'm glad to say that IE6 and IE7 are finally dead (or sick enough to stop caring) but IE8 is still around — thankfully, it's the only exception.

Chiramisu
  • 4,589
  • 7
  • 44
  • 77
Álvaro González
  • 135,557
  • 38
  • 250
  • 339
  • 5
    Preferred answer as it doesn't apply CSS to header – Mike Jun 01 '15 at 15:51
  • Hi this is late by a couple of years but what about adding a selected class with a bg-color with jqeury to the table row when clicked. I noticed that the :nth-child pseudo class bg-color overrides when you add a "selected" class with jqeury – dutchkillsg May 25 '18 at 16:29
  • @dutchkillsg That appears to be a [brand new question](https://stackoverflow.com/questions/50534633/overing-nth-child-pseudo-classes-with-jquery) rather than a comment to my answer... – Álvaro González May 25 '18 at 18:21
  • 1
    For "zebra stripes" (i.e. vertical), just exchange `tr:nth-child(odd)` with `td:nth-of-type(odd)`. Note that in this case you're applying a different pseudo-class to the `td` rather than `tr` elements. – Chiramisu Jul 08 '19 at 16:28
45

Just add the following to your html code (withing the <head>) and you are done.

HTML:

<style>
      tr:nth-of-type(odd) {
      background-color:#ccc;
    }
</style>

Easier and faster than jQuery examples.

dnl-blkv
  • 1,818
  • 16
  • 22
bmich72
  • 610
  • 5
  • 6
  • 2
    This should be the accepted answer. As far as possible, CSS should handle styling, while javascript can be used to handle other matters. – Tormod Haugene Dec 02 '15 at 14:33
  • I don't do html on a daily basis. `#ccc` doesn't seem like a valid colour code to me. Can you explain? Thanks. – tommy.carstensen Apr 03 '18 at 01:10
  • 2
    @tommy.carstensen it's called "shorthand hexadecimal form". Basically `#ccc` gets expanded to `#cccccc`, which means that each RGB colour has the hexadecimal value `cc`, or decimal value `204` (i.e. `rgb(204, 204, 204)`). Read more about it here -> https://en.wikipedia.org/wiki/Web_colors#Shorthand_hexadecimal_form – Nick Grealy May 09 '18 at 14:53
14

can i write my html like this with use css ?

Yes you can but then you will have to use the :nth-child() pseudo selector (which has limited support though):

table.alternate_color tr:nth-child(odd) td{
    /* styles here */
}
table.alternate_color tr:nth-child(even) td{
   /* styles here */
}
mybrave
  • 1,477
  • 3
  • 17
  • 34
Sarfraz
  • 367,681
  • 72
  • 526
  • 573
13

We can use odd and even CSS rules and jQuery method for alternate row colors

Using CSS

table tr:nth-child(odd) td{
           background:#ccc;
}
table tr:nth-child(even) td{
            background:#fff;
}

Using jQuery

$(document).ready(function()
{
  $("table tr:odd").css("background", "#ccc");
  $("table tr:even").css("background", "#fff");
});

table tr:nth-child(odd) td{
           background:#ccc;
}
table tr:nth-child(even) td{
            background:#fff;
}
<table>
  <tr>
    <td>One</td>
    <td>one</td>
   </tr>
  <tr>
    <td>Two</td>
    <td>two</td>
  </tr>
</table>
Santosh Khalse
  • 10,808
  • 3
  • 34
  • 36
10

Most of the above codes won't work with IE version. The solution that works for IE+ other browsers is this.

   <style type="text/css">
      tr:nth-child(2n) {
             background-color: #FFEBCD;
        }
</style>
Sriwantha Attanayake
  • 7,386
  • 5
  • 39
  • 44
9
<script type="text/javascript">
$(function(){
  $("table.alternate_color tr:even").addClass("d0");
   $("table.alternate_color tr:odd").addClass("d1");
});
</script>
Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
  • 47
    OK I know jQuery is fairly ubiquitous on this site but regardless you shouldn't post jQuery without explanation. This script won't work on its own. – DisgruntledGoat Jun 21 '10 at 12:36
4

You can use nth-child(odd/even) selectors however not all browsers (ie 6-8, ff v3.0) support these rules hence why most solutions fall back to some form of javascript/jquery solution to add the classes to the rows for these non compliant browsers to get the tiger stripe effect.

redsquare
  • 77,122
  • 20
  • 149
  • 156
3

There is a fairly easy way to do this in PHP, if I understand your query, I assume that you code in PHP and you are using CSS and javascript to enhance the output.

The dynamic output from the database will carry a for loop to iterate through results which are then loaded into the table. Just add a function call to the like this:

echo "<tr style=".getbgc($i).">";  //this calls the function based on the iteration of the for loop.

then add the function to the page or library file:

function getbgc($trcount)
{

$blue="\"background-color: #EEFAF6;\"";
$green="\"background-color: #D4F7EB;\"";
$odd=$trcount%2;
    if($odd==1){return $blue;}
    else{return $green;}    

}

Now this will alternate dynamically between colors at each newly generated table row.

It's a lot easier than messing about with CSS that doesn't work on all browsers.

Hope this helps.

mark
  • 31
  • 1
0

Please try this way: it can use in Html file for WebView

<head>
    
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
          
        th, td {
            text-align: left;
            padding: 8px;
        }
          
        tr:nth-child(even) {
            background-color: Lightgreen;
        }
    </style>
</head>
Mori
  • 941
  • 8
  • 16