Components

Fieldset

Use the fieldset component to group related form inputs.

When to use this component

Use the fieldset component when you need to show a relationship between multiple form inputs. For example, you may need to group a set of text inputs into a single fieldset when asking for an address.

<fieldset class="govuk-fieldset">
  <legend class="govuk-fieldset__legend govuk-fieldset__legend--xl">
    <h1 class="govuk-fieldset__heading">
      What is your address?
    </h1>
  </legend>

  <div class="govuk-form-group">
    <label class="govuk-label" for="address-line-1">
      Building and street
      <span class="govuk-visually-hidden">line 1 of 2</span>
    </label>
    <input class="govuk-input" id="address-line-1" name="address-line-1" type="text">
  </div>

  <div class="govuk-form-group">
    <label class="govuk-label" for="address-line-2">
      <span class="govuk-visually-hidden">Building and street line 2 of 2</span>
    </label>
    <input class="govuk-input" id="address-line-2" name="address-line-2" type="text">
  </div>

  <div class="govuk-form-group">
    <label class="govuk-label" for="address-town">
      Town or city
    </label>
    <input class="govuk-input govuk-!-width-two-thirds" id="address-town" name="address-town" type="text">
  </div>

  <div class="govuk-form-group">
    <label class="govuk-label" for="address-county">
      County
    </label>
    <input class="govuk-input govuk-!-width-two-thirds" id="address-county" name="address-county" type="text">
  </div>

  <div class="govuk-form-group">
    <label class="govuk-label" for="address-postcode">
      Postcode
    </label>
    <input class="govuk-input govuk-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.

Primary options
Name Type Description
describedBy string Text or element id to add to the aria-describedby attribute to provide description of the group of fields for screenreader users.
legend object Options for the legend See legend.
classes string Classes to add to the fieldset container.
attributes object HTML attributes (for example data attributes) to add to the fieldset container.
caller nunjucks-block Not strictly a parameter but Nunjucks code convention. Using a call block enables you to call a macro with all the text inside the tag. This is helpful if you want to pass a lot of content into a macro. To use it, you will need to wrap the entire fielset component in a call block. See checkboxes component for an example.
Options for legend
Name Type Description
text string Required. If html is set, this is not required. Text to use within the legend. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the legend. If html is provided, the text argument will be ignored.
classes string Classes to add to the legend.
isPageHeading boolean Whether the legend also acts as the heading for the page.
{% from "input/macro.njk" import govukInput %}
{% from "fieldset/macro.njk" import govukFieldset %}

{% call govukFieldset({
  legend: {
    text: "What is your address?",
    classes: "govuk-fieldset__legend--xl",
    isPageHeading: true
  }
}) %}

  {{ govukInput({
    label: {
      html: 'Building and street <span class="govuk-visually-hidden">line 1 of 2</span>'
    },
    id: "address-line-1",
    name: "address-line-1"
  }) }}

  {{ govukInput({
    label: {
      html: '<span class="govuk-visually-hidden">Building and street line 2 of 2</span>'
    },
    id: "address-line-2",
    name: "address-line-2"
  }) }}

  {{ govukInput({
    label: {
      text: "Town or city"
    },
    classes: "govuk-!-width-two-thirds",
    id: "address-town",
    name: "address-town"
  }) }}

  {{ govukInput({
    label: {
      text: "County"
    },
    classes: "govuk-!-width-two-thirds",
    id: "address-county",
    name: "address-county"
  }) }}

  {{ govukInput({
    label: {
      text: "Postcode"
    },
    classes: "govuk-input--width-10",
    id: "address-postcode",
    name: "address-postcode"
  }) }}

{% endcall %}

If you’re using the examples or macros for radios, checkboxes or date input, the fieldset will already be included.

How it works

The first element inside a fieldset must be a legend which describes the group of inputs. This could be a question, such as ‘What is your current address?’ or a statement like ‘Personal details’.

If you’re asking just one question per page as recommended, you can set the contents of the <legend> as the page heading, as shown in the 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.

<fieldset class="govuk-fieldset">
  <legend class="govuk-fieldset__legend govuk-fieldset__legend--xl">
    <h1 class="govuk-fieldset__heading">
      Legend as page heading
    </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.

Primary options
Name Type Description
describedBy string Text or element id to add to the aria-describedby attribute to provide description of the group of fields for screenreader users.
legend object Options for the legend See legend.
classes string Classes to add to the fieldset container.
attributes object HTML attributes (for example data attributes) to add to the fieldset container.
caller nunjucks-block Not strictly a parameter but Nunjucks code convention. Using a call block enables you to call a macro with all the text inside the tag. This is helpful if you want to pass a lot of content into a macro. To use it, you will need to wrap the entire fielset component in a call block. See checkboxes component for an example.
Options for legend
Name Type Description
text string Required. If html is set, this is not required. Text to use within the legend. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the legend. If html is provided, the text argument will be ignored.
classes string Classes to add to the legend.
isPageHeading boolean Whether the legend also acts as the heading for the page.
{% from "fieldset/macro.njk" import govukFieldset %}

{{ govukFieldset({
  legend: {
    text: "Legend as page heading",
    classes: "govuk-fieldset__legend--xl",
    isPageHeading: true
  }
}) }}

On question pages containing a group of inputs, including the question as the legend helps users of screenreaders to understand that the inputs are all related to that question.

Include any general help text which is important for filling in the form and cannot be written as hint text in the legend, but try to keep it as concise as possible.

Research on this component

If you’ve used this component, get in touch to share your user research findings.

Get in touch

If you’ve got a question, idea or suggestion share it in #govuk-design-system on cross-government Slack (open in app) or email the Design System team on govuk-design-system-support@digital.cabinet-office.gov.uk

Discuss ‘Fieldset’ on GitHub