Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Full-Stack Web Development — Alpha School

Lesson 4: Forms and Tables: Collecting Input the Accessible Way

Forms are how a website listens. Every search box, sign-up page, quiz and contact form is a form, and getting forms right matters more than almost any other HTML skill, because a form that confuses people or loses their input is a website that fails at its job. The good news is that accessible forms are not harder to build than careless ones: the same few habits that make a form work for a screen reader user also make it work better for everyone, including people on phones, people in a hurry and people who make typos. In this lesson you will build a form that labels every field, checks input kindly, and explains itself clearly, and a table that presents data in a way anyone can read.

Labels, inputs and names

Every form control needs a <label> connected to it with the for attribute, which matches the control's id, like this: <label for="email">Email address</label> <input type="email" id="email" name="email">. That connection does three useful things: clicking the label focuses the field, which helps people with shaky hands or small phone screens; screen readers announce the label when the field is reached; and the browser knows what the field is for. The type attribute matters too: type="email" brings up an email keyboard on phones and checks for an @ sign, type="number" brings up a number keyboard, and type="date" brings up a calendar, all for free. The name attribute is what the server sees when the form is submitted, so give every field a clear, short name, and group related fields in a <fieldset> with a <legend> that names the group, such as “Contact details”.

Helping people get it right

Nobody enjoys being told they filled in a form wrong, so design your form to prevent mistakes rather than punish them. Use the required attribute only on fields you genuinely need, add placeholder text as an example of the format you want, never as a replacement for a label, because placeholder text disappears as soon as someone starts typing. The browser can do basic checks on its own, such as making sure an email field contains an @ sign, but you should also explain what you want in plain words next to the field, for example “Use at least 8 characters”, and when something is wrong, say which field and how to fix it, rather than showing a vague error at the top of the page. A submit button should say what will happen, such as “Send message” or “Create account”, not just “Submit”, and after submission, show a clear confirmation so the visitor knows their message arrived.

<form action="/signup" method="post">
  <fieldset>
    <legend>Join the reading club</legend>
    <label for="name">Your name</label>
    <input type="text" id="name" name="name" required>
    <label for="email">Email address</label>
    <input type="email" id="email" name="email" required>
    <label for="age">Age group</label>
    <select id="age" name="age">
      <option>Under 13</option>
      <option>13–17</option>
      <option>18 or over</option>
    </select>
  </fieldset>
  <button type="submit">Join the club</button>
</form>

Tables for data, not for layout

Tables are for data that belongs in rows and columns, such as timetables, results and price lists, and they should never be used to lay out a page, which is the job of CSS. A proper table starts with a <caption> that names what the table shows, then a <thead> with a row of <th> header cells, and a <tbody> with rows of <td> data cells. The scope attribute on header cells, such as scope="col" for a column header or scope="row" for a row header, tells screen readers which cells each header describes, so a blind user can hear “Monday, 9:00, Maths” instead of a meaningless stream of cells. Keep tables simple: one idea per table, short cell contents, and if a table needs more than a few columns on a phone screen, consider whether a list would serve the reader better.

A quick test you can run yourself

Before you call a form finished, test it the way a visitor would. Tab through it with your keyboard only: every field should be reachable, the focus should always be visible, and the order should follow the visual order on the page. Try submitting it empty and with mistakes, and check that the error messages make sense. Then zoom the page to 200 percent, which is how many people with low vision read the web, and confirm that labels stay next to their fields and nothing overlaps. Finally, read the form aloud: if the labels and instructions make sense without seeing the page, a screen reader user will be able to use it too. These five minutes of testing catch most form problems, and they are the same checks professional teams run before a page goes live.

Watch & Learn

One video to watch for this unit, from a trusted educator.

  • HTML Full Course - Build a Website Tutorial — freeCodeCamp.org
    Mike Dane's beginner HTML course walks through tags, links, images, lists, tables and forms — the same building blocks as this unit, with plenty of examples to follow along.

Key Terms

Form
A set of controls, such as text fields and buttons, that lets a visitor send information to a site.
Label
Text connected to a form control with the for attribute, telling the visitor and screen readers what the field is for.
Input type
The type attribute, such as email, number or date, which tells the browser what kind of value to expect.
Fieldset / legend
A fieldset groups related fields, and its legend names the group, such as “Contact details”.
Placeholder
Example text shown inside an empty field; it disappears when typing starts and must never replace a label.
Caption
A short title for a table, naming what the table shows.
Scope
An attribute on a table header cell saying whether it heads a row or a column, so screen readers can announce it correctly.

Discussion Questions

  1. Why is placeholder text a poor replacement for a label? Think about what happens when someone starts typing, and what a screen reader announces.
  2. A form asks for a phone number but marks it as required. When might that be a problem, and what could the designer do instead?
  3. Why should tables be used for data but never for page layout?

Practice Questions

  1. How do you connect a <label> to its input?
  2. What does type="email" do that type="text" does not?
  3. What is the job of the name attribute on a form field?
  4. In a table, what is the difference between <th> and <td>?
  5. Why should a submit button say “Send message” instead of “Submit”?
Show answers
  1. Give the input an id and set the label's for attribute to the same value. For example, <label for="email"> with <input id="email"> links the two together.
  2. It brings up an email keyboard on phones and checks for an @ sign. The browser knows the field expects an email address and can help the visitor get it right.
  3. It names the field in the data sent to the server. Without a name, the server receives the value but has no idea what it is.
  4. th is a header cell and td is a data cell. Headers name the row or column, and data cells hold the values, and screen readers use that difference to announce the table correctly.
  5. It tells the visitor what will happen. Clear wording reduces mistakes and anxiety, and it helps screen reader users who hear the button out of context.
Keep reading to complete this lesson