Accessible Forms & Navigation — UI/UX

Every field needs a <label for> that exactly matches its id. Without one, Chromium uses the placeholder (the example text inside the field) as the name…

Try filling in an online form without touching the mouse, using only Tab and Enter. Often you have to go through every menu link before you reach the first field. Then you no longer know where you are: where's the focus now?

Forms and navigation are where people do the most on a page: choosing, typing, pressing, moving to the next part. Picture the paper form on the homestay's front desk. Every box has printed words next to it. The three choices for the number of nights sit inside one frame titled "Number of nights". Notes about mistakes are written right beside the box. A regular guest may also jump straight to the part they need. On the web, the printed words are a <label>, the frame is a <fieldset>, the note about the mistake is aria-describedby, and the shortcut is a skip link.

We'll work on the Terrace Room booking form, the same one as the Lab on this page. Whenever the text says "in Chromium's tree", we read it from Chromium 147's accessibility tree, for every field and in every state of the Lab.

Labels that are connected

This is the WhatsApp number field on Mrs. Sri's form, with its label. The aria-... attributes in this code are covered one by one in the next sections.

<label for="wa">WhatsApp number</label>
<input id="wa" name="wa" type="tel" autocomplete="tel" aria-required="true" aria-invalid="true" aria-describedby="wa-help wa-error">
<p id="wa-help" class="help">Example: 07700 900123 or +44 7700 900123</p>
<p id="wa-error" class="error" role="alert">This number is 7 digits. A mobile number starts with 07 and is 11 digits long.</p>

In the exercise, the form has <input type="text" id="name"> and <input type="email" id="email"> with placeholders only. Add <label for="name">Name</label> and <label for="email">Email</label>. Each for must point at an id that really exists.

Required: required or aria-required

A red asterisk can only be seen. So that a screen reader also reads out that the field is required, put an attribute on the input itself. Choose one of these two ways:

Attribute In Chromium's tree Does the browser check too?
required required: true yes, unless the form has novalidate
aria-required="true" required: true no, you write the check yourself

novalidate is a form attribute that turns off the browser's own checks. Mrs. Sri's form checks the WhatsApp number itself (the form design lesson), so it uses aria-required="true" on Full name and WhatsApp number. The optional email doesn't get that attribute. Use only one: W3C's ARIA in HTML specification asks that aria-required="true" not be put on an element that already has required. In the exercise, give the required inputs aria-required="true".

Grouped choices: fieldset and legend

This is the choice of nights as radio buttons (round choice buttons), wrapped in one <fieldset> with a <legend> as its title:

<fieldset class="nights">
  <legend>Number of nights</legend>
  <label><input type="radio" name="nights" value="1"> 1 night</label>
  <label><input type="radio" name="nights" value="2" checked> 2 nights</label>
  <label><input type="radio" name="nights" value="3"> 3 nights</label>
</fieldset>

Errors that can be heard

A field in error needs three things (the pattern already appeared in the feedback lesson):

This meets WCAG 3.3.1 (Error Identification): the field in error is marked, and the error is described in text.

Many errors at once. role="alert" fits one field the guest has just left. An alert is read out by interrupting whatever is being read. So if the guest presses the submit button with two wrong fields, two alerts appear together and interrupt each other. The GOV.UK Design System (the design guide for UK government sites) uses an error summary at the very top of <main>:

<div class="error-summary" id="summary" tabindex="-1" hidden>
  <h2>There are 2 things to fix</h2>
  <ul>
    <li><a href="#name">Enter your full name.</a></li>
    <li><a href="#wa">This number is 7 digits. A mobile number starts with 07 and is 11 digits long.</a></li>
  </ul>
</div>

Skip link

Every page on Mrs. Sri's site starts with four navigation links. Keyboard users have to pass them on every page: from the top of the page, only the 5th Tab reaches the night choice. A skip link is the first link on the page, jumping straight to the main content (WCAG 2.4.1, Bypass Blocks):

<a href="#main" class="skip-link">Skip to main content</a>
<nav aria-label="Main navigation">...</nav>
<main id="main" tabindex="-1">...</main>

