Estimated time: 20 minutes (about 8 minutes reading, 12 minutes practice)
Learning goals: By the end, you'll know how to build a table with headers, rows and cells, merge cells, add captions, and group table sections.
When to use tables
Tables are for tabular data: information that naturally fits rows and columns, like timetables, prices or scores. Don't use tables to lay out a page. That's a job for CSS.
Part 1: A basic table (4 min)
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Amira</td>
<td>28</td>
</tr>
<tr>
<td>Tom</td>
<td>34</td>
</tr>
</table>| Element | Meaning |
|---|---|
<table> | The whole table |
<tr> | A table row |
<th> | A header cell (bold and centred by default) |
<td> | A standard data cell |
Every row should have the same number of cells (unless you merge cells, see Part 3). Browsers show tables without borders by default. CSS adds them.
Part 2: Captions and sections (4 min)
Add structure so tables are easier to read and more accessible.
<table>
<caption>Monthly Sales (£)</caption>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Sales</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">January</th>
<td>1,200</td>
</tr>
<tr>
<th scope="row">February</th>
<td>1,450</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>2,650</td>
</tr>
</tfoot>
</table>-
<caption>is the table's title. It must come first inside<table>. -
<thead>,<tbody>and<tfoot>group the header, main rows and footer. -
scope="col"orscope="row"tells screen readers whether a header applies to a column or a row.
Part 3: Merging cells (4 min)
Use colspan to stretch a cell across columns and rowspan to stretch it down rows.
<table>
<tr>
<th colspan="2">Contact Details</th>
</tr>
<tr>
<td>Email</td>
<td>hello@example.com</td>
</tr>
<tr>
<td>Phone</td>
<td>01234 567890</td>
</tr>
</table><table>
<tr>
<th rowspan="2">Day</th>
<th>Morning</th>
<th>Afternoon</th>
</tr>
<tr>
<td>Maths</td>
<td>Art</td>
</tr>
</table>When a cell spans several columns or rows, leave out the cells it covers, so the row totals still add up.
Quick reference
| Attribute | Used on | Effect |
|---|---|---|
colspan | <th>, <td> | Spans multiple columns |
rowspan | <th>, <td> | Spans multiple rows |
scope | <th> | Says whether the header is for a column or row |
Common mistakes
- Using tables for page layout
- Rows with different numbers of cells
-
Using
<td>for headings instead of<th> -
Putting
<tr>directly inside<table>while also using<thead>or<tbody>(use sections consistently) -
Forgetting to close
</tr>,</td>or</table> -
Adding borders with old attributes like
border="1"instead of CSS
Key takeaways
-
<table>contains<tr>rows, which contain<th>and<td>cells. -
Use
<th>for headings and addscopefor accessibility. -
<caption>,<thead>,<tbody>and<tfoot>add meaning and structure. -
colspanandrowspanmerge cells. - Tables are for data, not layout.
Exercises (12 min)
-
Identify (1 min). Which element is used for each?
- a) A column heading
- b) A row
- c) The table's title
Answers: a)<th>, b)<tr>, c)<caption>. -
Fix the errors (3 min).
<table> <tr> <td>Fruit</td> <td>Colour </tr> <td>Apple</td> <td>Red</td> <caption>Fruit Colours</caption> </table>
Hints: use<th>for headings, close the second</td>, wrap "Apple" and "Red" in a<tr>, and move<caption>to the top of the table. -
Build it (6 min). Create
tables.htmlwith a weekly timetable:-
a
<caption> -
a
<thead>with the days Monday to Friday as column headers (plus an empty first cell) -
a
<tbody>with at least three time slots, using<th scope="row">for each time -
one
colspancell, such as a lunch break stretching across all days
-
a
-
Challenge (2 min). Add a
<tfoot>row, and userowspanto make a double-length lesson run across two time slots.
Lesson 7 checkpoint
-
I can build a table with
<tr>,<th>and<td>. -
I can add a caption and group rows with
<thead>,<tbody>and<tfoot>. -
I can merge cells with
colspanandrowspan. - I know tables are for data, not layout.