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>
for="wa"on the label exactly matchesid="wa"on the input. That connection is what becomes the field's name in the accessibility tree. It also makes a click on the label's words move focus to the field.- Without a label, Chromium uses the placeholder as a fallback name. The first version of Mrs. Sri's form only had
placeholder="07700 900123", so in Chromium's tree this field was named "07700 900123": an example number, not what's being asked for. In the same way, the Full name field was named "Hadi Santoso", the example in its placeholder. The placeholder also leaves the screen as soon as the guest types.
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>
- Without the
<fieldset>, each radio in the tree is onlyradio "2 nights". With<fieldset>and<legend>, the radios sit insidegroup "Number of nights", so a screen reader can say what the choice is for. - The shared
name="nights"makes the three one radio group. Only one can be checked, and Tab stops once in the group, on the checked radio. The left and right arrow keys move the choice. We checked this in Chromium, WebKit and Firefox: the same in all three. - The night buttons from earlier lessons (
<button aria-pressed>) are fine too. Radios are used here because this choice is part of a form that gets sent.
Errors that can be heard
A field in error needs three things (the pattern already appeared in the feedback lesson):
aria-invalid="true"marks the field as invalid. Set it only when there really is an error.aria-describedby="wa-help wa-error"connects the example and the error message to the field, through theidof those two paragraphs. In Chromium's tree the field's description becomes "Example: 07700 900123 or +44 7700 900123 This number is 7 digits. ...". That description is read after the field's name, when the field gets focus.role="alert"on the error paragraph makes its content read out as soon as it appears. The paragraph is there from the start, empty, and is filled when an error happens.
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>
- When the submit button is pressed, the code removes the
hiddenattribute (which hides the summary), then callsfocus()on this summary to move focus there.tabindex="-1"lets code focus the<div>, so a screen reader reads its content at once. - Each link points to its field's
id. We tried it in Chromium, Firefox and WebKit: pressing the#walink moves focus to the number field. - The message beside each field stays, with exactly the same sentence as in the summary.
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>
href="#main"points at anid="main"that really exists. If the id isn't there, the link goes nowhere.transform: translateY(-200%)moves the link up until it is off the screen..skip-link:focusbrings it back when the link is focused, so it doesn't bother mouse users. Don't usedisplay: none(which hides an element completely): an element like that can't be focused at all.- We tested it in Chromium, WebKit and Firefox: Tab, Enter, then one more Tab already reaches the night choice. Without
tabindex="-1", focus stays on<body>(the whole page), but the next Tab still lands inside main. Withtabindex="-1", focus really moves to main.
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;
}
:focus-visibleapplies when the browser judges the focus should be visible. What we found in Chromium and Firefox: Tab always triggers it. A mouse click on a button, link or radio doesn't. A click in a text field does, because people need to know where the cursor is. Safari on Mac doesn't even focus a clicked button.:focusapplies to every focus, clicks included. Because its ring also appears on a click, designers often remove it, and what goes missing is the ring keyboard users need.
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
- A placeholder as the only name. Chromium uses it when there is nothing else. But the name becomes an example, and it disappears while the guest types. Use
<label for>. - A
forandidthat don't match, or a skip link to a missing id. The connection breaks without any message. The exercise checks that everyforandhref="#..."points at an id that exists. requiredandaria-required="true"on the same element. Pick one.- Radios without
<fieldset>and<legend>. "2 nights" is heard with no context. outline: nonewith nothing in its place. Use:focus-visiblewith its own outline.
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.