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, you may need to group a set of text inputs into a single fieldset when asking for an address.

Open this default fieldset example in new window
Copy default fieldset code
<fieldset class="hse-fieldset">
  <legend class="hse-fieldset__legend hse-fieldset__legend--l">
    <h1 class="hse-fieldset__heading">
      What is your address?
    </h1>
  </legend>

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

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

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

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

  <div class="hse-form-group">
    <label class="hse-label" for="address-postcode">
      Postcode
    </label>
    <input class="hse-input hse-input--width-10" id="address-postcode" name="address-postcode" type="text">
  </div>

</fieldset>
Close default fieldset code
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.

Nunjucks arguments for default fieldset
Name Type Required Description
Name describedBy Type string Required false Description One or more element IDs to add to the `aria-describedby` attribute, used to provide additional descriptive information for screenreader users.
Name legend Type object Required false Description Options for the legend
Name legend{}.text Type string Required true Description If `html` is set, this is not required. Text to use within the legend. If `html` is provided, the `text` argument will be ignored.
Name legend{}.html Type string Required true Description If `text` is set, this is not required. HTML to use within the legend. If `html` is provided, the `text` argument will be ignored.
Name legend{}.classes Type string Required false Description Classes to add to the legend.
Name legend{}.isPageHeading Type boolean Required false Description Whether the legend also acts as the heading for the page.
Name classes Type string Required false Description Classes to add to the fieldset container.
Name attributes Type object Required false Description HTML attributes (for example data attributes) to add to the fieldset container.
Copy default fieldset code
{% from 'fieldset/macro.njk' import fieldset %}
{% from "input/macro.njk" import input %}

{% call fieldset({
  legend: {
    "text": "What is your address?",
    "classes": "hse-fieldset__legend--l",
    "isPageHeading": true
  }
}) %}

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

  {{ input({
    label: {
      "html": '<span class="hse-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": "hse-u-width-two-thirds",
    "id": "address-town",
    "name": "address-town"
  }) }}

  {{ input({
    label: {
      "text": "County"
    },
    "classes": "hse-u-width-two-thirds",
    "id": "address-county",
    "name": "address-county"
  }) }}

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

{% endcall %}
Close default fieldset code

How to use a fieldset

The first element in a fieldset must be a legend which describes the group of inputs.

This could be:

  • a question like "what is your address?"
  • a statement like "personal details"

Ask 1 question per page so users of screen readers only hear the contents once.

Open this as heading fieldset example in new window
Copy as heading fieldset code
<fieldset class="hse-fieldset">
  <legend class="hse-fieldset__legend hse-fieldset__legend--xl">
    <h1 class="hse-fieldset__heading">
      What is your address?
    </h1>
  </legend>
</fieldset>
Close as heading fieldset code
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.

Nunjucks arguments for as heading fieldset
Name Type Required Description
Name describedBy Type string Required false Description One or more element IDs to add to the `aria-describedby` attribute, used to provide additional descriptive information for screenreader users.
Name legend Type object Required false Description Options for the legend
Name legend{}.text Type string Required true Description If `html` is set, this is not required. Text to use within the legend. If `html` is provided, the `text` argument will be ignored.
Name legend{}.html Type string Required true Description If `text` is set, this is not required. HTML to use within the legend. If `html` is provided, the `text` argument will be ignored.
Name legend{}.classes Type string Required false Description Classes to add to the legend.
Name legend{}.isPageHeading Type boolean Required false Description Whether the legend also acts as the heading for the page.
Name classes Type string Required false Description Classes to add to the fieldset container.
Name attributes Type object Required false Description HTML attributes (for example data attributes) to add to the fieldset container.
Copy as heading fieldset code
{% from 'fieldset/macro.njk' import fieldset %}

{{ fieldset({
  "legend": {
    "text": "What is your address?",
    "classes": "hse-fieldset__legend--xl",
    "isPageHeading": true
  }
}) }}
Close as heading fieldset code

Accessibility

On question pages containing a group of inputs, include the question as the legend. This helps users of screen readers understand that the inputs are all related to that question.

Put any help text that cannot be written as hint text in the legend. Keep it as short as possible.

Updated: August 2023