Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Group radio buttons with fieldset and legend in HTML

15 A radio group that behaves like a group

fair

Offer three mutually exclusive choices. Group them so assistive technology announces the question as well as the options, and make exactly one of them selected to begin with.

Stuck? Three hints, smallest first
  1. 1. Radios are exclusive only if they share the same name. Different names means three independent toggles.
  2. 2. <fieldset> with a <legend> is what names the group.
  3. 3. Each radio still needs its own id and its own <label for>.

index.html

<fieldset class="seats">
  <legend>Seat class</legend>

  <p class="opt">
    <input id="cls-std" name="seatclass" type="radio" value="standard" checked>
    <label for="cls-std">Standard</label>
  </p>

  <p class="opt">
    <input id="cls-exec" name="seatclass" type="radio" value="executive">
    <label for="cls-exec">Executive</label>
  </p>

  <p class="opt">
    <input id="cls-sleep" name="seatclass" type="radio" value="sleeper">
    <label for="cls-sleep">Sleeper</label>
  </p>
</fieldset>

style.css

.seats {
  max-width: 280px;
  margin: 0;
  padding: 10px 14px 14px;
  border: 1px solid #c9cad1;
  border-radius: 8px;
}

.seats legend {
  padding: 0 6px;
  font-weight: 600;
}

.opt {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px;
}

.opt input {
  accent-color: #3055d8;   /* tints the native control, no rebuild needed */
  width: 16px;
  height: 16px;
}

.opt input:focus-visible {
  outline: 2px solid #17171a;
  outline-offset: 2px;
}

.opt label { cursor: pointer; }
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 15

const names = [...html.matchAll(/name="([^"]+)"/g)].map((m) => m[1]);
const distinct = [...new Set(names)];

test('three radios', (html.match(/type="radio"/g) || []).length, 3);
test('they all share one name, which is what makes them exclusive', distinct, ['seatclass']);
test('each has its own id', new Set([...html.matchAll(/id="([^"]+)"/g)].map((m) => m[1])).size, 3);
test('and its own label', (html.match(/<label for="/g) || []).length, 3);
test('the group is named by a legend inside a fieldset', hasTag(html, 'fieldset') && hasTag(html, 'legend'), true);
test('exactly one is checked to start with', (html.match(/\bchecked\b/g) || []).length, 1);
test('the native control is tinted rather than rebuilt', /accent-color/.test(css), true);
test('focus is visible on the control itself', /input:focus-visible/.test(css), true);

Output


      

The shared name is the mechanism, not a convention: change one of them and you get three checkboxes wearing round costumes, all independently selectable. <legend> is the other half — without it a screen reader reads “Standard, radio button, one of three” and never says what the question was. accent-color is worth knowing because the usual alternative is hiding the input and drawing a fake one, which throws away keyboard behaviour, the focus ring and Windows high contrast mode in one go.

Newsletter

New writing, when it lands.

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