|
| 1 | +--- |
| 2 | +title: Group form fields |
| 3 | +layout: default |
| 4 | +parent: Web forms |
| 5 | +nav_order: 5 |
| 6 | +description: Use fieldset and legend to group form fields. |
| 7 | +--- |
| 8 | + |
| 9 | +# Fieldset and legend to group form fields |
| 10 | + |
| 11 | +Group form fields that belong together inside a `<fieldset>` with a descriptive `<legend>`. The advantage to this is that the form fields are grouped together both visually and semantically, making the design match the code. |
| 12 | + |
| 13 | +The `fieldset` and `legend` elements are the native HTML way to group fields. When a screen reader enters a fieldset, the `legend` of that group will be announced. This is a helpful way of grouping related fields together, such as differentiating between an order address and a shipping address; but is critical when grouping radio buttons and checkboxes. |
| 14 | + |
| 15 | + |
| 16 | +<img src="{{site.baseurl}}/assets/images/fieldset-radio-buttons.png" alt="Screenshot of grouped radio buttons that let you select the best color"> |
| 17 | + |
| 18 | + |
| 19 | +```html |
| 20 | +<fieldset> |
| 21 | + |
| 22 | + <legend>Which is the best color?</legend> |
| 23 | + |
| 24 | + <input name="colorOption" type="radio" id="purple" value="purple" /> |
| 25 | + <label for="purple">Purple</label> |
| 26 | + |
| 27 | + <input name="colorOption" type="radio" id="aubergine" value="aubergine" /> |
| 28 | + <label for="aubergine">Aubergine</label> |
| 29 | + |
| 30 | + <input name="colorOption" type="radio" id="magenta" value="magenta" /> |
| 31 | + <label for="magenta">Magenta</label> |
| 32 | + |
| 33 | + <input name="colorOption" type="radio" id="all" value="all" /> |
| 34 | + <label for="all">All of the above</label> |
| 35 | + |
| 36 | +</fieldset> |
| 37 | +``` |
| 38 | + |
| 39 | +WCAG requires that the structure and relationships are programmatically determinable, and fieldset/legend is one way to meet that. The same can be achieved by using `role="group"` and `aria-labelledby`. But always keep the [first rule of ARIA use](https://www.w3.org/TR/using-aria/#rule1) in mind: |
| 40 | + |
| 41 | +> If you can use a native HTML element or attribute with the semantics and behavior you require already built in, instead of re-purposing an element and adding an ARIA role, state or property to make it accessible, then do so. |
| 42 | +
|
| 43 | +## Example using ARIA (not preferred) |
| 44 | + |
| 45 | +```html |
| 46 | +<div role="group" aria-labelledby="dummy-legend"> |
| 47 | + |
| 48 | + <h2 id="dummy-legend">Which is the best color?</h2> |
| 49 | + |
| 50 | + <input name="colorOption" type="radio" id="purple" value="purple" /> |
| 51 | + <label for="purple">Purple</label> |
| 52 | + |
| 53 | + <input name="colorOption" type="radio" id="aubergine" value="aubergine" /> |
| 54 | + <label for="aubergine">Aubergine</label> |
| 55 | + |
| 56 | + <input name="colorOption" type="radio" id="magenta" value="magenta" /> |
| 57 | + <label for="magenta">Magenta</label> |
| 58 | + |
| 59 | + <input name="colorOption" type="radio" id="all" value="all" /> |
| 60 | + <label for="all">All of the above</label> |
| 61 | + |
| 62 | +</div> |
| 63 | +``` |
| 64 | + |
| 65 | +## WCAG Success Criteria for naming grouped form fields |
| 66 | + |
| 67 | +Naming grouped form fields is necessary to meet the WCAG success criteria: |
| 68 | + |
| 69 | +- [1.3.1 Info and relationships](https://www.w3.org/WAI/WCAG22/quickref/#info-and-relationships) (Level A). |
| 70 | +- [3.3.2 Labels or Instructions](https://www.w3.org/WAI/WCAG22/quickref/#labels-or-instructions) (Level AA). |
| 71 | + |
| 72 | +## Resources |
| 73 | + |
| 74 | +- W3C: [Grouping Controls](https://www.w3.org/WAI/tutorials/forms/grouping/) |
| 75 | +- NL Design System: [Guidelines for web forms (Dutch content)](https://nldesignsystem.nl/richtlijnen/formulieren/). |
| 76 | +- MDN: [The Field Set element](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/fieldset). |
0 commit comments