A <fieldset>
groups several related form-fields (and their <label>
s) together within a single <form>
. A <legend>
inside of a <fieldset>
provides a caption that describes the field grouping.
The default browser-defined styling for a <fieldset>
and <legend>
doesn't fit with our desired style, so SWAN features its own Fieldset component that overrides the default. This lets you make use of the nice accessibility benefits of <fieldset>
and <legend>
without having to use the browser's default visuals.
Usage
The preview has been updated.
For comparison, here is a <fieldset>
with the default styling:
The preview has been updated.
Accessibility
- All
<fieldset>
tags must have a<legend>
tag that labels them, for accessibility. If the information in the legend is superfluous to sighted users, the legend can be hidden on screens by usingVisuallyHidden
see docs here. - The
<legend>
cannot be (or contain) a heading (H1-H6) tag. If the page structure requires a heading to label the form fields, don't use a<fieldset>
tag. Instead, group them using a wrapper tag that has therole="radiogroup"
attribute (for radio buttons) orrole="group"
attribute (for everything else), plus anaria-labelledby
attribute whose value is the id of the heading.