Leet School

{module 3}

Page sections and a price table

Your first page in HTML20 min read

What you will be able to do

  • Divide a page into its large parts with header, nav, main, section and footer.
  • Build a price table with a caption and header cells.
  • Decide whether a piece of content is a table, a list or a paragraph.

Why it matters

Lotus Laundry washes and irons clothes near the old city of Chiang Mai, Thailand. Its owner, Malee Srisuk, wrote her prices in one long paragraph on the shop's page: so much per kilo to wash, so much to iron, a different price for duvets, and when each one is ready.

Customers read the paragraph quickly, picked out the wrong number, and argued at the counter when the bill was higher. A tourist who uses a screen reader could not find the prices at all without listening to the whole page.

Prices are data with rows and columns: a service, its price, when it is ready. HTML has an element for that shape, the table. It also has elements that name the large parts of a page, so that anyone can jump to the part they need.

The idea

The large parts of a page

Most pages have the same large parts: an introduction at the top, a menu of links, the content the page exists for, and a closing part. HTML has an element for each one.

  • header is the introduction: usually the name of the business and a sentence about it.
  • nav holds the main links for moving around: the navigation.
  • main is the content the page exists for. A page has only one main.
  • section groups one topic inside the page, and starts with a heading.
  • footer is the closing part: contact details, the owner, the date.

These elements change nothing you can see. They are for software. A screen reader can jump straight to main or to nav, the same way it jumps to a heading. As in lesson 2, you choose them for what the content is.

On a long page, the navigation can jump to a section. First give the section an id attribute. An id is a name for one element, and it must be unique: no other element on the page may have the same one.

From index.html
<section id="hours">

Then link to it with # and the id:

From index.html
<a href="#hours">Opening hours</a>

A click scrolls the page to that section, and the address in the address bar ends with #hours.

When content is a table

A table is for data with rows and columns: each row is one thing, and each column is one fact about it. A price list is a table: each row is a service, and the columns are its name, its price and when it is ready. A reader can look down a column to compare prices, or along a row to learn all about one service.

If the content has only one fact per item, such as a list of subjects, it is a list, not a table. If it is sentences, it is paragraphs. And never use a table to place things side by side on the screen. That is the job of CSS, in module 3.

The parts of a table

A table is built from rows, and each row from cells.

From index.html
<tr>
  <th scope="col">Service</th>
  <th scope="col">Price</th>
  <th scope="col">Ready</th>
</tr>
<tr>
  <td>Wash and fold, per kilo</td>
  <td>$1.50</td>
  <td>Next day</td>
</tr>
  • table holds the whole table.
  • tr is a table row.
  • th is a header cell: it names a column or a row.
  • td is a data cell: one fact.
  • scope="col" says that a header cell names the column below it.

Header cells matter to a screen reader. When its user moves to the cell "$1.50", it can also say "Price", the header of that column. Without header cells, its user hears a list of values and must remember what each column was.

A caption gives the table a title. It goes first, straight after the opening <table> tag, and the browser shows it above the table:

From index.html
<caption>
  Prices in US dollars. Clothes are weighed when you bring them in.
</caption>

Worked example

Make a folder called lotus-laundry, with index.html in it, and type this page:

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Lotus Laundry, Chiang Mai</title>
  </head>
  <body>
    <header>
      <h1>Lotus Laundry</h1>
      <p>Washing, drying and ironing near the old city of Chiang Mai.</p>
      <nav>
        <a href="#prices">Prices</a>
        <a href="#hours">Opening hours</a>
      </nav>
    </header>
    <main>
      <section id="prices">
        <h2>Prices</h2>
        <table>
          <caption>
            Prices in US dollars. Clothes are weighed when you bring them in.
          </caption>
          <tr>
            <th scope="col">Service</th>
            <th scope="col">Price</th>
            <th scope="col">Ready</th>
          </tr>
          <tr>
            <td>Wash and fold, per kilo</td>
            <td>$1.50</td>
            <td>Next day</td>
          </tr>
          <tr>
            <td>Wash and iron, per kilo</td>
            <td>$2.50</td>
            <td>In 2 days</td>
          </tr>
          <tr>
            <td>Duvet, any size</td>
            <td>$6.00</td>
            <td>In 2 days</td>
          </tr>
        </table>
      </section>
      <section id="hours">
        <h2>Opening hours</h2>
        <p>Every day from 8:00 to 19:00.</p>
      </section>
    </main>
    <footer>
      <p><a href="mailto:malee@lotuslaundry.example">Email Malee at Lotus Laundry</a></p>
    </footer>
  </body>
</html>

Save it and open it in your browser. This is the text the browser shows:

Output
Lotus Laundry

Washing, drying and ironing near the old city of Chiang Mai.

