The Academy

Loading the search index…

Searches the academy on this device only.

Tables, forms, semantic structure and accessibility

Build tables for data and forms that people can fill in, use semantic elements to give a page meaning, and apply the basic habits that make pages accessible.

30 minelementary

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

By the end of this lesson you will be able to

  • Build a small table with a header row and data cells.
  • Create a form with labelled text, email, choice and submit controls.
  • Choose semantic elements such as header, nav, main and footer instead of generic boxes.
  • Apply basic accessibility habits: labels, alt text, heading order and clear link text.

Most real pages contain more than headings and paragraphs. They show timetables and prices in tables, and collect names and messages with forms. They also have to work for people who use a screen reader, a keyboard instead of a mouse, or a small phone screen. This lesson shows the HTML for tables and forms, and the semantic elements that give a page meaning for browsers, search engines and assistive tools.

Tables for data

A table shows data in rows and columns, such as a timetable or a price list. Use tables for data only, never to arrange the look of a page. The <table> element holds rows (<tr>, table row). A header cell is <th> and an ordinary data cell is <td>. A <caption> gives the table a title. Every row should contain the same number of cells.

HTML
<table>
  <caption>Garden club sessions</caption>
  <thead>
    <tr><th scope="col">Day</th><th scope="col">Time</th><th scope="col">Activity</th></tr>
  </thead>
  <tbody>
    <tr><td>Saturday</td><td>09:00</td><td>Digging</td></tr>
    <tr><td>Wednesday</td><td>17:30</td><td>Watering</td></tr>
  </tbody>
</table>
A table of club sessions. The first row is made of header cells.

<thead> and <tbody> separate the header from the body. The scope="col" attribute tells assistive tools that each header describes the column beneath it. A screen reader can then say "Time: 09:00" instead of reading a bare number with no context.

Simulated interface: not a real screenshot

How the browser draws the club table, with the key parts marked.

example.org/sessions

Garden club sessions

Day | Time | Activity

Saturday | 09:00 | Digging

Wednesday | 17:30 | Watering

  1. Marker 1: caption: the table title
  2. Marker 2: th cells: column headers
  3. Marker 3: td cells: the data
How the browser draws the club table, with the key parts marked.
Read this figure as text

A simulated browser window showing a three-column table with a header row and two data rows.

A browser window with the address example.org/sessions. The body shows the caption Garden club sessions, then a header row with Day, Time and Activity, then two data rows: Saturday, 09:00, Digging and Wednesday, 17:30, Watering. Marker 1 points to the caption, marker 2 to the header row and marker 3 to the data rows.

Forms and labels

A form collects information from the reader. The <form> element wraps the controls. The <input> element makes a field; its type attribute chooses the kind: text, email, checkbox, radio and so on. A <textarea> gives space for a longer message, <select> with <option> makes a drop-down list and <button type="submit"> sends the form. The name attribute names the data when the form is sent.

Every control needs a label. A <label> is a caption linked to its input: the label's for attribute must match the input's id. A linked label is read aloud by screen readers, and clicking it puts the cursor in the field, which gives a bigger target for fingers and mouse clicks. Hint text shown inside an empty field is not a replacement for a label because it vanishes as soon as you type.

HTML
<form action="/join" method="post">
  <p>
    <label for="name">Your name</label>
    <input id="name" name="name" type="text" required>
  </p>
  <p>
    <label for="email">Email address</label>
    <input id="email" name="email" type="email" required>
  </p>
  <p>
    <label for="day">Preferred day</label>
    <select id="day" name="day">
      <option>Saturday</option>
      <option>Wednesday</option>
    </select>
  </p>
  <p>
    <label for="note">Message</label>
    <textarea id="note" name="note" rows="3"></textarea>
  </p>
  <button type="submit">Join the club</button>
</form>
A simple sign-up form. It uses fictional data and posts nowhere real.

The required attribute makes the browser refuse to send the form if that field is empty, and type="email" makes it check that the text looks like an email address. This is a convenience for the reader. It is not real security: a server must always check the data again. Making the form actually deliver the information to somebody needs a server, which is beyond this lesson.

Simulated interface: not a real screenshot

The sign-up form as drawn by a browser, with labels and the button marked.

example.org/join

Join the club

Your name: [ Amina Hassan ]

Email address: [ amina@example.com ]

Preferred day: [ Saturday v ]

Join the club
  1. Marker 1: Heading for the form
  2. Marker 2: Label linked to its field
  3. Marker 3: Submit button
The sign-up form as drawn by a browser, with labels and the button marked.
Read this figure as text

A simulated browser window showing a form with name, email, day and message controls and a submit button.

A browser window with the address example.org/join. The page has a heading Join the club, then rows for Your name, Email address, Preferred day and Message, and a button Join the club. Marker 1 points to the heading, marker 2 to the label rows (each label is linked to its field) and marker 3 to the submit button.

Semantic structure

Semantic elements describe the role of a part of the page, not just its look. Instead of putting everything in anonymous boxes, you use <header> for the top of the page, <nav> for the main navigation links, <main> for the unique content, <section> and <article> for self-contained parts, <aside> for side notes and <footer> for the bottom. A page should have only one <main>.

Why bother? Screen reader users can jump straight to the navigation or the main content. Search engines understand the page better. Your own code is easier to read. The generic containers <div> and <span> still exist for grouping when no semantic element fits, but use them last.

