Mark Up a Data Table

Rosa’s plots page sums up the garden in two sentences: The garden has 24 plots. Ana, Jamal, Mei, Tomás and Priya tend the first five. It doesn’t say who tends which plot. A visitor wants to find one plot and read across to its details, which is what rows and columns are for.

In this lesson, you’ll turn those sentences into a table of the first five plots and check what Chrome builds from it in the Elements panel and the Accessibility pane. You’ll also decide when content belongs in a table at all. Headers are what make a grid of values readable, so start with them.

Build the Table Around Its Headers

A grid of values only makes sense with its titles: Ana is who tends plot 1, and 3 × 4 m is its size. So an HTML table carries its headers in the markup from the start. <table> holds the grid, each <tr> (table row) holds one row, and each cell is either a <td> (table data) or a <th> (table header). A <th> names its column or its row, and its scope attribute says which: scope="col" or scope="row".

Open plots.html. In <main>, replace the paragraph that starts with The garden has 24 plots, from its <p> to its </p>, with this:

Language: HTML
<p>The garden has 24 plots. Here are the first five.</p>
<table>
  <caption>The first five plots and who tends them</caption>
  <thead>
    <tr>
      <th scope="col">Plot</th>
      <th scope="col">Size</th>
      <th scope="col">Tended by</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>3 × 4 m</td>
      <td>Ana</td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td>3 × 4 m</td>
      <td>Jamal</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>3 × 4 m</td>
      <td>Mei</td>
    </tr>
    <tr>
      <th scope="row">4</th>
      <td>3 × 4 m</td>
      <td>Tomás</td>
    </tr>
  </tbody>
</table>

The <caption>, the first element inside <table>, gives the table its title. The header row sits in <thead> (table head), and the data rows in <tbody> (table body). Each plot number is a <th scope="row">, because it names its row the way Tended by names its column. Every row has three cells, so they line up into columns.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.