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:

catdog
birdfish

Table cells with borders

catdog
birdfish
.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

catdog
birdfish
.sharedbd1800 {
 border-collapse: collapse;
}

.sharedbd1800 td {
 border: solid 1px gray;
}

Cell Padding

catdog
birdfish
.pad7473 td {
 border: solid 1px green;
 padding: 1ex;
}

Alternating colors for rows

dog1
dog2
dog3
dog4
dog5
dog6
dog7
dog8
.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 attributesCSS
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.

HTML Table