Sub topic 1
What a Form Is For: Fewer Fields, Clearer Purpose
Prerequisites
Assume you know basic HTML form elements, CSS spacing, and how users move through a page with a keyboard. This chapter focuses on design decisions rather than JavaScript validation.
Start with the form's purpose
A form collects the smallest amount of information needed for a specific task. Before adding a field, ask:
- What decision or action does this value support?
- Can the site infer it from another value?
- Can the field wait until a later step?
Fewer fields reduce typing, uncertainty, and errors. Group fields by the user's goal, such as “Create an account” or “Send a delivery,” rather than by the data your database happens to store.
Labels: every control needs one
A visible label tells users what to enter. It also gives the control an accessible name. Connect a label to its control by making for equal the input's id.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>Clicking “Email address” now focuses the input. Screen readers can also announce the label when the input receives focus.
Where to put the label
Top-aligned labels usually work well on responsive forms. They keep the label close to the control and leave room for long translations. Left-aligned labels can reduce vertical length on wide screens, but they need a consistent grid and more horizontal scanning.
Floating labels move a label into the field and then above it. They can save space, but they are easy to misread and can disappear or become too small. Use them only when testing shows a clear benefit.
Hint text, and why placeholders are not labels
A placeholder is temporary example text inside a field. It disappears after typing, has weak contrast in many designs, and cannot replace a label. Put instructions and examples in persistent text, then connect that text with aria-describedby.
<label for="postcode">Postcode</label>
<p id="postcode-hint">For example, SW1A 1AA</p>
<input id="postcode" name="postcode" autocomplete="postal-code"
aria-describedby="postcode-hint">Field order: match the user's mental model
Order fields in the sequence people need to think and act. Ask for contact details before delivery preferences, and delivery details before payment. Keep related fields together. Avoid asking for optional information before the primary task is complete.
Exercise
Why should a postcode example use hint text instead of only a placeholder?
Show answer
Hint text remains visible after typing and can be announced through aria-describedby, while a placeholder disappears.