Prices Opening hours
Prices
Prices in US dollars. Clothes are weighed when you bring them in.
Service                   Price                     Ready
Wash and fold, per kilo   $1.50                     Next day
Wash and iron, per kilo   $2.50                     In 2 days
Duvet, any size           $6.00                     In 2 days
Opening hours

Every day from 8:00 to 19:00.

Email Malee at Lotus Laundry

The header cells are bold, and the caption sits above the table. The browser draws no lines between the cells. Lines are part of how a table looks, so they come from CSS. The two navigation links sit side by side, because a link alone is not a block of its own.

The large parts changed nothing you can see. Here is how many of each the page has, in this order: header, nav, main, section and footer:

Output
1
1
1
2
1

Click "Opening hours" in the navigation. On a short page the window may not move far, because there is little to scroll. Look at the address bar instead. It now ends with #hours. On your computer the folders before index.html are your own. On the checker's computer the address was this:

Output
file:///home/you/project/index.html#hours

Open the Elements panel and look inside table. You see a tbody element around the rows that you did not write. The browser adds one to every table, as it added head and body in lesson 1.

Common mistakes

Header cells written as data cells

The learner made the first row bold with strong, so it looks like a row of headers:

From no-header-cells.html
<tr>
  <td><strong>Service</strong></td>
  <td><strong>Price</strong></td>
  <td><strong>Ready</strong></td>
</tr>

Here is how many header cells the browser found:

Output
0

The table looks almost the same, and means less. A screen reader can no longer say "Price" with "$1.50". The words name columns, so they are th with scope="col", and the strong is not needed: the browser makes header cells bold.

Prices lined up with spaces

From spaces.html
<p>
  Wash and fold, per kilo      $1.50     Next day
  Wash and iron, per kilo      $2.50     In 2 days
</p>

In the editor, the columns line up. This is what the browser shows:

Output
Wash and fold, per kilo $1.50 Next day Wash and iron, per kilo $2.50 In 2 days

As in lesson 2, the browser turns every run of spaces and line breaks into one space. This is Malee's paragraph again. Data with rows and columns belongs in a table.

Think it through

Lotus Laundry is now closed on Sundays and open late on Fridays. Somchai, who helps Malee, wants to show the opening hours as a table too, with a row for each kind of day.

Understand. In: three kinds of day and their hours. Out: a table in which a reader can find when the laundry opens and closes on any day, and a screen reader can say which day each time belongs to.

Break down. The caption, the header row, and one row for each kind of day.

Plan. Columns: the day, the opening time, the closing time. The header row has th cells with scope="col" for "Opens" and "Closes". In every other row, the first cell names the row, so it is a th too, with scope="row". The top left corner names nothing, so it is an empty td.

Build. He writes the caption, the header row and one row, "Monday to Thursday", and checks it in the browser before he adds "Friday" and "Sunday".

Test. He picks one cell, "21:00", and says its headers aloud, as a screen reader would: "Friday, Closes, 21:00". That is right. The odd case is Sunday: there is no time to write. He writes "Closed" in the first time cell and leaves the second empty, then changes his mind: an empty cell could look like a mistake, so both say "Closed".

Reflect. The day names look like ordinary data, but they name the rows, so they are header cells. A table can have headers along the top and down the side.

At work

Clients tell you what they need in conversation, and conversation is easy to forget. After Malee explained her prices on the phone, a careful developer wrote two lists: what she said, and what she did not say.

Notes from the call with Malee, Lotus Laundry

She said: wash and fold $1.50 a kilo, ready next day. Wash and iron $2.50 a kilo, ready in 2 days. Duvets $6 each.

She did not say: whether there is a smallest load, such as 2 kilos. When a duvet is ready. Whether the price is the same for a same-day wash. Whether she collects from hotels.

The first list becomes the table. The second list becomes the questions for the next message. Without it, the gaps would only show up as a blank cell in the table, or as an argument at the counter.

Your tasks

Task 1: Build the page

Build the worked example in lotus-laundry/index.html. Add a fourth service to the table with an invented price, and a third link in the navigation that jumps to a new section about how to find the laundry.

You are done when the table has four rows of services, and the new link changes the address bar to end with the id of your new section.

Task 2: Read the structure

Open the Elements panel. Find header, nav, main, both section elements and footer, and the tbody that the browser added.

You are done when you can say, for each th in your table, which column it names.

Task 3: The opening hours table

Build the opening hours table from "Think it through" in the "Opening hours" section, in place of the paragraph. Use row headers for the days.

You are done when the table has a caption, a header row with scope="col", and a th with scope="row" at the start of each other row.

Recap

  • header, nav, main, section and footer name the large parts of a page. A page has one main.
  • An id names one element. A link to # and that id jumps to it.
  • A table is for data with rows and columns, never for placing things on the screen.
  • A table has a caption, rows of tr, header cells th with a scope, and data cells td.

Go deeper