Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Build an accessible booking form in HTML and CSS

34 A booking form, properly wired

hard

Build the whole form: grouped fields, a real label on every control, hints and errors that are announced, native validation, and one field shown in its error state. No placeholder standing in for a label.

Stuck? Three hints, smallest first
  1. 1. Every for must match an id, and every aria-describedby must point at an element that exists.
  2. 2. aria-invalid="true" exposes the error state; style from that attribute so the two cannot drift apart.
  3. 3. The error message needs to be in the field’s aria-describedby, or it is only visible to people who can see it.

index.html

<form class="booking" action="#" method="post" novalidate>
  <fieldset>
    <legend>Passenger</legend>

    <p class="field">
      <label for="bk-name">Full name</label>
      <input id="bk-name" name="name" type="text" autocomplete="name" required>
    </p>

    <p class="field">
      <label for="bk-phone">Phone</label>
      <input id="bk-phone" name="phone" type="tel" autocomplete="tel"
             inputmode="numeric" pattern="0[0-9]{10}" required
             aria-describedby="bk-phone-hint">
      <span class="hint" id="bk-phone-hint">Eleven digits, starting with 0.</span>
    </p>
  </fieldset>

  <fieldset>
    <legend>Journey</legend>

    <p class="field">
      <label for="bk-route">Route</label>
      <select id="bk-route" name="route" required>
        <option value="">Choose a route</option>
        <option value="abuja">Lagos to Abuja</option>
        <option value="kano">Lagos to Kano</option>
      </select>
    </p>

    <p class="field">
      <label for="bk-seats">Seats</label>
      <input id="bk-seats" name="seats" type="number" min="1" max="6" value="0"
             required aria-invalid="true" aria-describedby="bk-seats-error">
      <span class="error" id="bk-seats-error">Choose between 1 and 6 seats.</span>
    </p>
  </fieldset>

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

style.css

.booking {
  max-width: 320px;
  font-size: 0.9rem;
}

.booking fieldset {
  margin: 0 0 14px;
  padding: 10px 14px 4px;
  border: 1px solid #c9cad1;
  border-radius: 8px;
}

.booking legend { padding: 0 6px; font-weight: 700; }

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

.field label { font-weight: 600; }

.field input,
.field select {
  padding: 8px 10px;
  border: 1px solid #8e8e98;
  border-radius: 6px;
  background: #ffffff;
  color: #17171a;
  font: inherit;
}

.field input:focus-visible,
.field select:focus-visible,
button:focus-visible {
  outline: 2px solid #3055d8;
  outline-offset: 1px;
}

.hint { color: #5c5c66; font-size: 0.8rem; }

.error { color: #a3341f; font-size: 0.8rem; font-weight: 600; }

/* Styled from the ARIA attribute, so the red border and the announced state
   come from one source and cannot disagree. */
.field input[aria-invalid='true'] {
  border-color: #a3341f;
  border-width: 2px;
}

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

button:hover { background: #2543b4; }
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 34

const labels = [...html.matchAll(/<label for="([^"]+)"/g)].map((m) => m[1]);
const fields = [...html.matchAll(/<(?:input|select|textarea)[^>]*\bid="([^"]+)"/g)].map((m) => m[1]);
const ids = [...html.matchAll(/\bid="([^"]+)"/g)].map((m) => m[1]);
const described = [...html.matchAll(/aria-describedby="([^"]+)"/g)]
  .flatMap((m) => m[1].split(/\s+/));

test('four controls, four labels', [fields.length, labels.length], [4, 4]);
test('every label points at a control that exists', labels.every((f) => fields.includes(f)), true);
test('and every control has a label pointing at it', fields.every((f) => labels.includes(f)), true);
test('every describedby target exists in the markup', described.every((d) => ids.includes(d)), true);
test('the fields are grouped under legends', countTag(html, 'fieldset') === 2 && countTag(html, 'legend') === 2, true);
test('no placeholder is doing a label\'s job', /placeholder=/.test(html), false);
test('the broken field is exposed as invalid and styled from that', /aria-invalid="true"/.test(html) && /\[aria-invalid='true'\]/.test(css), true);
test('a real submit button, and focus is never merely removed', /<button type="submit"/.test(html) && /outline:\s*none/.test(css) === false, true);

Output


      

The fourth check is the one worth stealing for your own projects: an aria-describedby pointing at an id that does not exist fails silently — nothing looks wrong, the hint is simply never announced. Comparing the two lists catches it. Styling the error from [aria-invalid='true'] rather than a separate class means the red border and the announced state are the same fact. Note novalidate on the form: it is here so the preview can show the error state without the browser intercepting first, and in a real form you would only use it when you are handling validation yourself.

Newsletter

New writing, when it lands.

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