Components

Select

<div class="govuk-form-group">
  <label class="govuk-label" for="sort">
    Sort by
  </label>
  <select class="govuk-select" id="sort" name="sort">
    <option value="published">Recently published</option>
    <option value="updated" selected>Recently updated</option>
    <option value="views">Most views</option>
    <option value="comments">Most comments</option>
  </select>
</div>
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
id string Required. Id for each select box.
name string Required. Name property for the select.
items array Required. Array of option items for the select. See items.
label object Label text or HTML by specifying value for either text or html keys. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the errorMessage component (e.g. text). See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the anchor tag.
attributes object HTML attributes (for example data attributes) to add to the anchor tag.
Options for items
Name Type Description
value string Value for the option item.
text string Required. Text for the option item.
selected boolean Sets the option as the selected.
disabled boolean Sets the option item as disabled.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. 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 label. If `html` is provided, the `text` argument will be ignored.
for string Required. The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. 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 hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "select/macro.njk" import govukSelect %}

{{ govukSelect({
  id: "sort",
  name: "sort",
  label: {
    text: "Sort by"
  },
  items: [
    {
      value: "published",
      text: "Recently published"
    },
    {
      value: "updated",
      text: "Recently updated",
      selected: true
    },
    {
      value: "views",
      text: "Most views"
    },
    {
      value: "comments",
      text: "Most comments"
    }
  ]
}) }}

When to use this component

The select component should only be used as a last resort in public-facing services because research shows that some users find selects very difficult to use.

How it works

The select component allows users to choose an option from a long list. Before using the select component, try asking users questions which will allow you to present them with fewer options.

Asking questions means you’re less likely to need to use the select component, and can consider using a different solution, such as radios.

There are 2 ways to use the select component. You can use HTML or, if you’re using Nunjucks or the GOV.UK Prototype Kit, you can use the Nunjucks macro.

<div class="govuk-form-group">
  <label class="govuk-label" for="sort">
    Sort by
  </label>
  <select class="govuk-select" id="sort" name="sort">
    <option value="published">Recently published</option>
    <option value="updated" selected>Recently updated</option>
    <option value="views">Most views</option>
    <option value="comments">Most comments</option>
  </select>
</div>
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
id string Required. Id for each select box.
name string Required. Name property for the select.
items array Required. Array of option items for the select. See items.
label object Label text or HTML by specifying value for either text or html keys. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the errorMessage component (e.g. text). See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the anchor tag.
attributes object HTML attributes (for example data attributes) to add to the anchor tag.
Options for items
Name Type Description
value string Value for the option item.
text string Required. Text for the option item.
selected boolean Sets the option as the selected.
disabled boolean Sets the option item as disabled.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. 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 label. If `html` is provided, the `text` argument will be ignored.
for string Required. The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. 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 hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "select/macro.njk" import govukSelect %}

{{ govukSelect({
  id: "sort",
  name: "sort",
  label: {
    text: "Sort by"
  },
  items: [
    {
      value: "published",
      text: "Recently published"
    },
    {
      value: "updated",
      text: "Recently updated",
      selected: true
    },
    {
      value: "views",
      text: "Most views"
    },
    {
      value: "comments",
      text: "Most comments"
    }
  ]
}) }}

Research on this component

Watch a video about how some users struggle with selects.

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 ‘Select’ on GitHub