Skip to main content

HTML Table Styling

Use CSS to make your tables look better.


HTML Table - Zebra Stripes

Giving every other row a background color creates a zebra-stripe effect. Use the tr:nth-child(even) selector.

Note: Using (odd) instead colors rows 1, 3, 5, and so on, rather than 2, 4, 6.

Example

Every other row colored.

Prompt
A table of numbers 1 to 20 in a 5-row by 4-column grid, with a zebra-stripe effect coloring every other row using the CSS tr:nth-child(even) selector.
Output: complete HTML page

HTML Table - Vertical Zebra Stripes

For vertical stripes, color every other column instead of every other row, using :nth-child(even) on the cells.

Note: Put the selector on both th and td if you want the stripes on headers and regular cells.

Example

Every other column colored.

Prompt
A table of numbers 1 to 20 with vertical zebra stripes, coloring every other column using td:nth-child(even) and th:nth-child(even).
Output: complete HTML page

Combine Vertical and Horizontal Zebra Stripes

You can combine both to stripe every other row and every other column. Using a transparent rgba() color makes the overlaps blend into an overlapping effect.

Example

Stripes on both rows and columns with transparency.

Prompt
A table of numbers 1 to 20 combining horizontal and vertical zebra stripes using a transparent rgba() background on both even rows and even columns, producing an overlapping effect.
Output: complete HTML page

Horizontal Dividers

Adding a border only at the bottom of each row gives a table with horizontal dividers. Add the border-bottom property to all tr elements.

Example

A table with horizontal divider lines.

Prompt
A table with only horizontal dividers, made by adding a bottom border to every tr. Columns First Name, Last Name, Savings with three people.
Output: complete HTML page

Hoverable Table

Use the :hover selector on tr to highlight a row when the mouse moves over it.

Example

Rows that highlight on hover.

Prompt
A table whose rows highlight on mouse over using the tr:hover selector. Columns First Name, Last Name, Savings with three people.
Output: complete HTML page