Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Build an accessible form with labels in HTML

14 A form where every field has a label

easy

Build a three-field form. Every field gets a real label, the right input type, and the browser’s own required check.

Stuck? Three hints, smallest first
  1. 1. <label for="x"> must match an id="x" on the field. Nothing else joins them.
  2. 2. The type changes the keyboard on a phone and the validation you get free: email, tel, date.
  3. 3. required is an attribute with no value, and the browser enforces it before submit.

index.html

<form class="book" action="#" method="post">
  <p class="row">
    <label for="name">Full name</label>
    <input id="name" name="name" type="text" autocomplete="name" required>
  </p>

  <p class="row">
    <label for="email">Email</label>
    <input id="email" name="email" type="email" autocomplete="email" required>
  </p>

  <p class="row">
    <label for="travel">Travel date</label>
    <input id="travel" name="travel" type="date" required>
  </p>

  <button type="submit">Reserve</button>
</form>

style.css

.book { max-width: 300px; }

.row {
  display: grid;
  gap: 4px;
  margin: 0 0 12px;
}

.row label {
  font-size: 0.85rem;
  font-weight: 600;
}

.row input {
  padding: 8px 10px;
  border: 1px solid #8e8e98;
  border-radius: 6px;
  font: inherit;            /* inputs do not inherit typography by default */
}

.row input:focus-visible {
  outline: 2px solid #3055d8;
  outline-offset: 1px;
}

button {
  padding: 9px 16px;
  border: 0;
  border-radius: 6px;
  background: #3055d8;
  color: #ffffff;
  font: inherit;
  cursor: pointer;
}

button:focus-visible {
  outline: 2px solid #17171a;
  outline-offset: 2px;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 14

// Every label must point at an id that exists, and every field must be pointed at.
const labelled = [...html.matchAll(/<label for="([^"]+)"/g)].map((m) => m[1]);
const ids = [...html.matchAll(/<input id="([^"]+)"/g)].map((m) => m[1]);

test('three labels', labelled.length, 3);
test('each one points at a field that exists', labelled.every((f) => ids.includes(f)), true);
test('and every field has a label pointing at it', ids.every((i) => labelled.includes(i)), true);
test('the types are chosen, not all text', ['type="email"', 'type="date"'].every((t) => html.includes(t)), true);
test('all three are required', (html.match(/\brequired\b/g) || []).length, 3);
test('autocomplete hints are given where they exist', (html.match(/autocomplete="/g) || []).length, 2);
test('the controls inherit the page font', /font\s*:\s*inherit/.test(css), true);
test('focus is visible and the outline is never removed', /:focus-visible/.test(css) && /outline\s*:\s*none/.test(css) === false, true);

Output


      

for and id are the only thing joining a label to its field. Get them wrong and the label still looks right, but clicking it no longer focuses the input and a screen reader announces the field as unlabelled — a failure that is invisible on screen, which is why the check compares the two lists rather than counting tags. type earns its keep on a phone, where email and date summon a different keyboard, and autocomplete is the difference between a form a returning user fills in one tap and one they retype.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.