HTML Tables
Tables arrange data in rows and columns, like a spreadsheet. Use them for genuinely tabular data — timetables, price lists, scores, comparisons — not for page layout.
The basic building blocks
| Element | Meaning |
|---|---|
<table> |
The whole table |
<tr> |
A table row |
<th> |
A header cell (bold and centered by default) |
<td> |
A data cell |
<caption> |
A title for the table (first child of <table>) |
<thead>, <tbody>, <tfoot> |
Group header, body and footer rows |
A table is built row by row. Each <tr> contains cells, and every row should have the same number of columns.
<table>
<caption>Weekend reading list</caption>
<tr>
<th>Title</th>
<th>Author</th>
<th>Pages</th>
</tr>
<tr>
<td>The Hobbit</td>
<td>J. R. R. Tolkien</td>
<td>310</td>
</tr>
<tr>
<td>Matilda</td>
<td>Roald Dahl</td>
<td>240</td>
</tr>
</table>Without CSS, tables look quite plain: no borders and little spacing. Let's fix that.
Borders and spacing
Table styling is done with CSS. The property border-collapse: collapse merges neighboring cell borders into single lines, which almost always looks better.
<style>
table { border-collapse: collapse; font-family: sans-serif; }
th, td { border: 1px solid #999; padding: 8px 12px; }
th { background-color: #e34c26; color: white; text-align: left; }
tbody tr:nth-child(even) { background-color: #f6f6f6; }
tbody tr:hover { background-color: #ffe9e2; }
td.num { text-align: right; }
</style>
<table>
<caption>Fruit stall prices</caption>
<thead>
<tr><th>Fruit</th><th>Origin</th><th>Price / kg</th></tr>
</thead>
<tbody>
<tr><td>🍎 Apples</td><td>Local</td><td class="num">2.40</td></tr>
<tr><td>🍌 Bananas</td><td>Ecuador</td><td class="num">1.80</td></tr>
<tr><td>🍇 Grapes</td><td>Italy</td><td class="num">4.10</td></tr>
<tr><td>🍊 Oranges</td><td>Spain</td><td class="num">2.90</td></tr>
</tbody>
</table>Useful styling properties:
| CSS | Effect |
|---|---|
border-collapse: collapse |
Single borders between cells |
padding on th, td |
Space inside cells |
width: 100% on table |
Table fills its container |
text-align |
Horizontal alignment in cells |
vertical-align |
Vertical alignment (top, middle, bottom) |
tr:nth-child(even) |
Zebra stripes for readability |
Spanning rows and columns
A cell can stretch across several columns with colspan or several rows with rowspan. Remember to leave out the cells that the span replaces.
<style>
table { border-collapse: collapse; font-family: sans-serif; text-align: center; }
th, td { border: 1px solid #777; padding: 8px 14px; }
th { background: #eef2ff; }
</style>
<table>
<caption>School timetable</caption>
<tr>
<th>Time</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th>
</tr>
<tr>
<th>9:00</th><td>Math</td><td>Art</td><td rowspan="2">Science lab</td>
</tr>
<tr>
<th>10:00</th><td>History</td><td>Music</td>
</tr>
<tr>
<th>11:00</th><td colspan="3">Sports day 🏃</td>
</tr>
</table>Header scope
The scope attribute on <th> tells assistive technology whether a header applies to a column (scope="col") or a row (scope="row"). This helps screen readers announce "Tuesday, 10:00: Music" instead of just "Music".
<tr><th scope="col">Name</th><th scope="col">Score</th></tr>
<tr><th scope="row">Ana</th><td>92</td></tr>Column groups
<colgroup> and <col> let you style entire columns at once (with a limited set of properties such as background and width):
<style>
table { border-collapse: collapse; font-family: sans-serif; }
th, td { border: 1px solid #aaa; padding: 6px 12px; }
</style>
<table>
<colgroup>
<col>
<col span="2" style="background-color: #fff6d6;">
<col style="background-color: #e3f7e3;">
</colgroup>
<tr><th>Player</th><th>Round 1</th><th>Round 2</th><th>Total</th></tr>
<tr><td>Kim</td><td>12</td><td>15</td><td>27</td></tr>
<tr><td>Luis</td><td>9</td><td>18</td><td>27</td></tr>
</table>Tip: Don't use tables to position parts of a page side by side. Use CSS Flexbox or Grid for layout; layout tables confuse screen readers and are hard to make responsive.
Note: Wide tables can overflow on phones. Wrap the table in a
<div style="overflow-x: auto">so it can scroll sideways.
Test yourself
Which element defines a table row?
<td><th><tr><row>
How do you make a cell span three columns?
<td span="3"><td colspan="3"><td rowspan="3"><td cols="3">
Which CSS property merges adjacent cell borders into a single line?
border-spacing: 0border-merge: trueborder-collapse: collapseborder-style: single