Table rows, headers, and cells
Inside this lesson
- Grouping rows with thead, tbody, and tfoot
- Adding a caption
- Merging cells with colspan
- Merging cells with rowspan
Short answer
Bigger tables are easier to read and style when you group rows. <thead> holds header rows, <tbody> holds data rows, and <tfoot> holds totals. <caption> gives the table a title. colspan and rowspan let one cell stretch across columns or rows.
thead, tbody, tfoot, and caption
<table>
<caption>Pili tart orders, first week of June</caption>
<thead>
<tr><th>Day</th><th>Boxes</th><th>Sales</th></tr>
</thead>
<tbody>
<tr><td>Monday</td><td>12</td><td>₱3,600</td></tr>
<tr><td>Tuesday</td><td>9</td><td>₱2,700</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td><td>21</td><td>₱6,300</td></tr>
</tfoot>
</table>
The caption must be the first child of the table. When a long table is printed, browsers can repeat the thead on each page.
colspan: stretch across columns
<tr>
<th colspan="2">Room type</th>
<th>Rate</th>
</tr>
colspan="2" makes one cell as wide as two columns. That row now needs one fewer cell.
rowspan: stretch down rows
<tr>
<th rowspan="2">Weekend</th>
<td>Saturday</td>
<td>₱2,500</td>
</tr>
<tr>
<td>Sunday</td>
<td>₱2,500</td>
</tr>
The Weekend cell covers two rows, so the next row starts one cell shorter. Draw the table on paper before coding merged cells. It saves a lot of confusion.
Keep merging simple
Merged cells are harder for screen readers and harder to fit on small screens. If a table needs many merges, consider splitting it into two simple tables.
Try it: add structure
Wrap the rows in thead, tbody, and tfoot, add a caption, and make the Total label span two columns.
Live preview
Quick quiz
Common mistakes
- Forgetting to remove a cell after colspan. The row grows too wide and columns shift.
- Caption in the wrong place. It must be the first element inside table.
- Overusing merged cells. Complex tables confuse screen readers.
Your action
Tick each item when you can do it. Your progress saves on this device.
Key terms
- <thead>
- Groups header rows.
- <tbody>
- Groups the main data rows.
- <tfoot>
- Groups footer rows like totals.
- <caption>
- A visible title for a table.
- colspan
- Makes a cell span several columns.
- rowspan
- Makes a cell span several rows.
Questions beginners ask
Is tbody required?
Browsers add it automatically, but writing it keeps your code clear.
Can a table have two tbody elements?
Yes. Use them to group sections, like one per month.
Does caption help SEO?
It helps readers and screen readers first. It also gives search engines context.
Related lessons
Need a website built or fixed while you practice? The team at Remar Digital can help.