Table Structure and When to Use Tables
What a table is for, the elements that build one (table, tr, th, td, thead, tbody, tfoot, caption), and why tables are for data, not page layout.
Table Structure and When to Use Tables
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Use a table when the information has two dimensions, where each value is meaningfully read against a row label and a column label. A schedule, a price list or a results grid qualifies; a list of links or a page layout does not.
- The minimum is `<table>`, `<tr>` for rows, and `<td>` or `<th>` for cells. `<th>` marks a header cell, which is what lets software tell a label from a value.
- `<thead>`, `<tbody>` and `<tfoot>` group rows into a header, body and footer. The parser adds a `<tbody>` for you if you leave it out, and `<tfoot>` renders at the bottom wherever it sits in the source.
- `<caption>` is the table's title and must be its first child. It gives the table an accessible name, so a screen reader user can decide whether to enter it.
- Never build page layout out of tables. Layout is the job of CSS grid and flexbox, and a layout table makes assistive technology announce rows and columns that mean nothing.
The problem tables solve
Most content is one-dimensional: a paragraph, a list, a sequence of steps. Some information is a grid. A train timetable has a row per station and a column per departure. A pricing page has a row per feature and a column per plan. The meaning of a cell comes from where it sits: “£12” is only useful once you know which plan’s column and which feature’s row it belongs to.
A table is HTML’s way of saying that out loud. You give the browser the grid, and it can lay it out, and software can use the row and column relationship. A screen reader can announce “Pro plan, Storage, 100 gigabytes” because the markup says which cell sits under which header.
Here is a quick test for whether you need a table: could this live in a spreadsheet and still make sense? If yes, it is a table. If you would never put it in a spreadsheet, it is not.
The pieces
A table is built from a small set of elements:
<table>
<caption>Opening hours</caption>
<thead>
<tr><th>Day</th><th>Opens</th><th>Closes</th></tr>
</thead>
<tbody>
<tr><td>Monday</td><td>09:00</td><td>17:00</td></tr>
<tr><td>Tuesday</td><td>09:00</td><td>17:00</td></tr>
</tbody>
</table>
<tr> is a row. Inside a row, <td> is a data cell and <th> is a header cell. Visually, browsers make <th> bold and centered by default, but the real difference is meaning: a <th> says “I am a label for other cells”. Accessible tables builds on that with scope and header associations.
<thead>, <tbody> and <tfoot> group rows. They matter for three reasons. They tell assistive technology which rows are the header. They give CSS a handle, like tbody tr:nth-child(even). And when a table prints across pages, browsers can repeat the header on each page.
What the parser does with your table
Tables are one of the places where the HTML parser edits your markup. In a test, <table><tr><td>a</td></tr></table>, with no <tbody>, produced a DOM with one <tbody> holding the row. table.tBodies.length was 1 in headless Chromium, Firefox and WebKit. If you write CSS or JavaScript that assumes table > tr, it will not match, because the row’s real parent is the inserted <tbody>. Writing the <tbody> yourself avoids the surprise.
Order is also looser than the source suggests. In another test, a table whose source order was <thead>, <tfoot>, <tbody> rendered the footer last in all three browsers, and table.rows listed the header row, body rows and then the footer row. The footer’s place is set by the element, so you can write it before the body if you like, though most people write the more natural top-to-bottom order.
The caption
<caption> is the table’s title. It has to be the first child of <table>. A test of the accessibility tree in Chromium and Firefox showed table "Q3 sales" for a table with a caption, so the caption becomes the table’s accessible name. A bold paragraph above the table does none of that. Prefer the caption, and use CSS (caption-side) if you want it below.
Tables are not for layout
Before CSS layout worked, people built whole pages out of nested tables, with invisible spacer images holding the columns apart. It worked, and it left a mess: the markup said “table” where the content was a header, a sidebar and an article.
Using a table for layout still costs you. A screen reader announces a table with rows and columns, which means nothing for a page layout. The structure is rigid and awkward on narrow screens. Use CSS grid and flexbox for layout. If you inherit a layout table you cannot rewrite, role="presentation" removes the table semantics. In a test, a table with that role showed up in the accessibility tree as plain text, not a table.
Changing a table’s display in CSS is a related worry. Years ago, doing so could strip table semantics in some browsers. In a test on display: block, all three browsers still reported a table. Treat that as encouraging, not a promise for every screen reader, and test with one if you rely on it.
Wide tables on small screens
Tables do not shrink the way paragraphs do. A table with many columns will overflow a phone. The common fix is to wrap it in a container with overflow-x: auto, so the table scrolls inside its box and the page does not. See overflow and scroll containers. The video’s runner-up goes further, with CSS that reshapes each row into a stacked card.
What the video covers
The embed is a clip, 3:14 to 9:31, from Learndev’s “HTML Lists & Tables Tutorial” (April 2024), part of a beginners series. The first three minutes cover ordered, unordered and description lists, which have their own lesson, so the clip starts where tables begin. According to its chapter list, it covers HTML tables, table borders, the header, body and footer groups, table captions and column groups. I could not read a transcript, so I do not describe what is shown. The table-border chapter is likely to use the old border attribute or basic CSS, so style with CSS in your own work. The parser behavior, tfoot placement, accessibility-tree results and layout caveats above are mine and were checked in a browser.
Where this fits
This opens the tables group. Accessible tables covers how to make headers and captions work for screen readers, and complex tables covers merged cells and column groups. If a table is going into an email, the constraints in HTML email and legacy constraints change the advice above, because email clients still rely on tables for layout.
Check yourself
You have a row of five product cards, each with an image, a name and a price. Is this a table?
No. Each card is a self-contained unit, not a value read against a row label and a column label. Use a list of articles or plain containers and arrange them with CSS grid or flexbox.
You write `<table><tr><td>a</td></tr></table>` with no `<tbody>`. What does the DOM contain?
A `<tbody>` wrapped around the row. The HTML parser inserts it, so `table.tBodies.length` is 1 even though you never wrote one. This is why a CSS selector like `table > tr` does not match in the browser.
If `<tfoot>` comes before `<tbody>` in the source, where does it appear on screen?
At the bottom, below the body rows. The footer's position comes from the element, not from its place in the source. `table.rows` also lists header rows, body rows, then footer rows, whatever the source order.
Why is a `<caption>` better than a bold paragraph above the table?
The caption is part of the table, so the browser exposes it as the table's name and it moves with the table. A paragraph is an unrelated piece of text that assistive technology does not connect to the table.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.