How to Give Alternate Rows of a Table a Different Colour (HTML/CSS)

Creating Zebra Striped Rows for a Table


How to Give Alternate Rows of a Table a Different Colour (HTML/CSS)

by Christopher Heng, thesitewizard.com

This article deals with how you can give alternate rows of a table a different colour ("color" in US English) using CSS. This makes your table faster to visually scan, since it's easier to spot which line a particular piece of data is on. (If you are not sure what I mean, scroll down to see the illustration later in this article.)

Prerequisites

Since this guide deals with HTML and CSS, you will need some knowledge of both. At the very minimum, you should know how to insert CSS into a web page. If your website is a blog, you must have some way of inserting CSS into your post.

Those who don't even have a website yet should probably start at the beginning, with How to Create a Website.

CSS for Alternating Colours Between Table Rows

Let's say that you want to create a table that looks something like the following:

Demo table
First column Second column Third column
Data Data Data
Data Data Data
Data Data Data

The HTML code for the demo is as follows:

<table id="demo">
<caption>Demo table</caption>
<tr>
  <td>First column</td>
  <td>Second column</td>
  <td>Third column</td>
</tr>
<tr>
  <td>Data</td>
  <td>Data</td>
  <td>Data</td>
</tr>
<tr>
  <td>Data</td>
  <td>Data</td>
  <td>Data</td>
</tr>
<tr>
  <td>Data</td>
  <td>Data</td>
  <td>Data</td>
</tr>
</table>

And the CSS code is:

table#demo tr:nth-child(odd) {
  background-color: #0074ab ;
  color: yellow ;
}
table#demo tr:nth-child(even) {
  background-color: #e6f7ff ;
}
table#demo {
  border-collapse: collapse ;
}
table#demo td {
  padding: 5px ;
}
table#demo caption {
  font-style: italic ;
  background-color: black ;
  color: white ;
}

Explanation of the CSS

The pseudo-class that allows you to do all the alternate shading magic is nth-child. Specifically, nth-child(odd) matches all the odd numbered children/descendants and nth-child(even) the even. Take the following line from the demo above.

table#demo tr:nth-child(odd) {

Translated to English, it means that the rules that follow apply to all odd-numbered <tr> descendants of the <table> with the id demo. You can place any number of normal CSS rules in that block. In my case, I merely placed rules to change the foreground and background colours. If your table has a different id, you should change the part that says "#demo" accordingly.

You can of course use different colours for your table. Just specify the value you want in the background-color rule. The text (foreground) colour is controlled using a color rule. For maximum compatibility, state your colours using RGB numbers, as I did, instead of their English names. You can find a list of HTML values for various colours on Wikipedia, or, if your editor has a colour picker, just use that.

Note that only this block of rules is needed to create the alternate-row shading effect.

table#demo tr:nth-child(odd) {
  background-color: #0074ab ;
  color: yellow ;
}
table#demo tr:nth-child(even) {
  background-color: #e6f7ff ;
}

The remainder of the rules are there merely because I wanted my table to have a certain appearance, and you can ignore them unless you want the same effect. For example, I added border-collapse: collapse; to remove the space between each table cell, padding to add a bit of space within each cell (so that the words don't go right up to the edge), and rules for the caption to put it on a black background.

Compatibility

This code uses CSS features that require a fairly modern browser, such as the current versions of Chrome, Firefox, Edge and Safari. It will not work correctly in Internet Explorer 8 and earlier.

Copyright © 2021 Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to thesitewizard.com newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Articles

New Articles

Popular Articles

How to Link to This Page

It will appear on your page as:

How to Give Alternate Rows of a Table a Different Colour (HTML/CSS)





Home
Donate
Contact
Link to Us
No Spam Policy
Privacy Policy
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

 
Free webmasters and programmers resources, scripts and tutorials
 
HowtoHaven.com: Free How-To Guides
 
Site Design Tips at thesitewizard.com
Find this site useful?
Please link to us.