← Teacher guide

Tables

Students build tables one row at a time with table, tr, th and td, and learn that every row needs the same number of cells.

Structure and style a page · Parts of a page · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 12 min · Modify 8 min · Mix it up 4 min · Make 10 min

Before class

Do the Make task yourself (about 5 minutes).

A table is built row by row: <tr> is a table row, <th> a heading cell and <td> an ordinary cell ("table data"). Columns are not written anywhere: they appear because every row has its cells in the same order. Students who think in columns, or in spreadsheets, often write all of one column first. Tables are for data; the look (lines, colours) comes from rules, and page layout is a later course.

Running the lesson

  1. Predict: the tempting wrong answer is 3 rows of 2 words, reading each <tr> as a column. Run it with Boxes on to see each row as a box going across.
  2. Investigate: the row-building task and Sam's table are the same idea from two sides: the cells of one house belong in one <tr>. The short row is the quiet failure: the browser shows it without complaint, so count cells per row.
  3. Modify: adding a column is the hard part. It needs a <th> in the heading row and a <td> in every other row.
  4. Mix it up: the row takes the class and the name's cell holds the link. Ask where each piece goes before they answer.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Building a table row by row with the same number of cells in each. Have them count the cells per row before they look at the page.

When students get stuck

What you will seeA question to askThe fix
One row is shorter than the others, or a value sits in the wrong column.Does every row have the same number of cells, in the same order?Add the missing <td> so each row has a cell for every column.
Text appears above the table instead of inside it.Is that text inside a <td> or <th>?All text in a table goes inside cells; a title goes above the <table> in an <h2>.
The new column only has a heading.Which rows have a cell for the new column?Add one <td> to every data row, in the same position.

Answers to the checks

  1. Predict: How will the six words be arranged on the page?
    2 rows of 3 words. Each <tr> is one row going across, and its three cells line up under the cells of the other row to make 3 columns.
  2. Build a row: Put the lines in order to make one row for the Kestrels: 7 points and 2 wins. One line is not needed.
    see the program below
    <tr> opens the row, the three cells go inside it in the same order as the headings, and </tr> closes it. </table> would end the whole table.
    <tr>
      <td>Kestrels</td>
      <td>7</td>
      <td>2</td>
    </tr>
    
  3. Sam's table: Sam wrote the names first, then the points, with one <tr> for each. Sam wanted two columns, House and Points. How many columns does the page have?
    1, because every <tr> holds one cell
    Every <tr> is a row with one cell, so the page has four rows and one column. To get House and Points side by side, each house needs its own <tr> with both cells in it.
  4. The short row: The Hawks have no points, and their row is shorter than the others. Click the line that causes it.
    line 4
    Line 4 has only one cell. Every row needs a cell for each column, so the Hawks row needs <td>9</td> after the name. The browser shows the short row without any error.
  5. Mix it up: The Hawks win next week. Which row gives them the gold row and makes their name a link to https://example.com/hawks?
    <tr class="winner"><td><a href="https://example.com/hawks">Hawks</a></td><td>9</td></tr>
    The class goes on the <tr> so the whole row takes the .winner rule, and the href goes on the <a> inside the name's cell.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Add a row

<h2>House points</h2>
<table>
  <tr>
    <th>House</th>
    <th>Points</th>
  </tr>
  <tr>
    <td>Owls</td>
    <td>12</td>
  </tr>
  <tr>
    <td>Hawks</td>
    <td>9</td>
  </tr>
  <tr>
    <td>Kestrels</td>
    <td>7</td>
  </tr>
</table>

Modify › Add a column

<h2>House points</h2>
<table>
  <tr>
    <th>House</th>
    <th>Points</th>
    <th>Wins</th>
  </tr>
  <tr>
    <td>Owls</td>
    <td>12</td>
    <td>4</td>
  </tr>
  <tr>
    <td>Hawks</td>
    <td>9</td>
    <td>3</td>
  </tr>
  <tr>
    <td>Kestrels</td>
    <td>7</td>
    <td>2</td>
  </tr>
</table>

Make

<h1>Our week</h1>
<table>
  <tr>
    <th>Day</th>
    <th>Morning</th>
    <th>Afternoon</th>
  </tr>
  <tr>
    <td>Monday</td>
    <td>Maths</td>
    <td>Sport</td>
  </tr>
  <tr>
    <td>Tuesday</td>
    <td>Science</td>
    <td>Art</td>
  </tr>
</table>

Stretch and support

Stretch: Add a caption with <caption>House points</caption> just after <table>, and a totals row at the bottom. Style the heading cells with a th rule.

Support: Give students the Modify starter and ask them only to copy the Hawks row and change it to the Kestrels.

Exit ticket

A table has 3 columns and 4 rows of data under the heading row. How many <td> cells does it need, and why?

12 cells: every one of the 4 rows needs a cell for each of the 3 columns (4 × 3).

What each step is for

Where it leads

Next lesson names the parts of a page with header, nav, main and footer. Tables also lead to data work: the same rows and columns are a spreadsheet, a CSV file, and later a list of lists in Python. Class idea: collect class data (favourite sport, pets) and publish it as a table.