HTML
<body>
  <header>
    <h1>Garden club</h1>
    <nav>
      <a href="index.html">Home</a>
      <a href="sessions.html">Sessions</a>
    </nav>
  </header>
  <main>
    <h2>Next session</h2>
    <p>Saturday at nine o'clock.</p>
  </main>
  <footer>
    <p>Contact: club@example.org</p>
  </footer>
</body>
A semantic page layout.
Diagram

Generic boxes compared with semantic elements.

Generic boxes
  • div (top)
  • div (links)
  • div (content)
  • div (bottom)
  • No role is announced

Works, but says nothing

Semantic elements
  • header
  • nav
  • main
  • footer
  • Roles are announced

Meaning built in

Generic boxes compared with semantic elements.
Read this figure as text

Two columns comparing a page built from div boxes with a page built from semantic elements.

Left column, Generic boxes: div, div, div, div. Nothing says what each box is for, so assistive tools cannot offer shortcuts. Right column, Semantic elements: header, nav, main and footer. Each name says its role, so screen reader users can jump to the navigation or the main content, and search engines understand the page.

Accessibility basics

Accessibility means that people with different abilities and devices can use your page. That includes people who are blind, have low vision, cannot use a mouse, find small targets hard to press or are in bright sunlight. Good HTML gives you most of it for free. The habits below cost almost nothing.

  • Give every informative image meaningful alt text. If an image is purely decoration, use an empty alt="" so screen readers skip it.
  • Use headings in order (h1, then h2, then h3) and never skip levels to get a size.
  • Write link text that makes sense alone. Use "Read the seed guide", not "Click here".
  • Label every form field with a linked <label>.
  • Do not rely on colour alone to carry meaning, such as red text only for errors. Add words or symbols too.
  • Check that you can reach every link and button by pressing the Tab key, and that you can see where you are.

Common mistakes

  • ✖ Instead of: Using a table to position things on the page.

    ✔ Do this: Tables are for data. Use headings, sections and CSS for layout.

  • ✖ Instead of: Using <br> tags or empty paragraphs to make space.

    ✔ Do this: Use CSS margins for spacing and separate paragraphs for separate ideas.

  • ✖ Instead of: Skipping from <h1> to <h4> because h4 looks nicer.

    ✔ Do this: Keep heading levels in order and style the size with CSS.

  • ✖ Instead of: Making a button out of a plain <div> with a click handler.

    ✔ Do this: Use <button> or <a>. They work with the keyboard and screen readers automatically.

If something goes wrong

My table looks like plain text with no lines.
Browsers draw tables without borders by default. The table is correct. Borders are added later with CSS.
Pressing submit reloads the page and nothing seems to happen.
A form with no real server address cannot store anything. For practice that is normal; check that the controls look correct and carry on.
The browser will not let me submit my form.
A required field is probably empty, or an email field does not look like an address. Fill in every required field with fictional data.
My table has cells in the wrong columns.
Count the <td> or <th> elements in each row. Every row must have the same number, and each must be closed.

Key terms

Form label
The visible caption of a form field, linked to the field so that screen readers and clicks connect the two.
Semantic HTML
HTML elements whose names say what a part of the page is for, such as header, nav, main and footer.
Accessibility
Making devices, documents and websites usable by people with different abilities, such as poor sight, hearing or movement.

Practise it for real

Reading is not the same as being able to do it. Complete these on your own computer, then tick only what you have actually done. These ticks are your own honest record: nothing checks them for you.

Guided exercise

Write a timetable

In the live editor from the previous lesson, build a table of a fictional weekly timetable with a caption, a header row of th cells with scope="col" and at least three data rows. Check that each row has the same number of cells.

You have succeeded when:
Guided exercise

Create a labelled form

Write a contact form with a name field, an email field and a message box, each with a label linked by for and id, and a submit button. Click each label in the preview to prove it is linked: the cursor should jump into its field.

You have succeeded when:
Independent challenge

Challenge: make a page semantic and accessible

Take a page built only from <div> elements (invent one with a title, three links, a paragraph and a footer line) and rewrite it using header, nav, main and footer. Add alt text to one image and change one "click here" link into clear link text. Explain in two sentences who benefits.

You have succeeded when:

Knowledge check

6 questions. You need 80% to pass. Mistakes here are part of learning, and you can retry as often as you like.

  1. Question 1.Which element makes a header cell in a table?
  2. Question 2.Kamau writes <label>Email</label><input id="mail" type="email"> and finds that clicking the word Email does not put the cursor in the field.What is missing?
  3. Question 3.Hint text shown inside an empty field is a good replacement for a visible label.
  4. Question 4.Match each semantic element to its job.
  5. Question 5.Which link text is best for accessibility?
  6. Question 6.The required attribute on an input is enough on its own to keep a website secure.

Summary

  • Tables are for data: table, tr, th, td, caption, with thead and tbody.
  • A form uses form, input (with type), textarea, select, button and a linked label for every field.
  • Semantic elements (header, nav, main, footer) describe roles, not looks.
  • Alt text, ordered headings, clear link text and keyboard access make pages usable by more people.

Checklist: can you do these?

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

Related lessons

References and further reading

Official sources for details that can change between versions.

On this page