Sub topic 1
When a table is the right choice
Before you write any table markup, decide whether a table is the right tool. This chapter gives you a way to make that decision, and it shows what goes wrong when you choose badly.
Prerequisites
This chapter assumes you can:
- Write a basic HTML document with
html,head, andbodyelements. - Write elements and attributes, such as
<p class="note">. - Read simple CSS selectors and properties, such as
td { padding: 1rem; }.
You do not need to know any table elements yet. The next chapter introduces them.
What makes data tabular
A table presents data that has meaning in two directions at once. Each value is described by its row and by its column. If you can't name both, you probably don't have a table.
Consider monthly expenses. The value 900 means something only when you know the row (Rent) and the column (Cost). Remove either label and the number is ambiguous. That two-way relationship is the signal to use a table.
Compare a list of three blog post titles. Each title stands on its own and has no second dimension. A list fits better.
A quick test
Ask these questions about your content:
- Does each value need both a row label and a column label to make sense?
- Would a reader want to scan down a column and compare values?
- Would a spreadsheet be a natural home for this data?
If you answer yes to most of them, use a table. If you answer no, look at the alternatives below.
Choosing the right alternative
Each common layout need has an element or technique built for it.
| What you are building | Better choice | Why |
|---|---|---|
| A set of related items in one dimension | `ul` or `ol` | Screen readers announce the list and its length. |
| Terms and their descriptions | `dl` | Each term pairs with one description. |
| Page columns, cards, or a navigation bar | CSS flexbox or grid | These handle spacing and wrapping that tables cannot. |
| A form with labels beside inputs | `label` and `input` with CSS layout | Labels stay linked to their controls. |
| Prices by product and by month | `table` | Every value has a row and a column meaning. |
Why layout tables cause problems
Early web pages used tables to position content, such as a sidebar beside a main column. Avoid this today. Tables used for layout cause several problems:
- Screen readers announce the structure as data, for example "table with 3 columns and 2 rows". This misleads people who rely on them.
- The source order is tied to the visual grid, so rearranging a page for small screens is hard.
- The markup grows large and is difficult to maintain.
CSS grid and flexbox solve layout problems directly and keep the HTML meaningful.
A grid that looks like a table but isn't
Some content looks like rows and columns but isn't tabular. A photo gallery is the usual example. The images sit in a grid, but no image is described by a row heading and a column heading. Use CSS grid for a gallery. The same applies to a row of pricing cards. Each card is a self-contained unit.
The reverse also happens. A pricing comparison with plans down the side and features across the top is tabular. Every cell answers the question "does this plan include this feature?". A table is correct here.
Tabular data in a table
This is a small example that passes the test. Each number has a row (item) and a column (cost). Later chapters explain each element.
<table>
<tr>
<th>Item</th>
<th>Cost</th>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
<tr>
<td>Food</td>
<td>300</td>
</tr>
</table>What you build in the next chapters
Once you decide to use a table, the rest of the tutorial applies. You will build rows and cells, add header cells and a caption, group rows, merge cells, and make the table work with screen readers. Finally, you will style it and handle wide tables on small screens. Every one of those steps assumes the decision made in this chapter.
Exercise
A page shows a row of three product cards. Each card has an image, a title, and a price. Should you use a table? Explain your answer with the two-direction test.
Show answer
No. Each card is a self-contained unit, and no value depends on both a row label and a column label. The cards are a one-dimensional set of items, so use a list with CSS flexbox or grid for the arrangement. A table would add meaning that doesn't exist and would mislead screen reader users.
Exercise
You have quarterly sales figures for four regions, with regions down the side and quarters across the top. Which structure fits, and why?
Show answer
A table. Each sales figure needs both a region (row) and a quarter (column) to make sense, and readers will compare values down a column or across a row.