Form elements
-
Fieldset
Use a fieldset to group related form inputs.
When to use a fieldset
Use a fieldset when you need to show a relationship between multiple form inputs. For example, to group a set of text inputs into a single fieldset when asking for an address or a date.
<fieldset class="nhsuk-fieldset">
<legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--l">
<h1 class="nhsuk-fieldset__heading">
What is your address?
</h1>
</legend>
<div class="nhsuk-form-group">
<label class="nhsuk-label" for="address-line-1">
Building and street <span class="nhsuk-u-visually-hidden">line 1 of 2</span>
</label>
<input class="nhsuk-input" id="address-line-1" name="address-line-1" type="text">
</div>
<div class="nhsuk-form-group">
<label class="nhsuk-label" for="address-line-2">
<span class="nhsuk-u-visually-hidden">Building and street line 2 of 2</span>
</label>
<input class="nhsuk-input" id="address-line-2" name="address-line-2" type="text">
</div>
<div class="nhsuk-form-group">
<label class="nhsuk-label" for="address-town">
Town or city
</label>
<input class="nhsuk-input nhsuk-u-width-two-thirds" id="address-town" name="address-town" type="text">
</div>
<div class="nhsuk-form-group">
<label class="nhsuk-label" for="address-county">
County
</label>
<input class="nhsuk-input nhsuk-u-width-two-thirds" id="address-county" name="address-county" type="text">
</div>
<div class="nhsuk-form-group">
<label class="nhsuk-label" for="address-postcode">
Postcode
</label>
<input class="nhsuk-input nhsuk-input--width-10" id="address-postcode" name="address-postcode" type="text">
</div>
</fieldset>
Nunjucks macro options
Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.
Some options are required for the macro to work; these are marked as "Required" in the option description.
If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.
Name | Type | Required | Description |
---|---|---|---|
describedBy | string | false | One or more element IDs to add to the `aria-describedby` attribute, used to provide additional descriptive information for screenreader users. |
legend | object | false | Options for the legend |
legend{}.text | string | true | If `html` is set, this is not required. Text to use within the legend. If `html` is provided, the `text` argument will be ignored. |
legend{}.html | string | true | If `text` is set, this is not required. HTML to use within the legend. If `html` is provided, the `text` argument will be ignored. |
legend{}.classes | string | false | Classes to add to the legend. |
legend{}.isPageHeading | boolean | false | Whether the legend also acts as the heading for the page. |
classes | string | false | Classes to add to the fieldset container. |
attributes | object | false | HTML attributes (for example data attributes) to add to the fieldset container. |
{% from 'fieldset/macro.njk' import fieldset %}
{% from "input/macro.njk" import input %}
{% call fieldset({
legend: {
text: "What is your address?",
classes: "nhsuk-fieldset__legend--l",
isPageHeading: true
}
}) %}
{{ input({
label: {
html: 'Building and street <span class="nhsuk-u-visually-hidden">line 1 of 2</span>'
},
id: "address-line-1",
name: "address-line-1"
}) }}
{{ input({
label: {
html: '<span class="nhsuk-u-visually-hidden">Building and street line 2 of 2</span>'
},
id: "address-line-2",
name: "address-line-2"
}) }}
{{ input({
label: {
text: "Town or city"
},
classes: "nhsuk-u-width-two-thirds",
id: "address-town",
name: "address-town"
}) }}
{{ input({
label: {
text: "County"
},
classes: "nhsuk-u-width-two-thirds",
id: "address-county",
name: "address-county"
}) }}
{{ input({
label: {
text: "Postcode"
},
classes: "nhsuk-input--width-10",
id: "address-postcode",
name: "address-postcode"
}) }}
{% endcall %}
How to use a fieldset
The 1st element in a fieldset must be a <legend>
which describes the group of inputs. This could be a question, such as "What is your address?" or a statement like "Personal details".
If you are asking just 1 question per page as we recommend, you can set the contents of the <legend>
as the page heading. You can see an example below. This is good practice as it means that users of screen readers will only hear the contents once.
Read more about why and how to set legends as headings on the GOV.UK Design System.
<fieldset class="nhsuk-fieldset">
<legend class="nhsuk-fieldset__legend nhsuk-fieldset__legend--xl">
<h1 class="nhsuk-fieldset__heading">
What is your address?
</h1>
</legend>
</fieldset>
Nunjucks macro options
Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.
Some options are required for the macro to work; these are marked as "Required" in the option description.
If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.
Name | Type | Required | Description |
---|---|---|---|
describedBy | string | false | One or more element IDs to add to the `aria-describedby` attribute, used to provide additional descriptive information for screenreader users. |
legend | object | false | Options for the legend |
legend{}.text | string | true | If `html` is set, this is not required. Text to use within the legend. If `html` is provided, the `text` argument will be ignored. |
legend{}.html | string | true | If `text` is set, this is not required. HTML to use within the legend. If `html` is provided, the `text` argument will be ignored. |
legend{}.classes | string | false | Classes to add to the legend. |
legend{}.isPageHeading | boolean | false | Whether the legend also acts as the heading for the page. |
classes | string | false | Classes to add to the fieldset container. |
attributes | object | false | HTML attributes (for example data attributes) to add to the fieldset container. |
{% from 'fieldset/macro.njk' import fieldset %}
{{ fieldset({
legend: {
text: "What is your address?",
classes: "nhsuk-fieldset__legend--xl",
isPageHeading: true
}
}) }}
Accessibility
On question pages containing a set of inputs, group them using a <fieldset>
and include the question as the <legend>
. This helps screen reader users understand that the inputs are all related to that question.
Include any important general help text in the legend if it would help the user fill in the form, and you cannot write it as hint text. Keep it as short as possible because screen readers may repeat the legend for each input in the fieldset.
Research
If you have used this component, get in touch to share your user research findings.
Help us improve this guidance
Share insights or feedback and take part in the discussion. We use GitHub as a collaboration space. All the information on it is open to the public.
Read more about how to feedback or share insights.
If you have any questions, get in touch with the service manual team.
Updated: July 2024