Sub topic 9
Styling Tables with CSS
What this chapter assumes
You should know the basic table elements (table, tr, td, th, caption, thead, tbody, tfoot). You should also be comfortable with CSS selectors, the box model, and properties such as border and padding. This chapter adds no new markup. It only styles the tables you already know how to build.
Start with the defaults
A browser draws an unstyled table with no borders. Each cell is as wide as its content needs. Header cells are bold and centered. Every cell sits slightly apart from its neighbors.
Once you add borders to cells, a second default appears. Each cell has its own border, and a gap separates neighboring cells. That gap is the next thing to fix.
Merge borders with border-collapse
The border-collapse property controls how cell borders meet.
| Value | Result | Notes |
|---|---|---|
| `separate` (default) | Each cell keeps its own border, with a gap between cells. | `border-spacing` sets the gap size. |
| `collapse` | Neighboring borders merge into one line. | `border-spacing` is ignored. |
Most data tables use collapse. It gives a clean grid, and you write one border rule instead of managing double lines.
Padding, alignment, and header shading
Put borders and padding on th and td, not on table. Padding gives text room and works with both collapse modes. Use text-align to line up content. Text normally reads best left-aligned, and numbers read best right-aligned.
You can shade the header with a rule on thead th. Add tbody tr:nth-child(even) to stripe alternate rows. The nth-child count starts again inside each row group, so the first body row is odd even when the header has its own row.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Styled table</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid #ccc;
padding: 0.5rem 0.75rem;
text-align: left;
}
thead th {
background: #f0f0f0;
}
tbody tr:nth-child(even) {
background: #fafafa;
}
caption {
caption-side: top;
font-weight: bold;
text-align: left;
}
</style>
</head>
<body>
<table>
<caption>Monthly expenses</caption>
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Cost</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Rent</th>
<td>900</td>
</tr>
<tr>
<th scope="row">Food</th>
<td>300</td>
</tr>
</tbody>
</table>
</body>
</html>Save this as an .html file and open it in a browser. You should see a full-width grid with a shaded header and a bold caption above it.
Style the caption
The caption element is a normal element, so you can set its font, color, and margin. caption-side moves it above (top) or below (bottom) the table. Keep it in the markup, because screen readers use it as the table's name. Do not replace it with a styled heading.
Style whole columns
The col element lets one rule reach every cell in a column. This works only for a few properties: border, background, width, and visibility. Other properties, such as text-align, do not pass from col to its cells.
<style>
.numeric {
background: #eef6ff;
width: 8rem;
}
td:nth-child(n + 2) {
text-align: right;
}
</style>
<table>
<colgroup>
<col>
<col span="2" class="numeric">
</colgroup>
<tr>
<th scope="col">City</th>
<th scope="col">Population</th>
<th scope="col">Area</th>
</tr>
<tr>
<td>Oslo</td>
<td>700000</td>
<td>454</td>
</tr>
</table>The col rule sets the background and width of the last two columns. The td:nth-child rule right-aligns the numbers, which col cannot do.
Common errors
- Double borders. You added
borderto cells but leftborder-collapseatseparate. Setcollapseontable. - Spacing has no effect.
border-spacingdoes nothing undercollapse. Use cell padding instead. - Alignment on
colis ignored. Select the cells withtd:nth-child()or give them a class. - Stripes look wrong.
nth-childcounts rows inside one parent. Athead,tbody, andtfooteach start counting again. - Using
display: blockon table parts. This removes the table layout, and some browsers then stop exposing table semantics to assistive technology. Style the table without changing itsdisplayvalue.
Exercise
A table has border: 1px solid #ccc on every td, but you see a visible gap and double lines between cells. Which single declaration on table fixes it, and why does setting border-spacing: 0 alone sometimes fall short?
Show answer
Add border-collapse: collapse; to table. It merges neighboring borders into one line. border-spacing: 0 removes the gap, but with separate each cell still draws its own border, so adjacent borders sit side by side and look twice as thick.