Tables & data
5 min lesson
- An HTML table displays data in rows and columns, like a spreadsheet on a web page.
- Use tables for tabular data only, never for page layout.
- Think of a table as a grid of cells: each row is a line, each column is a field, and each cell holds one value.
- A table starts with <table>, rows use <tr>, header cells use <th> and data cells use <td>.
- A <caption> describes the table, and <thead>, <tbody> and <tfoot> group its parts.
- This builds on Figure, figcaption & image best practices: both lessons add structure around content.
- The example shows a small price table with a caption and a header row; you will see a grid with bold headings.
- Next, Forms & inputs lets the user send data, not just read it.
- Use tables for data that naturally fits rows and columns.
- Put column titles in th cells, not td, so tools know they are headers.
- Add a caption to every table; it explains what the data means.
- Never use a table to lay out a page; use CSS instead.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tables</title>
</head>
<body>
<!-- A table showing product prices -->
<table>
<!-- The caption explains what the table contains -->
<caption>Product prices</caption>
<!-- The header row: one th per column -->
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<!-- The body rows: one tr per record -->
<tbody>
<tr>
<td>Tea</td>
<td>120</td>
</tr>
<tr>
<td>Coffee</td>
<td>150</td>
</tr>
</tbody>
</table>
</body>
</html>What happens: A page showing a table titled "Product prices" with a header row "Product | Price", a row "Tea | 120" and a row "Coffee | 150".
Breaking it down
table
<table>...</table>Wraps the whole table. Without it, the browser treats rows as plain content.
caption
<caption>Product prices</caption>A title that belongs to the table. Screen readers announce it before reading the data.
tr
<tr>...</tr>One row of the table. Every cell in the row must sit inside the same tr.
th
<th>Price</th>A header cell. Browsers bold it, and assistive tools use it to describe the column.
td
<td>120</td>A regular data cell. Each td matches one column in its row.
Table tags
| Tag | Use for |
|---|---|
| table | The whole table |
| caption | A table title |
| thead | Header rows |
| tbody | Main data rows |
| tr | One row |
| th | A header cell |
| td | A data cell |
A helpful analogy
A table is like a page from a ledger book: rows are lines of entries, columns are the kinds of information, and each box holds one value. A caption is the heading written at the top of the page.
Nesting rules
- 1.th and td must sit inside tr, never directly inside table.
- 2.Keep thead before tbody, and tfoot after tbody, for a clean structure.
Common mistakes
Using a table for page layout
Avoid
<table><tr><td>Sidebar</td><td>Main</td></tr></table>Use instead
<div class="layout">...</div> with CSSTables are meant for data. Using them for layout confuses screen readers and breaks on small screens.
Putting column titles in td cells
Avoid
<td>Product</td>Use instead
<th>Product</th>th marks a header, so tools can announce 'Product: Tea'. td cells carry no such meaning.
Quick tips
- •Check the table with a screen reader; well-marked headers make each cell understandable.
- •If you need a border, use CSS; HTML attributes for borders are outdated.