slop audit
Blog post 77 / Accessibility

Radio button accessibility: HTML groups and testing

Improve radio button accessibility with native HTML, fieldset and legend, clear labels, keyboard selection, validation, and focused browser tests.

By Slop Audit · 7 min read

Radio button accessibility means a visitor can understand the question, identify each option, choose one answer, and move on using their preferred input method. Start with native HTML radio inputs, a named group, and visible labels. Then test the selected value and keyboard behavior on the deployed form.

A pricing selector or student project form can look correct while exposing unrelated controls, allowing two answers, or submitting a value different from the one shown. This guide focuses on ordinary single-choice form groups. Use checkboxes when visitors can choose several independent answers, and links or buttons when the control performs navigation or an action.

Radio button accessibility launch checklist

  1. Put one question in a fieldset with a concise legend.
  2. Give every option its own visible, associated label.
  3. Use the same name for options belonging to one answer and distinct value attributes.
  4. Keep native inputs visible and keyboard focus easy to see.
  5. Test arrow-key selection, entry into the group, and exit from it.
  6. Check required selection and the value sent to the server.
  7. Repeat with a screen reader, long labels, and a narrow layout.

For validation, network failures, and successful submission of the whole form, use the website form testing checklist. The checks below isolate the radio group's contract before you test that larger flow.

Build an accessible radio group with native HTML

This example asks for one report format. No answer is selected initially, and the choice is required. The form uses GET for a read-only report preview; adapt the destination and method to your application.

<form id="report-options" action="/report-preview" method="get">
  <fieldset class="radio-group">
    <legend>Report format (required)</legend>

    <label class="radio-option" for="format-html">
      <input id="format-html" type="radio"
        name="format" value="html" required>
      <span>HTML report</span>
    </label>

    <label class="radio-option" for="format-text">
      <input id="format-text" type="radio"
        name="format" value="text" required>
      <span>Plain text report</span>
    </label>
  </fieldset>

  <button type="submit">Preview report</button>
</form>

The legend supplies the shared question. Each label identifies an answer and lets a visitor select it by clicking the text. Keep the legend short enough to remain useful when announced alongside an option. W3C's grouping controls tutorial recommends fieldset and legend for related radio controls and demonstrates individually labelled options.

Do not replace the question with an unassociated paragraph just because its typography looks right. Likewise, the group name does not replace each option's label. Inspect the browser's accessibility properties for both the group and its inputs. The accessible name guide explains how labels and ARIA overrides affect the names users receive.

Native radio inputs already provide radio semantics and browser interaction. Adding role="radio" or manually synchronizing aria-checked is unnecessary for these inputs. If you inherit a custom widget made from div elements, restoring native inputs is often a smaller repair than maintaining its keyboard and state code.

Keep name, id, and value separate

These attributes solve different problems:

  • id identifies one element and connects it to a label. Keep IDs unique across the page, including repeated form components.
  • name identifies the submitted field and groups radios with the same form owner. Options for one question share it.
  • value identifies the selected answer in the submitted data. Use explicit, distinct values that your server accepts.

In the example, selecting “Plain text report” produces format=text. If you accidentally use name="html" and name="text", the browser treats the options as separate groups and may allow both to be checked. If two different questions share one name inside the same form, choosing an answer in one question can clear the other.

A fieldset communicates a relationship but does not create mutual exclusion by itself. The inputs' radio type, name, and form ownership determine that behavior. MDN's radio input reference explains grouping, submitted values, and why an omitted value sends the unhelpful default on.

In a controlled React component, keep the checked state derived from one selected value and update it when the input changes. A handler that ignores the new value can make the selection snap back after a click. Compare the DOM's checked state with the eventual request payload when debugging this failure.

Preserve visible controls, labels, and focus

Style the surrounding label to create a comfortable activation area while retaining the native input:

.radio-group {
  min-inline-size: 0;
}

.radio-option {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.75rem;
  cursor: pointer;
}

.radio-option input {
  flex: none;
  margin-block-start: 0.25rem;
  accent-color: #174ea6;
}

.radio-option input:focus-visible {
  outline: 3px solid #174ea6;
  outline-offset: 3px;
}

.radio-option span {
  min-inline-size: 0;
  overflow-wrap: anywhere;
}

Review these colors against your actual background. Keep the checked mark visible as well as any colored card border. Test long translated labels and increased text size; avoid a fixed height that clips the answer text. The touch target size guide covers measuring the clickable area and separating adjacent options.

