On the wall of a food stall there is usually a price list: a Menu column, a Price column, and one row for each dish. That price list is a table: data arranged in rows and columns. A class timetable is a table too.
In HTML, a table is built from the inside out. Cells (the boxes inside a table) are grouped into a row, and the rows are grouped inside a <table>. You never write the columns themselves. They come from the order of the cells in each row.
A table is written row by row
<table>
<thead>
<tr>
<th scope="col">Menu</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fried Rice</td>
<td>$3</td>
</tr>
<tr>
<td>Sweet Iced Tea</td>
<td>$1</td>
</tr>
</tbody>
</table>
Start with the four main tags. <thead>, <tbody> and scope come after this:
<table>wraps the whole table.<tr>(table row) is one row. HTML has no tag for columns. You write one row after another, and the columns come from the order of the cells in each row. The first cell of every row forms the first column, and the second cell forms the second column.<th>(table header) is a heading cell. The browser shows it bold and centred.<td>(table data) is a normal data cell.
Every <th> and <td> must be inside a <tr>. Every row should have the same number of cells, so the columns stay aligned. The result of the code above: a table with two columns, Menu and Price, and two menu rows below them.
Separate headings from content: <thead> and <tbody>
The code above also uses two wrappers:
<thead>wraps the heading row.<tbody>wraps the data rows.
Inside both, there are still <tr> rows with their cells. So the wrapping order, from the outside to the inside, is: <table>, then <thead> or <tbody>, then <tr>, then <th> or <td>.
With or without <thead> and <tbody>, the table looks the same. But browsers, screen readers (programs that read pages aloud for blind users) and search engines can now recognise which part is the heading and which is the content. When a long table is printed over several pages, the browser can repeat the <thead> row at the top of each page.
scope: a column heading or a row heading
<th scope="col">means this cell is the heading for the column below it.<th scope="row">means this cell is the heading for the row beside it, for example the name of the day at the start of each row in a timetable.
With scope, a screen reader can read the heading together with each cell, for example "Price, $3". The title of the whole table goes in <caption>, right after <table>.
Here is an order list with a <caption> and a total row:
<table>
<caption>Table 3 Order</caption>
<tr><th scope="col">Menu</th><th scope="col">Qty</th><th scope="col">Price</th></tr>
<tr><td>Fried Rice</td><td>2</td><td>$6</td></tr>
<tr><th scope="row" colspan="2">Total</th><td>$6</td></tr>
</table>
The title Table 3 Order shows above the table. colspan="2" makes the Total cell two columns wide, Menu and Qty. That is why the total row has only two cells, not three. For a cell that is several rows tall, use rowspan.
⚠️ Columns with gaps, fake headings, and tables used for layout
- Cells written straight into
<table>. A<td>without a<tr>makes the browser build the rows itself. The result is often different from what you intended. - Rows with different numbers of cells. The columns then have holes or are shifted.
- Capital letters in cell text. If an exercise asks for
table, a cell that containsTablecounts as different. - A heading row made of
<td>. The text is not bold, and screen readers don't read it as a heading. Use<th>. - A table for page layout.
<table>is only for data that really is a table. Arranging the parts of a page is the job of CSS, with features called Grid or Flexbox.
💡 Wide tables on a phone screen. Wrap the table in
<div style="overflow-x: auto">. Thestyleattribute holds CSS rules for that tag. With this rule, the table can be scrolled sideways inside that<div>, while the page itself stays in place.
TL;DR: You write a table row by row: <table> wraps everything, <tr> is one row, <th> is a heading cell, and <td> is a data cell. The heading row goes in <thead>, and the data rows go in <tbody>. scope="col" marks a column heading, and scope="row" marks a row heading. In the exercises, type cell text exactly as asked, including capital letters.
⚡ Row after row
Write:
<table>
<tr>
<th>Menu</th>
<th>Price</th>
</tr>
<tr>
<td>Fried Rice</td>
<td>$3</td>
</tr>
</table>
Guess first: which tag makes the columns?
None. Columns come from the order of the cells (the boxes inside a table) in each row: the first cell forms the first column, the second cell forms the second column.
Meaning: <tr> is a row, <th> is a heading cell (bold, centred), <td> is a data cell. Every row should have the same number of cells, so the columns stay aligned.
⚡ Separate headings from content
Write:
<table>
<thead>
<tr>
<th scope="col">Menu</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fried Rice</td>
<td>$3</td>
</tr>
</tbody>
</table>
Meaning: <thead> wraps the heading row, and <tbody> wraps the data rows. The table looks the same as without them, but browsers and screen readers can now recognise the heading row. When a long table is printed, the browser can repeat the <thead> row at the top of each page.
⚡ A column heading or a row heading
Meaning: scope="col" marks a column heading, and scope="row" marks a row heading. With scope, a screen reader reads the heading with each cell: "Price, $3".
In the exercises, type cell text exactly as asked, including capital letters: table and Table count as different.