<style>
  .skip-link { position: absolute; left: 8px; top: 8px; padding: 8px 12px; color: #fff; background: #1f2937; transform: translateY(-200%); }
  .skip-link:focus { transform: none; }
  main:focus { outline: none; }
</style>

In the exercise, you also make a skip link: add <a href="#main-content" class="skip-link">Skip to main content</a> at the very top, before the <nav>. <main id="main-content"> is already in the starter. Don't change its id.

Visible focus, a sensible order

Say the focus ring on Mrs. Sri's booking page was removed with outline: none to look "tidier". Mr. Hadi uses a keyboard and presses Tab 5 times from the top of the page. Where is the focus, and how does he know? Guess first.

The focus order follows the order of the elements in the HTML (WCAG 2.4.3, Focus Order). On Mrs. Sri's page the order is: Home, Rooms, Location, Bookings, the night choice, Full name, WhatsApp number, Email, the book button. Don't force an order with a positive tabindex (tabindex="1" and so on). Arrange the HTML instead.

Visible focus (WCAG 2.4.7, Focus Visible): a keyboard user must always know where the focus is. This CSS draws a 3px focus ring, 2px away from the element (outline-offset):

a:focus-visible, input:focus-visible, button:focus-visible {
  outline: 3px solid #1f2937;
  outline-offset: 2px;
}

So the answer to the guess: the 5th Tab is on the "2 nights" radio (four links, then the radio group, which is a single stop), but Mr. Hadi can't tell, because no ring is drawn.

⚠️ Fields without names, focus without a trace

TL;DR: Every field needs a <label for> that exactly matches its id. Without one, Chromium uses the placeholder (the example text inside the field) as the name ("07700 900123"). Required fields are marked with required (the browser checks too) or aria-required="true" (only read out by a screen reader), never both. Radio choices (round choice buttons) go in a <fieldset> with a <legend> as the title ("Number of nights"). One radio group = one Tab stop, and the arrow keys move the choice. For errors: aria-invalid="true" marks the wrong field, aria-describedby connects the example and the error message to the field, and role="alert" reads out an error paragraph that exists from the start as soon as it is filled (WCAG 3.3.1). A skip link is a link that jumps straight to the main content. It is written <a href="#main" class="skip-link"> at the very top, points at an id that really exists on <main>, and is hidden until focused (WCAG 2.4.1). Focus order = HTML order, never forced with a positive tabindex (2.4.3). Focus must be visible (2.4.7): :focus-visible applies on Tab and usually not on a mouse click, so never remove the outline without a replacement.

⚡ Labels that are connected

Write:

<label for="wa">WhatsApp number</label>
<input id="wa" name="wa" type="tel" autocomplete="tel" aria-required="true" aria-invalid="true" aria-describedby="wa-help wa-error">
<p id="wa-error" class="error" role="alert">This number is 7 digits. A mobile number starts with 07 and is 11 digits long.</p>

Meaning: for on the <label> exactly matches id on the input, and that connection becomes the field's name. Without a label, Chromium uses the placeholder (the example text inside the field) as the name: with the placeholder "07700 900123", the field is named "07700 900123", an example number, not what is being asked for. aria-invalid marks the wrong field, aria-describedby connects its error message, and role="alert" reads that message out as soon as it appears.

⚡ Choices inside one frame

Write:

<fieldset class="nights">
  <legend>Number of nights</legend>
  <label><input type="radio" name="nights" value="1"> 1 night</label>
  <label><input type="radio" name="nights" value="2" checked> 2 nights</label>
</fieldset>

Meaning: A radio is a round choice button. Without a <fieldset>, a radio is only heard as "2 nights", with no context. With a <legend> as the title ("Number of nights"), a screen reader can say what the choice is for. One radio group is one Tab stop, and the arrow keys move the choice.

⚡ A skip link and visible focus

Guess first: Mrs. Sri's page starts with four menu links, then the radio group for the nights. Its focus ring was removed with outline: none to look "tidier". Mr. Hadi, a keyboard user, presses Tab 5 times from the top. Where is the focus, and how does he know?

On the "2 nights" radio: four links, then the radio group, which is a single stop. But Mr. Hadi can't tell, because no ring is drawn.

Meaning: Use :focus-visible (it applies when focus needs to be visible, such as after Tab) with its own outline, and never remove the outline without a replacement. A skip link <a href="#main" class="skip-link"> at the very top jumps past the navigation, straight to the main content. Its href must point at an id that really exists.

What you will learn