HTML Table Tutorial: Tags, Examples and Nested Tables
November 11, 2026 · 10 min read
HTML Table Tutorial: Tags, Code Examples and Nested Tables
An HTML table is a grid built from four core tags: <table> wraps it, <tr> makes each row, <th> holds a header cell and <td> holds a data cell. This tutorial explains each tag with copyable code, a build-it-yourself exercise, a table nested inside a cell and a live editor to test it.
HTML Table Tags at a Glance
Each element has one fixed job, and only three of them are strictly needed for a working grid. The rest add structure, labels and accessibility.
| Tag | What it does | Needed? |
|---|---|---|
<table> | Wraps the whole table | Yes |
<caption> | Gives the table a visible title | Advised |
<colgroup> and <col> | Group columns so you can target them | Optional |
<thead> | Groups the header rows | Optional |
<tbody> | Groups the body rows | Optional (the browser adds one) |
<tfoot> | Groups summary rows, such as totals | Optional |
<tr> | One table row | Yes |
<th> | A header cell that labels a row or column | Advised |
<td> | A data cell | Yes |
The names are short for what they hold: td means table data and th means table header. Older code also puts border, cellpadding, cellspacing and align attributes on the table itself. MDN lists those as deprecated, so set borders, spacing and alignment with CSS instead. Our guide to table borders in HTML has the CSS for lines, spacing and colors.
How Do You Make a Table in HTML?
Open a <table>, add one <tr> per row, and fill each row with cells. The browser places cells left to right and rows top to bottom, so the order of your code is the order on screen. Here's the smallest useful HTML table code, with a header row and two data rows:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
<tr>
<td>Asha</td>
<td>Designer</td>
</tr>
<tr>
<td>Ben</td>
<td>Developer</td>
</tr>
</table>
That renders as a two-column grid with three rows. The top row shows "Name" and "Role" in bold, centred text, because that's how browsers style <th> by default. The two rows underneath hold plain data cells.
Give every row the same number of cells. The browser won't reject a short row, but it leaves a hole at the end, and the grid looks ragged. When one cell should cover two columns or two rows, you stretch it with colspan or rowspan rather than dropping cells, which is a topic of its own. Our guide to merging table cells covers both attributes.
A Complete HTML Table Example with caption, thead, tbody and tfoot
The basic version works, but most real tables need more structure. The example below uses every grouping element in the order the HTML Living Standard sets: <caption> first, then any <colgroup>, then <thead>, <tbody> and <tfoot> last.
<table>
<caption>Books read in 2026, by quarter</caption>
<thead>
<tr>
<th scope="col">Quarter</th>
<th scope="col">Fiction</th>
<th scope="col">Non-fiction</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Q1</th>
<td>4</td>
<td>2</td>
</tr>
<tr>
<th scope="row">Q2</th>
<td>3</td>
<td>5</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">Total</th>
<td>7</td>
<td>7</td>
</tr>
</tfoot>
</table>
Here's what each part of this sample HTML table contributes:
<caption>shows "Books read in 2026, by quarter" above the grid. It has to be the first child of<table>, and it tells every reader, including screen reader users, what the table is about.<thead>holds the column headings.scope="col"marks each one as the label for the cells below it.<tbody>holds the data rows. The first cell of each row is a<th scope="row">, so "Q1" labels the numbers to its right.<tfoot>holds the totals row, which renders at the bottom of the table.
You can leave out <tbody>, but you'll get one anyway. MDN's table reference notes that rows placed straight inside <table> get wrapped in an implicit <tbody>. That trips up CSS: a selector like table > tr matches nothing, because each row's parent is now the <tbody>.
Table Data vs Header Cells: When to Use th and scope
Use <td> for values and <th> for labels. If a cell describes other cells, it's a header, even when it sits in the first column instead of the top row. The scope attribute says which cells a header covers, and the spec defines four values:
colcovers the cells below it in its column.rowcovers the rest of its row.colgroupcovers the rest of a column group set up with<colgroup>.rowgroupcovers the rest of the<thead>,<tbody>or<tfoot>it sits in.
Leave scope out and the browser works out the direction itself, which the spec calls the auto state. That guess is usually right for a simple grid, but writing scope removes the doubt. The W3C WAI tables tutorial covers harder cases, such as headers that need the headers attribute.
A comparison grid shows row headers doing real work:
<table>
<caption>Trail comparison</caption>
<tr>
<td></td>
<th scope="col">Ridge Loop</th>
<th scope="col">Lake Path</th>
</tr>
<tr>
<th scope="row">Distance</th>
<td>8 km</td>
<td>5 km</td>
</tr>
<tr>
<th scope="row">Climb</th>
<td>420 m</td>
<td>90 m</td>
</tr>
</table>
The columns name the trails and each row names a property, so a reader always knows what "420 m" refers to. The empty corner is a <td> because it labels nothing.
Worked Exercise: Write the Code to Produce This HTML Table
Homework often shows a finished table and asks you to write the markup. Here's a typical target, a class timetable with the caption "Class timetable":
| Day | 9:00 | 11:00 |
|---|---|---|
| Monday | Maths | History |
| Tuesday | Science | Art |
| Friday | English | Music |
Work through it in four steps:
- Count the columns. There are three, so every row needs exactly three cells.
- Write the header row. It labels columns, so it gets three
<th scope="col">cells inside a<thead>. - Add the data rows. Each day labels its row, so the first cell is a
<th scope="row">and the two lessons are<td>cells. - Add the caption straight after the opening
<table>tag.
<table>
<caption>Class timetable</caption>
<thead>
<tr>
<th scope="col">Day</th>
<th scope="col">9:00</th>
<th scope="col">11:00</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Monday</th>
<td>Maths</td>
<td>History</td>
</tr>
<tr>
<th scope="row">Tuesday</th>
<td>Science</td>
<td>Art</td>
</tr>
<tr>
<th scope="row">Friday</th>
<td>English</td>
<td>Music</td>
</tr>
</tbody>
</table>
Before you hand it in, check four things. Every <tr> has three cells. Every opening tag has a matching closing tag. The <caption> is the first thing inside <table>. Labels use <th>, and values use <td>.
Can You Put a Table Inside a Table in HTML?
Yes. The spec defines what a <td> may contain as flow content, and a <table> counts as flow content, so a complete table can sit inside a cell. You open the inner <table> inside the <td> and close it before the outer </td>.
<table>
<caption>Orders this week</caption>
<thead>
<tr>
<th scope="col">Order</th>
<th scope="col">Customer</th>
<th scope="col">Items</th>
</tr>
</thead>
<tbody>
<tr>
<td>1042</td>
<td>Rivera</td>
<td>
<table>
<tr><th scope="col">Product</th><th scope="col">Qty</th></tr>
<tr><td>Notebook</td><td>2</td></tr>
<tr><td>Pen set</td><td>1</td></tr>
</table>
</td>
</tr>
</tbody>
</table>
The outer table shows one order per row. Its Items cell holds a small two-column table listing each product and its quantity. One spot where a table can't go is inside a <caption>, since the spec rules out table descendants there.
Nesting fits data where each row owns a set of sub-rows, like line items under an order. It has a real cost, though. The inner table sizes its own columns, so nested tables in different rows usually don't line up with each other. Each one is also a separate table, which adds extra navigation for screen reader users. For two or three short values, a list in the cell is often clearer.
Never nest tables to lay out a page. The spec says plainly that tables must not be used as layout aids, so build page layouts with CSS grid or flexbox.
Try the HTML Table Code in the Editor
The preview in our editor renders raw HTML inside Markdown, so you can paste any example above and see the table straight away. Scripts and event handlers are stripped, but table markup comes through.
The demo below holds a captioned table with a second table nested in one cell. It sticks to two columns and short words, because the preview pane is narrow and a nested table gets squeezed quickly. Change a cell, add a row, or delete the inner table and watch the preview update. Drag the divider if you want more room.
One gotcha applies whenever you mix HTML with Markdown. Under the CommonMark rules for HTML blocks, a block that starts with <table> ends at the first blank line. Anything after that is read as Markdown again, so a line indented by four spaces turns into a code block. Keep the whole table free of empty lines and it stays one block.
HTML Table or Markdown Table: Which Should You Write?
In a Markdown file, a pipe table is quicker to type and easier to read in source. Its limits show up fast, though. The GFM table syntax has no caption, no row headers, no merged cells and no nesting. GFM does allow raw HTML, so you can drop an HTML table into a README when you need those. The Markdown table guide covers the pipe syntax.
We prefer a Markdown table for anything a pipe table can express, and we switch to HTML only for captions, grouped headers or nesting. To skip the typing, the HTML table generator builds the markup from a grid you edit in your browser, with HTML escaping and output options. It doesn't output merged cells or nested tables, so you add those by hand. Converting a whole document? See how to convert Markdown to HTML.
An HTML table comes down to a short list of tags: rows in <tr>, labels in <th>, values in <td>, with <caption>, <thead>, <tbody> and <tfoot> adding structure. Paste the timetable exercise or the nested example into the editor to check it renders the way you expect. Then swap in your own data and keep each row at the same cell count.