Sub topic 13
Tables for tabular data
This chapter assumes you can write a basic HTML document, know the parts of an element (start tag, content, end tag, attributes), and understand block and inline content. It covers when to use a table, how to build one, and the mistakes that come up most often.
When to use a table
A table presents data that has meaning in rows and columns, such as schedules, prices or measurements. Do not use a table to arrange the page layout. Layout is a job for semantic elements and CSS. Screen readers announce a table as a grid of related cells, so a layout table gives confusing output.
The core elements
A table is built from a few nested elements:
| Element | Purpose |
|---|---|
| `table` | Wraps the whole table |
| `caption` | Gives the table a title |
| `thead`, `tbody`, `tfoot` | Group header, body and footer rows |
| `tr` | One row |
| `th` | A header cell |
| `td` | A data cell |
Here is a simple table:
<table>
<caption>Weekly study hours</caption>
<thead>
<tr>
<th scope="col">Day</th>
<th scope="col">Hours</th>
</tr>
</thead>
<tbody>
<tr>
<td>Monday</td>
<td>2</td>
</tr>
<tr>
<td>Tuesday</td>
<td>1</td>
</tr>
</tbody>
</table>The caption must be the first child of table. It names the table for every reader, including those using assistive technology. The thead holds the row of column headings, and tbody holds the data rows. Each tr contains only th and td cells.
Header cells and scope
Use th for any cell that labels other cells. The scope attribute says which cells a header applies to. The value col means the header labels the cells below it. The value row means it labels the cells to its right. Assistive technology uses this to read each data cell together with its headers, for example "Monday, Hours, 2".
If a table has row headings as well, make the first cell of each row a th:
<tr>
<th scope="row">Monday</th>
<td>2</td>
</tr>How a table is structured
Spanning cells
A cell can cover more than one column or row. Use colspan for columns and rowspan for rows. Both take a whole number.
<table>
<caption>Quarterly sales</caption>
<thead>
<tr>
<th scope="col">Region</th>
<th scope="col" colspan="2">Sales</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">North</th>
<td>120</td>
<td>135</td>
</tr>
</tbody>
</table>Here the "Sales" header covers two columns, so every row must still add up to three columns in total. Keep spans simple. Complex spanning is hard for screen reader users to follow.
Common errors
- Layout tables. Using a table to position content is wrong. Use semantic layout elements and CSS.
- Missing header cells. Putting bold
tdcells in the first row instead ofthremoves the meaning that assistive technology needs. - Cells outside rows. A
tdorthmust sit inside atr. Placing text directly intableis invalid. - Wrong row lengths. Rows with different numbers of columns, once spans are counted, produce a ragged table.
- Missing caption. Without a caption, the table has no title for readers who skip to it.
- Styling with attributes. Old attributes such as
borderandcellpaddingare obsolete. Use CSS.
Exercise
A table has a first row of column labels written as td cells in bold text. What should you change, and why?
Show answer
Change those cells to th elements and add scope="col". Bold td cells look like headings, but they carry no header meaning, so assistive technology cannot link each data cell to its column label. Group the row in a thead as well.