Tables and forms
Show data in accessible HTML tables with captions and header cells, and build forms with labels, text boxes, email fields, radio buttons, checkboxes, drop-downs and a submit button.
🎯 By the end of this lesson you will be able to:
- Build a data table with a caption, header cells and rows
- Build a form where every field has a label
- Choose the right input type and use required to check answers
Outline
What you'll learn and why it matters
Tables show data such as timetables, price lists and results. Forms are how websites collect information: sign-ups, searches, orders and contact messages. Both are easy to get wrong in ways that lock people out, so this lesson builds them the accessible way from the start.
Tables for data
A table is made of rows (<tr>, *table row*). Each row holds cells: <th> for a header cell and <td> for a data cell. A <caption> gives the table a title, and <thead> and <tbody> separate the header row from the data.
<table>
<caption>Class timetable, Monday</caption>
<thead>
<tr>
<th scope="col">Time</th>
<th scope="col">Subject</th>
<th scope="col">Room</th>
</tr>
</thead>
<tbody>
<tr>
<td>9:00</td>
<td>Maths</td>
<td>12</td>
</tr>
<tr>
<td>10:00</td>
<td>English</td>
<td>7</td>
</tr>
</tbody>
</table>Browser preview
scope="col" says the header belongs to its column. Screen readers use it to announce, for example, *Subject: English* as you move through the cells. Without CSS the table has no borders; you will add them in Module 2.
Your first form
A form is wrapped in <form>. Every field needs a <label> that says what to type. Connect them by giving the input an id and the label a matching for. Clicking the label then puts the cursor in the box, and screen readers read the label aloud.
<form action="/subscribe" method="post">
<p>
<label for="name">Your name</label><br>
<input type="text" id="name" name="name" autocomplete="name" required>
</p>
<p>
<label for="email">Email address</label><br>
<input type="email" id="email" name="email" autocomplete="email" required>
</p>
<p>
<button type="submit">Subscribe</button>
</p>
</form>Browser preview
| Attribute | What it does |
|---|---|
action and method | Where the answers are sent and how. A server or form service handles them; this course focuses on the form itself. |
id and for | Connect a label to its input. They must match exactly. |
name | The name each answer is sent under, such as email=asha@example.com. |
required | The browser will not submit the form until this field is filled in. |
autocomplete | Lets the browser fill in saved details such as name and email, saving time. |
Choose the right input type
The type attribute changes how a field behaves. On phones, it also changes the keyboard: email shows an @ key and tel shows a number pad.
| Type | For | Bonus |
|---|---|---|
text | Short text such as a name | - |
email | Email addresses | Checks there is an @ and a domain |
tel | Phone numbers | Number keypad on phones |
number | Quantities | Up and down arrows; min and max set limits |
date | Dates | A date picker |
password | Passwords | Hides what you type |
checkbox | Yes/no choices, or picking several options | - |
radio | Picking exactly one option from a group | - |
Choices: radio buttons, checkboxes, drop-downs and text areas
Radio buttons in a group share the same name, so only one can be chosen. Wrap the group in <fieldset> with a <legend> that asks the question. <select> makes a drop-down and <textarea> a box for longer text.
<form action="/order" method="post">
<fieldset>
<legend>Size</legend>
<input type="radio" id="small" name="size" value="small">
<label for="small">Small</label>
<input type="radio" id="large" name="size" value="large" checked>
<label for="large">Large</label>
</fieldset>
<p>
<label for="drink">Drink</label>
<select id="drink" name="drink">
<option value="chai">Masala chai</option>
<option value="coffee">Filter coffee</option>
</select>
</p>
<p>
<input type="checkbox" id="sugar" name="sugar" value="yes">
<label for="sugar">Add sugar</label>
</p>
<p>
<label for="notes">Anything else?</label><br>
<textarea id="notes" name="notes" rows="3" cols="30"></textarea>
</p>
<button type="submit">Place order</button>
</form>Browser preview
Practice
Guided exercise
A contact form
Build a contact form with a required name, a required email, a drop-down for the topic (Booking, Feedback, Other), a message box and a Send button.
- Start with
<form action="/contact" method="post">. - For each field add a
<label for="…">and an input with the sameid, plus aname. - Use
type="email"for the email and addrequiredto name and email. - Use
<select>with three<option>elements, and<textarea>for the message. - End with
<button type="submit">Send</button>.
Show the solution
<form action="/contact" method="post">
<p>
<label for="contact-name">Name</label><br>
<input type="text" id="contact-name" name="name" autocomplete="name" required>
</p>
<p>
<label for="contact-email">Email</label><br>
<input type="email" id="contact-email" name="email" autocomplete="email" required>
</p>
<p>
<label for="topic">Topic</label><br>
<select id="topic" name="topic">
<option value="booking">Booking</option>
<option value="feedback">Feedback</option>
<option value="other">Other</option>
</select>
</p>
<p>
<label for="message">Message</label><br>
<textarea id="message" name="message" rows="4" cols="30"></textarea>
</p>
<button type="submit">Send</button>
</form>Browser preview
How to get there: Every field has a label joined by matching for and id values. The email type and required let the browser check answers before sending, and name gives each answer a label when it reaches the server.
Your turn
A price table
Make a table with the caption Course prices, header cells Course and Price, and three rows: HTML & CSS (Free), Python (Free), SQL (Free).
Show the solution
<table>
<caption>Course prices</caption>
<thead>
<tr>
<th scope="col">Course</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr><td>HTML & CSS</td><td>Free</td></tr>
<tr><td>Python</td><td>Free</td></tr>
<tr><td>SQL</td><td>Free</td></tr>
</tbody>
</table>Browser preview
How to get there: The caption names the table, the header row uses <th scope="col">, and each data row is a <tr> with two <td> cells. & is how you write an & sign safely in HTML.
Common mistakes
A label's for does not match the input's id
You see: Clicking the label does nothing, and screen readers announce an unlabelled field
Fix: Copy the id into the for exactly, including capitals and hyphens.
Radio buttons with different name values
You see: You can select every option at once
Fix: Give every radio button in a group the same name.
Using placeholder instead of a label
You see: The hint vanishes as soon as you type, so people forget what the box was for
Fix: Add a visible <label> for every field.
Using type="text" for everything
You see: No email checking and the wrong keyboard on phones
Fix: Pick email, tel, number or date when they fit.
Using <td> for header cells
You see: Screen readers cannot tell which header a value belongs to
Fix: Use <th scope="col"> for column headers.
Recap
- Tables:
<table>,<caption>, rows in<tr>, header cells<th scope="col">, data cells<td>. - Forms: every field needs a
<label for>that matches the input'sid, plus aname. - Pick input types to get checking and the right phone keyboard;
requiredstops empty answers. - Group radio buttons with one shared name inside a
<fieldset>with a<legend>.
Key terms
- Form control
- Any field a user can fill in or choose: input, select, textarea or button.
- Validation
- Checking answers before they are sent, such as making sure an email has an @.
- Fieldset and legend
- A group of related fields, and the question or title for that group.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. How do you connect a label to its input?
2. Which input type shows a keyboard with an @ key on phones and checks the format?
3. Three radio buttons for Small, Medium and Large can all be selected at once. What is wrong?
4. Which element should hold a column heading such as Price in a data table?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.