CSS: Styling HTML Table
By Xah Lee. Date: . Last updated: .
This page shows you how to style the HTML โtableโ tag with CSS.
Table sans CSS
<table>
<tr><td>cat</td><td>dog</td></tr>
<tr><td>bird</td><td>fish</td></tr>
</table>
browser shows:
Table cells with borders
.cellborder6870 td {
border: solid 1px gray;
}
<table class="cellborder6870">
<tr><td>cat</td><td>dog</td></tr>
<tr><td>bird</td><td>fish</td></tr>
</table>
Table cells with shared border
.sharedbd1800 {
border-collapse: collapse;
}
.sharedbd1800 td {
border: solid 1px gray;
}
border-collapse: collapse means draw a single line between neighbor cells.
border-collapse: separate
Cell Padding
.pad7473 td {
border: solid 1px green;
padding: 1ex;
}
Alternating colors for rows
| dog | 1 |
| dog | 2 |
| dog | 3 |
| dog | 4 |
| dog | 5 |
| dog | 6 |
| dog | 7 |
| dog | 8 |
.alt6996 tr:nth-child(2n) {
background-color: silver;
}
HTML 4 Table attributes vs CSS Styling
Here is a comparison showing HTML 4 Table attributes and the CSS equivalent.
| HTML 4 table attributes | CSS |
cellpadding="3" | padding: 3px |
cellspacing="5" | margin: 5px |
bgcolor="lightgrey" | background-color: lightgrey |
align="right" | text-align: right |
Note that these HTML 4 table attributes are obsolete in HTML 5.