Text Fixer Tools

CSS Table with Alternating Color Rows

You can use the nth child selector in CSS3 to very simply create tables with alternating row colors.

I'll show you an example of a table with alternating rows along with some working code that you can copy.

If you need it, I also have information on how to build a table with alternating columns.

Browser Support for Nth Child Selector

All modern browsers support the nth child selector but it's not supported in Internet Explorer 8 and versions below. With that in mind, I have added an extra css declaration so that in these older version of IE all the tables rows will have a default background color. This is just in case you've been cursed by your present employer to support ancient browsers.

This allows the table to still be visually legible and functional but it doesn't exactly replicate the alternating row color effect that is present in modern browsers. If this was of paramount importance to you then you'd have to write some javascript for alternating colors to achieve this effect.

CSS Table Example with Alternating Rows

TextTextText
TextTextText
TextTextText
TextTextText

The CSS Code for the Table

To make the table above you have to use the tr:nth-child(odd) selector to define all the odd rows in a table and the tr:nth-child(even) selector to define all the even rows of the table.

The complete working CSS code and HTML for the table example can be copied from the box below.


<style type="text/css">
	.TFtable{
		width:100%; 
		border-collapse:collapse; 
	}
	.TFtable td{ 
		padding:7px; border:#4e95f4 1px solid;
	}
	/* provide some minimal visual accomodation for IE8 and below */
	.TFtable tr{
		background: #b8d1f3;
	}
	/*  Define the background color for all the ODD background rows  */
	.TFtable tr:nth-child(odd){ 
		background: #b8d1f3;
	}
	/*  Define the background color for all the EVEN background rows  */
	.TFtable tr:nth-child(even){
		background: #dae5f4;
	}
</style>

<table class="TFtable">
	<tr><td>Text</td><td>Text</td><td>Text</td></tr>
	<tr><td>Text</td><td>Text</td><td>Text</td></tr>
	<tr><td>Text</td><td>Text</td><td>Text</td></tr>
	<tr><td>Text</td><td>Text</td><td>Text</td></tr>
</table>

Alternating Table Rows just with CSS

Anyways, it's now pretty simple to make a table with rows of alternating colors just by using CSS.

Modern support is good for all browsers except for IE8 and below which is now generally less than 1% of the market and so you can easily provide them with a visually readable alternative.

The Most Popular Tools

The Newest Tools