I am using a table with alternate row color with this.
tr.d0 td {_x000D_
background-color: #CC9999;_x000D_
color: black;_x000D_
}_x000D_
tr.d1 td {_x000D_
background-color: #9999CC;_x000D_
color: black;_x000D_
}
_x000D_
<table>_x000D_
<tr class="d0">_x000D_
<td>One</td>_x000D_
<td>one</td>_x000D_
</tr>_x000D_
<tr class="d1">_x000D_
<td>Two</td>_x000D_
<td>two</td>_x000D_
</tr>_x000D_
</table>
_x000D_
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?
This question is related to
html
css
html-table
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.
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{_x000D_
background:#ccc;_x000D_
}_x000D_
table tr:nth-child(even) td{_x000D_
background:#fff;_x000D_
}
_x000D_
<table>_x000D_
<tr>_x000D_
<td>One</td>_x000D_
<td>one</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Two</td>_x000D_
<td>two</td>_x000D_
</tr>_x000D_
</table>
_x000D_
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.
<script type="text/javascript">
$(function(){
$("table.alternate_color tr:even").addClass("d0");
$("table.alternate_color tr:odd").addClass("d1");
});
</script>
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.
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 */
}
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>
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.
Source: Stackoverflow.com