HTML Table - Zebra Stripes


If you add a background color on every other table row, you will get a nice zebra stripes effect.

To style every other table row element, use the :nth-child(even) selector like this:

Pseudo-class :nth-child()

The :nth-child() pseudo-class represents an element that has an+b siblings before it in the document tree, for any positive integer or zero value of n, and has a parent element.

Syntax

selector:nth-child(an+b){ properties }

 

  • The examples of an+b are as follows:
    •  :nth-child(2n) /* represents every even element */
    •  :nth-child(even) /* same, represents every even element */
    •  :nth-child(2n+1) /* represents every odd element */
    •  :nth-child(odd) /* same, represents every odd element */
    •  :nth-child(10n-1) /* represents the 9th, 19th, 29th, etc, element */

Live Demo & Try it yourself! Read More » »

HTML Table - Vertical Zebra Stripes


To make vertical zebra stripes, style every other column, instead of every other row.

Set the :nth-child(even) for table data elements like this:


Live Demo & Try it yourself! Read More » »

Combine Vertical and Horizontal Zebra Stripes


You can combine the styling from the two examples above and you will have stripes on every other row and every other column.

If you use a transparent color you will get an overlapping effect.

Use an rgba() color to specify the transparency of the color:


Live Demo & Try it yourself! Read More » »

Horizontal Dividers


If you specify borders only at the bottom of each table row, you will have a table with horizontal dividers.

Add the border-bottom property to all tr elements to get horizontal dividers:


Live Demo & Try it yourself! Read More » »

Hoverable Table


Use the :hover selector on tr to highlight table rows on mouse over:


Live Demo & Try it yourself! Read More » »