Emmanuel Akinfulubi

HTML and CSSPractical I

Form field with an accessible error message in HTML

13 A form field with a visible label and an error

fair

Build a labelled text field that can show an error message, wired up so assistive technology announces both the label and the error.

Stuck? Three hints, smallest first
  1. 1. A <label for> pointing at the input’s id is the connection. Placeholder text is not a label.
  2. 2. aria-describedby points the input at its error message so it gets read out.
  3. 3. aria-invalid="true" tells assistive technology the field is currently wrong.

index.html

<div class="field">
  <label for="seats">Number of seats</label>
  <input id="seats" name="seats" type="number" min="1" max="14"
         value="0" aria-invalid="true" aria-describedby="seats-error">
  <p class="error" id="seats-error">Enter a number between 1 and 14.</p>
</div>

style.css

.field {
  display: grid;
  gap: 4px;
  max-width: 240px;
}

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

.field input {
  padding: 8px 10px;
  border: 1px solid #b9bac0;
  border-radius: 6px;
  font: inherit;
}

/* The invalid state is styled from the ARIA attribute, so the visual and the
   announced state cannot drift apart. */
.field input[aria-invalid='true'] {
  border-color: #c0392b;
}

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

.error {
  margin: 0;
  color: #c0392b;
  font-size: 0.8rem;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
10 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 13

test('the label is joined to the input', /<label for="seats"/.test(html), true);
test('and the input has that id', /id="seats"/.test(html), true);
test('the error is announced with the field', /aria-describedby="seats-error"/.test(html), true);
test('and that id exists', /id="seats-error"/.test(html), true);
test('the invalid state is exposed', /aria-invalid="true"/.test(html), true);
test('and styled from the same attribute', /\[aria-invalid='true'\]/.test(css), true);
test('the field has real min and max', hasAttr(html, 'min') && hasAttr(html, 'max'), true);
test('focus is visible', /:focus-visible/.test(css), true);
test('the outline is not removed', /outline\s*:\s*none/.test(css), false);
test('there is no placeholder standing in for a label', /placeholder=/.test(html), false);

Output


      

Styling from [aria-invalid='true'] rather than a separate .is-error class is the idea worth keeping: the red border and the announced state come from one source, so they cannot disagree. The last two assertions guard against the two most common mistakes — removing the focus outline, which makes keyboard navigation invisible, and using a placeholder as a label, which vanishes the moment someone types.

Newsletter

New writing, when it lands.

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