← Structure and style a page

Tables

Put data in a table by writing it one row at a time, with the same number of cells in every row.

Timetables, sports results, price lists and school reports are tables: data in rows and columns.

Some information is a grid: rows going across, columns going down. HTML has no tag for a column. You write the table one row at a time and the columns appear.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

page.html
<table>
  <tr>
    <th>House</th>
    <th>Points</th>
    <th>Wins</th>
  </tr>
  <tr>
    <td>Owls</td>
    <td>12</td>
    <td>4</td>
  </tr>
</table>

Read the code. <table> holds the table, and <tr> and <th> and <td> are new. How will the six words be arranged?

How will the six words be arranged on the page?

RunRun it and compare the result with your prediction.

Run

Compare the page with your prediction. Boxes is on, so each row and each cell shows as a box. Switch it off when you are done.

Code What it does Why
<table> … </table> Holds the whole table Everything in a table sits between these two tags
<tr> … </tr> Makes one row, going across tr is "table row". You write the table a row at a time
<th>House</th> Makes a heading cell th is "table heading". The browser makes it bold and centred
<td>Owls</td> Makes an ordinary cell td is "table data". The tag says what it is; the look is the browser's
three cells in each row Make three columns There is no column tag. A column is the cells in the same place in each row, so every row needs the same number of cells
no lines around the cells The table has no grid The browser draws none. Colours and text styles come from rules, as for any other tag
page.html
<table>
  <tr>
    <th>House</th>
    <th>Points</th>
    <th>Wins</th>
  </tr>
  <tr>
    <td>Owls</td>
    <td>12</td>
    <td>4</td>
  </tr>
</table>
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

Put the scrambled lines in order.

Sam wrote the table a column at a time.

<table>
  <tr><td>Owls</td></tr>
  <tr><td>Hawks</td></tr>
  <tr><td>12</td></tr>
  <tr><td>9</td></tr>
</table>

One row in this table is broken.

Put the lines in order to make one row for the Kestrels: 7 points and 2 wins. One line is not needed.

Pieces (click to add)
    Your program

      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?

      The Hawks have no points, and their row is shorter than the others. Click the line that causes it.

      ModifyChange the code on purpose and check the result.

      Modify

      Add the Kestrels, with 7 points.

      Need a hint?

      Copy a whole row, from <tr> to </tr>, then change the words.

      Show the expected result
      What the finished page looks like
      What it should look like. Your words can be different.

      Add a Wins column: the Owls have 4, the Hawks 3 and the Kestrels 2.

      Need a hint?

      A new column needs a heading cell in the first row.

      Another hint

      Then every other row needs one more <td>.

      Show the expected result
      What the finished page looks like
      What it should look like. Your words can be different.
      Try it yourselfYour edits are saved in this browser.

      Add a row for the Kestrels.

        This ticks when your page has all of the above.

        Add a Wins column.

          This ticks when your page has all of the above.

          InvestigateTrace how each line of code connects to what you see.

          Mix it up

          The Owls row is already gold and linked.

          page.html
          <style>
            th {
              background-color: navy;
              color: white;
            }
            .winner {
              background-color: gold;
            }
          </style>
          <table>
            <tr>
              <th>House</th>
              <th>Points</th>
            </tr>
            <tr class="winner">
              <td><a href="https://example.com/owls">Owls</a></td>
              <td>12</td>
            </tr>
            <tr>
              <td>Hawks</td>
              <td>9</td>
            </tr>
          </table>
          
          The Hawks win next week. Which row gives them the gold row and makes their name a link to https://example.com/hawks?

          MakeBuild something new using what you have learned. Expect a few tries; that is normal.

          Make

          Make a table of your own: a timetable for two days, your sports results, or a list of games with their ratings. Give it a heading row and at least two rows of data.

          Try it yourselfYour edits are saved in this browser.
          Need a hint?

          Plan it first: what are the column headings? How many rows?

          Another hint

          Build the heading row with <th>, then one row at a time with <td>. Count the cells in each row.

          Show the expected result
          What the finished page looks like
          What it should look like. Your words can be different.

          Make a table with a heading row and at least two rows of data.

            This ticks when your page has all of the above.

            For teachers

            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. About 45 minutes.

            Open the teacher guide: answers, solutions, slides and a worksheet.

            Students will be able to

            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.

            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

            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.

            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.

            What each step is for

            Now you can
            Next lesson10. Name the parts of a pageName the top, menu, content and bottom of a page with header, nav, main, section and footer, and find out why they change nothing you can see.