Sub topic 2
Rows and cells: table, tr, and td
This chapter assumes you can write a basic HTML document, add elements and attributes, and read simple CSS rules. It covers the three elements every table is built from, how browsers turn them into a grid, and the errors that come from misplacing them. Header cells, captions, row groups, and spanning have their own chapters. This chapter only mentions them where the basic elements depend on them.
The three core elements
A table is built from three elements nested in a fixed order.
tableis the container. It tells the browser that its contents form a grid of data.tris a table row. It holds one horizontal line of cells.tdis a table data cell. It holds one piece of content at the crossing of a row and a column.
HTML has no element for a column. A column is not written anywhere in the markup. It exists because cells line up vertically. The first td in every row belongs to column 1, the second to column 2, and so on.
<table>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
<tr>
<td>Food</td>
<td>300</td>
</tr>
<tr>
<td>Transport</td>
<td>80</td>
</tr>
</table>This markup makes a grid with three rows and two columns. By default the browser draws no borders, so the result looks like loosely aligned text. The structure is still there, and CSS or assistive technology can use it.
How the grid is formed
The browser builds the grid in two passes of reasoning.
- Each
trcreates a new row, from top to bottom in source order. - Each
tdinside that row is placed in the next free column slot, from left to right in source order.
The number of columns in the table is the width of its widest row. You never declare a column count. Ordinary rows with no spanning are easy to predict, because the Nth td in a row sits in column N.
The row and column positions come only from the order of the elements in the source. Whitespace and line breaks between the tags do not matter. These two tables produce the same grid:
<table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table><table>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</table>Prefer the second form. Putting one cell per line makes it easy to check that every row has the same number of cells.
What a td can contain
A td accepts flow content, which means most of what can appear inside a div. You can put text, links, images, lists, forms, and even another table inside a cell.
<table>
<tr>
<td>Tea</td>
<td>
<ul>
<li>Green</li>
<li>Black</li>
</ul>
</td>
<td><a href="https://example.com/tea">Buying guide</a></td>
</tr>
</table>A cell can hold block elements such as p and ul, so a long cell grows taller and the whole row grows with it. Every cell in a row shares the row's height. Every cell in a column shares the column's width. This sizing rule is why cells in a table always stay aligned, and it is also why one wide image or one long unbroken word can stretch a whole column.
A cell can also be empty. An empty td is valid and keeps its slot in the grid:
<table>
<tr>
<td>Mon</td>
<td>Gym</td>
<td>Read</td>
</tr>
<tr>
<td>Tue</td>
<td></td>
<td>Cook</td>
</tr>
</table>Do not delete the empty td. If you remove it, "Cook" moves left into column 2 and the row no longer lines up with the one above it.
The implicit tbody
Look at the parsed result of the first example. If you write tr elements directly inside table, the HTML parser inserts a tbody element between them. Your source has no tbody, but the document tree the browser builds does.
You can see this in a complete page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Implicit tbody</title>
</head>
<body>
<table id="expenses">
<tr>
<td>Rent</td>
<td>900</td>
</tr>
</table>
<script>
const table = document.getElementById("expenses");
console.log(table.children[0].tagName);
console.log(table.querySelectorAll(":scope > tr").length);
</script>
</body>
</html>Open the browser console. The first line prints TBODY. The second prints 0, because the tr is no longer a direct child of the table. This has two practical effects:
- A CSS selector such as
table > trnever matches in an HTML document, even though your source looks like it should. Usetable tr, or write thetbodyyourself and selecttbody > tr. - JavaScript that walks
table.childrenfinds thetbodyfirst. Thetable.rowsproperty avoids the problem, because it lists all rows no matter which group they sit in.
Optional end tags
The HTML specification lets you omit the end tags of tr and td in many cases. The following is valid HTML:
<table>
<tr><td>Rent<td>900
<tr><td>Food<td>300
</table>Parsers handle it, but you should still write every end tag. Omitted tags make a missing cell harder to spot, they confuse some editors and formatters, and they break when you move to XML-based tooling such as XHTML. This tutorial writes all end tags.
Rows of different lengths
Nothing forces the rows to have equal numbers of cells. If one row has fewer td elements, the browser leaves the remaining slots on the right empty:
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
</tr>
</table>The grid is three columns wide, and the second row stops after column 2. With no borders you may not notice the gap. With borders, the second row visibly has no third cell. The table is still valid HTML, but it is almost always a mistake. A screen reader user hears a row with fewer values than the others and has no way to know whether a value is missing on purpose.
When a value is genuinely missing, write an empty cell, or put a placeholder such as a dash or the text "n/a" in it. Choose one convention and use it across the table.
Which elements may go where
The content model of table is strict. Only certain elements may be direct children, and the parser repairs the tree when you break the rules.
| Element | Allowed as direct child of | Typical contents |
|---|---|---|
| `table` | Any flow content, including `td` | `caption`, `colgroup`, `thead`, `tbody`, `tfoot`, or `tr` |
| `tr` | `table`, `thead`, `tbody`, `tfoot` | `td` and `th` |
| `td` | `tr` | Flow content |
The most common repair is called foster parenting. If you put text, a div, or an input directly inside table or tr, the parser moves it out and places it before the table. The content then appears above the table, not inside it:
<table>
<tr>
<td>Rent</td>
This text is not inside a cell.
</tr>
</table>Here "This text is not inside a cell." is moved in front of the table in the rendered page. Wrapping the content in a td fixes it. The same applies to form elements. A form placed between table and tr is not allowed, so wrap the whole table in the form, or put the form inside a cell.
td versus th
A td says "this cell holds data." A th says "this cell labels other cells." Both go inside a tr, and both take part in the grid in the same way. If you mark a cell that is really a label as a td, the table still looks fine. But the label is then invisible to assistive technology, because nothing marks it as a heading for other cells. The next chapter covers th in full. For now, treat td as the right choice only for cells that are not labels.
Default appearance and a minimal stylesheet
Browsers give tables almost no default styling. The cell content is aligned to the top left in many cases, and cells have a small amount of internal spacing. To see the grid while you work, add a few rules:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Expenses</title>
<style>
table {
border-collapse: collapse;
}
td {
border: 1px solid #888;
padding: 0.4rem 0.6rem;
}
</style>
</head>
<body>
<table>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
<tr>
<td>Food</td>
<td>300</td>
</tr>
</table>
</body>
</html>Without border-collapse: collapse, each cell draws its own border with a gap between neighbours, which produces double lines. A later chapter explains the property in detail. Putting the border on td and not on table also means the border follows each cell, so empty cells keep a visible box. By default an empty cell with no border can lose its own border and background when the empty-cells property is set to hide, so check this if empty cells vanish from your styled table.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Text appears above the table | Content placed directly in `table` or `tr` | Move it into a `td` |
| Row looks shorter than others | Missing `td` | Add an empty `td` or placeholder |
| `table > tr` rule has no effect | Parser inserted a `tbody` | Use `table tr` or `tbody > tr` |
| Cells shift left in one row | An empty `td` was removed | Restore the empty cell |
| Table used for page layout | Wrong tool for the job | Use CSS grid or flexbox |
Exercise
A table has three rows. Rows 1 and 2 each contain three td elements. Row 3 contains two td elements. How many columns does the grid have, and what does the browser do with row 3?
Show answer
The grid has three columns, because the number of columns equals the widest row. Row 3 fills columns 1 and 2, and column 3 stays empty. The fix, if a value is missing, is to add an empty td or a placeholder to row 3.
Exercise
You write <table><tr><td>A</td></tr></table> and the CSS rule table > tr { background: yellow; }. The row does not turn yellow. Why, and what two selectors would work?
Show answer
The HTML parser inserts an implicit tbody between table and tr, so the tr is not a direct child of the table. The selectors table tr and tbody > tr would both match. Writing the tbody yourself and using tbody > tr makes the structure in the source match the structure in the browser.