Have a question?
Message sent Close
View Categories

CSS Table Style

2 min read

Table Padding #

To control the space between the border and the content in a table, use the padding property on <td> and <th> elements:

The padding Property #

This property adds space between the border and the content in a table.

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Cleveland Brown $250
th, td { padding: 15px; text-align: left; }

Horizontal Dividers #

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Cleveland Brown $250

Add the border-bottom property to <th> and <td> for horizontal dividers:

th, td { border-bottom: 1px solid #ddd; }

Hoverable Table #

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

First Name Last Name Points
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Cleveland Brown $250
tr:hover {background-color: #f5f5f5;}

Striped Tables #

First Name Last Name Points
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Cleveland Brown $250

For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:

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

Table Color #

The example below specifies the background color and text color of <th> elements:

Colored Table Header #

Firstname Lastname Savings
Peter Griffin $100
Lois Griffin $150
Joe Swanson $300
Cleveland Brown $250
th { background-color: #4CAF50; color: white; }

Powered by BetterDocs

Leave a Reply