How to create a zebra striped table with CSS?

To create a table on a web page, we use the <table> element. It allows us to set the table row using the <tr> element. Within that, the <td> elements are used to place the data. A table can also be striped. Such striped tables have a different look for every alternative row. To set a property for every alternative row, we will use the nth-child(even) property. Let us see how to create a zebra striped table with HTML and CSS.

Syntax

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

Example: Creating a Zebra Striped Table

The following example creates a table with alternating row colors using CSS −

<!DOCTYPE html>
<html>
<head>
<style>
    table {
        border-collapse: collapse;
        border-spacing: 0;
        width: 100%;
        border: 1px solid #ddd;
    }
    th, td {
        font-weight: bold;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        font-size: 18px;
        text-align: left;
        padding: 16px;
    }
    tr:nth-child(even) {
        background-color: #f2f2f2;
    }
    tr:nth-child(odd) {
        background-color: #ffffff;
    }
</style>
</head>
<body>
    <h1>Zebra Striped Table Example</h1>
    <table>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Birth Month</th>
        </tr>
        <tr>
            <td>Jack</td>
            <td>Roy</td>
            <td>January</td>
        </tr>
        <tr>
            <td>Steve</td>
            <td>Smith</td>
            <td>March</td>
        </tr>
        <tr>
            <td>Brandon</td>
            <td>Anderson</td>
            <td>February</td>
        </tr>
        <tr>
            <td>Sarah</td>
            <td>Johnson</td>
            <td>April</td>
        </tr>
    </table>
</body>
</html>
A table with alternating row colors appears: the header row and odd rows have white background, while even rows have light gray background (#f2f2f2), creating a zebra stripe pattern.

Alternative Method: Using Different Colors

You can also create more prominent stripes using darker colors −

<!DOCTYPE html>
<html>
<head>
<style>
    table {
        border-collapse: collapse;
        width: 100%;
        border: 1px solid #333;
    }
    th, td {
        padding: 12px;
        text-align: left;
        border-bottom: 1px solid #ddd;
    }
    th {
        background-color: #4CAF50;
        color: white;
    }
    tr:nth-child(even) {
        background-color: #8b8b8b;
        color: white;
    }
</style>
</head>
<body>
    <table>
        <tr>
            <th>Product</th>
            <th>Price</th>
            <th>Stock</th>
        </tr>
        <tr>
            <td>Laptop</td>
            <td>$999</td>
            <td>15</td>
        </tr>
        <tr>
            <td>Mouse</td>
            <td>$25</td>
            <td>50</td>
        </tr>
        <tr>
            <td>Keyboard</td>
            <td>$75</td>
            <td>30</td>
        </tr>
    </table>
</body>
</html>
A table with green header and dark gray even rows with white text appears, creating a more prominent zebra stripe effect.

Conclusion

The nth-child(even) pseudo-selector is the key to creating zebra striped tables. It automatically applies styles to every even-numbered row, making large tables more readable and visually appealing.

Updated on: 2026-03-15T14:41:37+05:30

862 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements