Sub topic 2
Rows and cells: table, tr, and td
This chapter assumes you can write a basic HTML document, add elements with attributes, and read simple CSS. It covers the three elements every table needs: table, tr, and td. Header cells, captions, and row groups come in later chapters.
The working model
An HTML table is a grid built row by row. You never declare columns directly. Instead, you write rows, and the browser builds columns from the cells inside them.
tableis the container for the whole grid.tr(table row) is one horizontal row.td(table data cell) is one cell holding data inside a row.
Nesting matters. A td must sit inside a tr, and a tr must sit inside a table (or inside a row group, covered later). Text placed directly inside table or tr is not valid.
<table>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
<tr>
<td>Food</td>
<td>300</td>
</tr>
</table>This produces two rows and two columns. The first td in each row forms column one. The second td forms column two.
What a cell can contain
A td holds flow content, so you can put text, links, images, lists, and even another table inside it. Use this freely, but keep each cell short. Long paragraphs in cells are hard to scan.
<table>
<tr>
<td>Docs</td>
<td><a href="https://developer.mozilla.org/">MDN</a></td>
</tr>
<tr>
<td>Tools</td>
<td>
<ul>
<li>Editor</li>
<li>Browser</li>
</ul>
</td>
</tr>
</table>How the browser decides the column count
The browser counts the cells in every row and uses the largest count as the number of columns. Each cell takes the next free position in its row, from left to right.
When rows have different counts, the shorter rows leave empty space at the right edge. The table still renders, but it looks ragged.
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
</tr>
</table>Here the table has three columns. The second row has no third cell, so its right side is blank.
If a cell has no data, write an empty td instead of leaving it out. An empty td keeps later cells in the correct column.
<tr>
<td>Ana</td>
<td></td>
<td>85</td>
</tr>If you leave the empty cell out, 85 moves into column two. That puts the value under the wrong heading.
The implied tbody
You may notice a tbody element in your browser's developer tools that you never wrote. When rows sit directly inside table, the HTML parser wraps them in a tbody for you. This matters when you write CSS or JavaScript. A selector like table > tr matches nothing in the parsed page, because the rows are children of the generated tbody. Use table tr instead, or write the tbody yourself. Chapter 5 covers row groups.
Adding a border to see the grid
By default, a table has no visible lines. To see the structure while you learn, add a temporary CSS rule.
<style>
table, td {
border: 1px solid #333;
}
</style>
<table>
<tr>
<td>North</td>
<td>120</td>
</tr>
<tr>
<td>South</td>
<td>95</td>
</tr>
</table>You will see double lines between cells, because each cell draws its own border and the table draws one too. A later chapter on styling shows how border-collapse merges them.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| `td` placed directly in `table` | Browser inserts a row, or the cell is moved | Wrap cells in a `tr` |
| Rows with different numbers of cells | Ragged right edge, misaligned data | Add empty `td` elements |
| Using `table > tr` in CSS | Selector matches nothing | Use `table tr` or `tbody > tr` |
| Closing tags left out | Browsers may guess, but results vary between tools | Always write `</td>` and `</tr>` |
| Using spaces or `<br>` to line up columns | Layout breaks when text size changes | Use real cells |
Try it
Build a table for a weekly schedule. Use three rows and three columns. Make one cell deliberately empty. Then add the temporary border rule and confirm the empty cell still holds its place.
Exercise
A table has two rows. The first row has three td elements. The second row has two td elements, and the missing cell is the middle one. What goes wrong if you simply delete the middle td, and how do you fix it?
Show answer
The cell that was in column three moves into column two, so its value appears under the wrong column, and column three is blank for that row. Fix it by keeping an empty td in the middle position, so the row still has three cells.
Exercise
You write the CSS selector table > tr { background: #eee; }, but no rows change color. Why, and what are two ways to fix it?
Show answer
The parser wraps rows in an implied tbody, so the rows are not direct children of table. Fix it by using the descendant selector table tr, or by selecting tbody > tr (or writing the tbody in your HTML).