Sub topic 1
When a Table Is the Right Choice
HTML has a table element, and the first decision you make with it is whether to use it at all. Tables are easy to misuse because a grid of rows and columns looks like a convenient way to line things up on a page. This chapter gives you a test for when a table is correct, shows what to use instead when it is not, and covers the edge cases where the choice is unclear.
Prerequisites
This chapter assumes you already know the following:
- Basic HTML document structure:
html,head,body, and how elements nest. - How to write elements and attributes, such as
<a href="...">. - Basic CSS selectors and properties, enough to read a rule like
ul { margin: 0; }.
You do not need to know any table elements yet. The next chapter introduces them one by one.
What makes data tabular
A table is a structure for data whose meaning depends on two things at once: the row it sits in and the column it sits in. Each cell is the answer to a question formed by combining a row label and a column label.
Look at this data:
| Item | Jan | Feb |
|---|---|---|
| Rent | 900 | 900 |
| Food | 300 | 340 |
The cell containing 340 means nothing alone. It means "food spending in February". Remove the row label and the column label and the number carries no information. That dependence on both axes is what makes data tabular.
The two-axis test
Ask these questions about your content:
- Can I name what each row represents? (Rent, Food, a person, a product.)
- Can I name what each column represents? (A month, a score, a price.)
- Does every cell answer "what is the value of this column for this row"?
If all three answers are yes, use a table. If you can only name one axis, you probably have a list. If the "columns" exist only because you want things side by side, you have a layout problem, and CSS should handle it.
Typical tabular content
| Content | Rows are | Columns are |
|---|---|---|
| Price list | Products | Size, price, stock |
| Timetable | Time slots | Days of the week |
| Sports results | Teams | Played, won, lost, points |
| Feature comparison | Features | Plans or products |
| Calendar month | Weeks | Days of the week |
Each of these has a header on both axes, or at least a header on one axis and a repeated kind of value on the other. Spreadsheets, statistics, schedules, and comparison matrices are the common cases.
Why tables were used for layout, and why that stopped
In the 1990s, CSS could not position elements reliably. Developers used tables with invisible borders to build page layouts: a table cell for the header, one for the sidebar, one for the main content. This worked visually but caused lasting problems:
- Screen readers announce a table as data, such as "table with 3 rows and 2 columns". A layout table makes a reader announce structure that does not exist.
- The reading order follows the source order of rows and cells, so you cannot reorder content for different screen sizes without rewriting the markup.
- The markup mixes content with presentation, so a redesign means editing every page.
- Layout tables adapt poorly to narrow screens, because cells keep their grid.
CSS Flexbox and Grid now handle layout directly. Using a table for layout is an error today. If you maintain old pages that do so, treat replacing them as a separate cleanup task.
A decision flow
The diagram below summarizes the choice. Follow it from the top whenever you are unsure.
The same content in different structures
Choosing a structure changes what the browser and assistive technology know about your content. Here are three small examples.
A table: two axes
<table>
<tr>
<th>Item</th>
<th>Jan</th>
<th>Feb</th>
</tr>
<tr>
<th>Rent</th>
<td>900</td>
<td>900</td>
</tr>
<tr>
<th>Food</th>
<td>300</td>
<td>340</td>
</tr>
</table>You do not need to understand every element yet. The point is that the markup states the relationships: each td belongs to a row and a column.
A list: one axis
A list of the steps in a recipe has an order and nothing else. A table with one column would add noise without adding meaning.
<ol>
<li>Boil the water.</li>
<li>Add the pasta.</li>
<li>Drain after ten minutes.</li>
</ol>A description list: name and value pairs
A set of facts about one thing, such as an author's details, pairs a name with a value. There is no second axis, because the "columns" are not a repeated kind of value across several rows. A description list (dl) expresses this directly.
<dl>
<dt>Name</dt>
<dd>Ana Silva</dd>
<dt>Role</dt>
<dd>Editor</dd>
</dl>CSS Grid: arrangement only
If you want three product cards side by side, the cards are not rows and columns of data. They are independent items arranged visually. Use a list for the items and Grid for the arrangement.
<style>
.cards {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
list-style: none;
padding: 0;
}
</style>
<ul class="cards">
<li>Card one</li>
<li>Card two</li>
<li>Card three</li>
</ul>On a narrow screen you can change grid-template-columns in a media query. A table cannot reflow this way, because its grid is part of its meaning.
Choosing between structures
| Your content | Best element | Why |
|---|---|---|
| Values that need a row label and a column label | `table` | Both axes carry meaning |
| Items in a sequence or set | `ul` or `ol` | One axis only |
| Name and value pairs about one subject | `dl` | Pairs, no repeated columns |
| Cards, sidebars, page regions | CSS Grid or Flexbox | Arrangement, not data |
| Form labels and inputs | `label` with CSS layout | Not data; see the edge cases |
Edge cases
The two-axis test is clear for most content. These cases need more thought.
Two-column data that looks like a table
A list of terms and definitions has two columns visually. Whether it is a table depends on whether the rows repeat the same kind of record. A glossary is a set of name and value pairs, so dl fits. A table of countries with a "capital" column and a "population" column has several columns that describe each row, so a table fits.
A useful rule: if adding a third column would make sense, you probably have a table. If a third column would be meaningless, you have pairs.
Forms
Labels on the left and inputs on the right can look like a table. They are not data. Use label elements associated with their inputs and lay them out with CSS. A table here makes screen readers announce data structure for a form and gives you no benefit.
Calendars
A month view is a legitimate table. Rows are weeks and columns are days of the week. Each cell is the answer to "what is on this day". The headers are the day names.
Single-row or single-column data
One row of values has only one axis. A list is usually better. Exceptions exist, such as a summary figure with a labeled header cell, but be able to say what the second axis is.
Sparse data
A comparison matrix with many empty cells is still a table when the empty cells are meaningful, for example "this plan does not include this feature". If most cells are empty and the content is really a few items, a list is clearer.
Tables of images or layout blocks
A gallery of photos is not tabular. The photos do not depend on a row label and a column label. Use a list with Grid.
Wide tables on phones
A table with many columns is still a table. Narrow screens are a styling problem, not a reason to switch to another element. A later chapter covers scrolling wrappers and other fixes.
Nested tables
A table inside a table cell is almost always a sign of a layout table. A genuine nested table is rare. Check first whether a second table, or a multi-level header, expresses the data better.
Common errors
- Using a table to line up text. If you add columns only to control spacing, switch to CSS.
- Using a list for real two-axis data. A list of strings such as "Rent, Jan: 900" hides the structure. A screen reader user cannot jump between columns.
- Choosing by appearance. Pick the element by meaning and then style it. A
dlcan be styled to look like a two-column grid, and a table can be styled to look like a list. Meaning stays in the markup. - Splitting one dataset into several tables to fit the page. Keep related data in one table and handle width with CSS.
- Using
display: tableondivelements for layout. This copies the old problem into CSS. It is valid CSS, but it does not give you real table semantics and does not need to.
What comes next
You now have a test for deciding whether a table is the right element. The next chapter starts building one, beginning with table, tr, and td, and then moves to header cells, captions, and row groups.
Exercise
For each case, say whether you would use a table, a list, a description list, or CSS layout, and give a one-sentence reason. (a) A bus timetable with stops down the side and departure times across the top. (b) The ingredients for a recipe. (c) A contact card with name, phone number, and email for one person. (d) Three feature boxes shown side by side on a landing page.
Show answer
(a) Table, because each time depends on both a stop (row) and a departure (column). (b) List, because ingredients form a single set with one axis. (c) Description list, because the content is name and value pairs about one subject. (d) CSS layout (a list arranged with Grid or Flexbox), because the boxes are independent items and the side-by-side placement is visual arrangement, not data.
Exercise
A colleague builds a two-column page with a sidebar on the left and an article on the right, using a table with one row and two cells. Name two problems with this and the replacement approach.
Show answer
Problems: screen readers announce it as a data table with rows and columns, and the layout cannot be rearranged or collapsed to one column on small screens without changing the markup. Replacement: use semantic elements such as aside and main (or article) and arrange them with CSS Grid or Flexbox, using a media query to stack them on narrow screens.
Exercise
A glossary lists terms and a one-sentence definition for each. Would you mark it up as a table or a description list, and what question would change your answer?
Show answer
A description list, because each entry is a name and value pair with no repeated kind of column. The answer would change if each term also had several attributes, such as part of speech, origin, and first year of use. Then each term would be a row described by several columns, and a table would fit.