Full answer
A form is a set of controls the user fills in. Four things make a basic form usable for everyone.
- A
labelfor each control. Connect it withfor(matching the control'sid), or wrap the control inside the label. Clicking the label focuses the input, and screen readers announce the label as the control's name. A placeholder is not a label. It disappears as the user types. - The right
type.type="email",tel,number,dateandpasswordgive the right keyboard on phones and some built-in checks. - A
nameattribute. It is the key that is sent to the server, so a control withoutnameis not submitted. fieldsetandlegendfor groups, such as a set of radio buttons. The legend becomes the group's name.
Add autocomplete values such as name, email and tel so browsers can fill in details. Use button type="submit" for the submit action.
In the accessibility tree, the two inputs are named "Full name" and "Email address", and the radio buttons are in a group named "Fee plan". Clicking the word "Email address" moves focus into the email box.
Group related fields, keep one question per label, and write labels so they still make sense when read out of context, for example "Email address" rather than just "Email".
<form action="/apply" method="post">
<p>
<label for="fullname">Full name</label>
<input id="fullname" name="fullname" type="text" autocomplete="name" required>
</p>
<p>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
</p>
<fieldset>
<legend>Fee plan</legend>
<label><input type="radio" name="plan" value="monthly" checked> Monthly</label>
<label><input type="radio" name="plan" value="yearly"> Yearly</label>
</fieldset>
<button type="submit">Apply</button>
</form>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Connect label for to input id, or wrap the input in the label.
- Pick the right input type for keyboard and validation.
- A control without a name attribute is not submitted.
- fieldset and legend name groups such as radio sets.
- A placeholder is not a replacement for a label.
Common mistakes
- using placeholder text as the only label. It fails contrast checks often, vanishes while typing and may not be announced reliably.
- leaving out name on an input and then wondering why the server does not receive the value.
Follow-ups
- what is the difference between GET and POST on a form?
- What is the difference between a button of type submit and type button?
- Why does clicking a label focus its input?
Tips
- Write labels above the inputs on small screens. They stay readable when the keyboard opens and when text is zoomed.
- Write a short form from memory in the interview: label, input, fieldset, button. It is a common live task.
- Mention autocomplete values for name, email and address.