Avoid hiding the input with display: none and making only a decorative circle visible. That removes the native input from keyboard navigation and the accessibility tree. A fully custom visual treatment needs a deliberate way to preserve the input's semantics, operability, checked state, and visible focus. Retaining the browser's control avoids that extra work for most launch forms.

Test radio button keyboard navigation

Use a fresh page load and start before the group. For an ordinary native group, verify that you can:

  1. Tab into the group and identify the focused option.
  2. Use arrow keys to move between options and change the selected answer.
  3. Confirm the previous answer becomes unchecked.
  4. Use Tab to leave for the next control and Shift+Tab to return.
  5. Select by clicking an option's text, then continue using the keyboard.

Test the browsers your project supports. Initial backward entry into a group with no checked option can differ between browsers. Do not write a universal assertion that Shift+Tab must land on the first unchecked radio. W3C's radio group pattern describes the ordinary keyboard model, notes that native initial-focus behavior can vary, and separates radio groups inside toolbars.

Do not add a custom arrow-key handler to native inputs as routine cleanup. It can compete with the browser's behavior or make one press skip an answer. Use the keyboard accessibility testing checklist to check the focus path before and after the group as well.

With a screen reader, confirm that the shared question, option label, checked state, and group context are understandable. Record the browser and assistive technology used. A role locator or screenshot cannot establish what was announced.

Validate selection without losing the answer

For same-name radio inputs, marking a member required makes selection necessary for that group. Applying it consistently to each member, as above, makes the source easier to maintain. With none checked, native validation should prevent an ordinary form submission. Verify this on the deployed page: a form with novalidate or a custom submission handler may use a different validation path. MDN documents these radio validation rules.

Decide explicitly whether a default answer is appropriate. If users must make a conscious choice, leave the group unchecked and explain the requirement in the legend. If a meaningful default is selected, verify it matches the value your application actually sends.

Server validation must reject missing or unexpected values independently of the browser. Preserve the visitor's answer when another field fails or a request needs retrying. If your application replaces native validation with custom errors, show a specific message near the question, associate it programmatically with the affected controls, and test how users find and correct it.

Add focused radio button testing to Playwright

The following test targets the HTML example on a safe preview route. Set RELEASE_ORIGIN to your test deployment and replace /report-options with the page containing the fixture. It checks visible label activation, selection exclusivity, one arrow-key transition, keyboard exit, and the browser's selected form value without submitting the form.

import { expect, test } from "@playwright/test";

const origin = process.env.RELEASE_ORIGIN ?? "http://localhost:4173";

test("report format supports labels, keyboard, and one value", async ({ page }) => {
  await page.goto(new URL("/report-options", origin).href);

  const group = page.getByRole("group", {
    name: "Report format (required)", exact: true,
  });
  const html = group.getByRole("radio", { name: "HTML report", exact: true });
  const text = group.getByRole("radio", { name: "Plain text report", exact: true });

  await expect(group).toBeVisible();
  await expect(html).not.toBeChecked();
  await expect(text).not.toBeChecked();

  await group.locator('label[for="format-html"]').click();
  await expect(html).toBeChecked();
  await expect(text).not.toBeChecked();

  await html.focus();
  await page.keyboard.press("ArrowDown");
  await expect(text).toBeFocused();
  await expect(text).toBeChecked();
  await expect(html).not.toBeChecked();

  await page.keyboard.press("Tab");
  await expect(page.getByRole("button", { name: "Preview report" }))
    .toBeFocused();

  const values = await page.locator("#report-options").evaluate((element) =>
    new FormData(element as HTMLFormElement).getAll("format"));
  expect(values).toEqual(["text"]);
});

Playwright documents radio selection actions and checked-state assertions. This test deliberately clicks the label and sends a keyboard event because setting checked state alone would miss those interaction failures.

The explicit focus() isolates the arrow-key behavior; keep a separate manual or end-to-end check for entering the group from the preceding control. Add empty-submit, server rejection, reset, and state-restoration cases to the broader form test. FormData confirms the browser's selected value here, while the complete submission test must also verify the application's actual request and result.

Check the surrounding public page with Slop Audit

After the group works, run Slop Audit on the public form page. Review available label findings, axe violation selectors, and the bounded keyboard focus evidence alongside the page's other launch findings. Fix the affected controls, then rescan the same URL to compare results.

Slop Audit does not run the radio-specific test above, verify every arrow-key transition, submit the form, or test screen-reader announcements. Keep that interaction coverage in your browser tests and manual release check. Use the public-page audit to find surrounding markup and readiness problems before sharing the form with visitors.