Categories

Tools

bitdefender

1&1 Web Hosting

How to create a table with rows of alternating colors with CSS


When you must have a data table html, it is useful to color the rows with alternating colors. This is possible thanks to the rules css tr: nth-child (even) and tr: nth-child (odd).

The following example shows a table with a few records from the Northwind database.

Code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to create a table with alternating color rows with CSS - MdmSoft</title>
    <style type="text/css">
        #tableCustomers {
            border-collapse: collapse;
            font-family: Verdana;
            font-size: 11px;
            width: 520px;
        }

            #tableCustomers th {
                background: #aaa;
                color: #ffd800;
                text-align:left;
            }

            #tableCustomers td {
                border: 1px solid #bbb;
                height: 10px;
                padding: 2px 4px;
            }

            #tableCustomers tr {
                background: #fff;
            }
                /*  
				Set the background color for all the ODD background rows  */
                #tableCustomers tr:nth-child(odd) {
                    background-color: rgb(255,240,240);
                }
                /*  Define the background color for all the EVEN background rows  */
                #tableCustomers tr:nth-child(even) {
                    background: rgb(240,240,255);
                }
    </style>
</head>
<body>
    <h1></h1>
    <table border="1" id="tableCustomers">
        <tr>
            <th scope="col">Company Name</th>
            <th scope="col">Contact Title</th>
            <th scope="col">City</th>
            <th scope="col">Country</th>
        </tr>
        <tr>
            <td>Wolski  Zajazd</td>
            <td>Owner</td>
            <td>Warszawa</td>
            <td>Poland</td>
        </tr>
        <tr>
            <td>Wilman Kala</td>
            <td>Owner/Marketing Assistant</td>
            <td>Helsinki</td>
            <td>Finland</td>
        </tr>
        <tr>
            <td>White Clover Markets</td>
            <td>Owner</td>
            <td>Seattle</td>
            <td>USA</td>
        </tr>
        <tr>
            <td>Wellington Importadora</td>
            <td>Sales Manager</td>
            <td>Resende</td>
            <td>Brazil</td>
        </tr>
        <tr>
            <td>Wartian Herkku</td>
            <td>Accounting Manager</td>
            <td>Oulu</td>
            <td>Finland</td>
        </tr>
        <tr>
            <td>Vins et alcools Chevalier</td>
            <td>Accounting Manager</td>
            <td>Reims</td>
            <td>France</td>
        </tr>
        <tr>
            <td>Victuailles en stock</td>
            <td>Sales Agent</td>
            <td>Lyon</td>
            <td>France</td>
        </tr>
        <tr>
            <td>Vaffeljernet</td>
            <td>Sales Manager</td>
            <td>århus</td>
            <td>Denmark</td>
        </tr>
        <tr>
            <td>Trail's Head Gourmet Provisioners</td>
            <td>Sales Associate</td>
            <td>Kirkland</td>
            <td>USA</td>
        </tr>
        <tr>
            <td>Tradição Hipermercados</td>
            <td>Sales Representative</td>
            <td>São Paulo</td>
            <td>Brazil</td>
        </tr>
        <tr>
            <td>Tortuga Restaurante</td>
            <td>Owner</td>
            <td>México D.F.</td>
            <td>Mexico</td>
        </tr>
        <tr>
            <td>Toms Spezialitäten</td>
            <td>Marketing Manager</td>
            <td>Münster</td>
            <td>Germany</td>
        </tr>
        <tr>
            <td>The Cracker Box</td>
            <td>Marketing Assistant</td>
            <td>Butte</td>
            <td>USA</td>
        </tr>
        <tr>
            <td>The Big Cheese</td>
            <td>Marketing Manager</td>
            <td>Portland</td>
            <td>USA</td>
        </tr>
        <tr>
            <td>Suprêmes délices</td>
            <td>Accounting Manager</td>
            <td>Charleroi</td>
            <td>Belgium</td>
        </tr>
    </table>
</body>
</html>

Here is the result for the sample:

How to create a table with alternating color rows with css

Posted in CSS by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.