Text input
<div class="govuk-form-group">
<label class="govuk-label" for="event-name">
Event name
</label>
<input class="govuk-input" id="event-name" name="event-name" type="text">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Event name"
},
id: "event-name",
name: "event-name"
}) }}
When to use this component
Use the text input component when you need to let users enter text that’s no longer than a single line, such as their name or phone number.
When not to use this component
Do not use the text input component if you need to let users enter longer answers that might span multiple lines. In this case, you should use the textarea component.
How it works
There are 2 ways to use the text input 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="event-name">
Event name
</label>
<input class="govuk-input" id="event-name" name="event-name" type="text">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Event name"
},
id: "event-name",
name: "event-name"
}) }}
Label text inputs
All text inputs must have visible labels; placeholder text is not an acceptable replacement for a label as it vanishes when users start typing.
Labels should be aligned above the text input they refer to. They should be short, direct and written in sentence case. Do not use colons at the end of labels.
If you’re asking just one question per page as recommended, you can set the contents of the <label>
as the page heading. 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.
Use appropriately-sized text inputs
Help users understand what they should enter by making text inputs the right size for the content they’re intended for.
By default, the width of text inputs is fluid and will fit the full width of the container they are placed into.
If you want to make the input smaller, you can either use a fixed width input, or use the width override classes to create a smaller, fluid width input.
Fixed width inputs
Use fixed width inputs for content that has a specific, known length. Postcode inputs should be postcode-sized, telephone number inputs should be telephone number-sized.
On fixed width inputs, the width will remain fixed on all screens unless it is wider than the viewport, in which case it will shrink to fit.
<div class="govuk-form-group">
<label class="govuk-label" for="width-20">
20 character width
</label>
<input class="govuk-input govuk-input--width-20" id="width-20" name="width-20" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="width-10">
10 character width
</label>
<input class="govuk-input govuk-input--width-10" id="width-10" name="width-10" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="width-5">
5 character width
</label>
<input class="govuk-input govuk-input--width-5" id="width-5" name="width-5" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="width-4">
4 character width
</label>
<input class="govuk-input govuk-input--width-4" id="width-4" name="width-4" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="width-3">
3 character width
</label>
<input class="govuk-input govuk-input--width-3" id="width-3" name="width-3" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="width-2">
2 character width
</label>
<input class="govuk-input govuk-input--width-2" id="width-2" name="width-2" type="text">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "20 character width"
},
classes: "govuk-input--width-20",
id: "width-20",
name: "width-20"
}) }}
{{ govukInput({
label: {
text: "10 character width"
},
classes: "govuk-input--width-10",
id: "width-10",
name: "width-10"
}) }}
{{ govukInput({
label: {
text: "5 character width"
},
classes: "govuk-input--width-5",
id: "width-5",
name: "width-5"
}) }}
{{ govukInput({
label: {
text: "4 character width"
},
classes: "govuk-input--width-4",
id: "width-4",
name: "width-4"
}) }}
{{ govukInput({
label: {
text: "3 character width"
},
classes: "govuk-input--width-3",
id: "width-3",
name: "width-3"
}) }}
{{ govukInput({
label: {
text: "2 character width"
},
classes: "govuk-input--width-2",
id: "width-2",
name: "width-2"
}) }}
Fluid width inputs
Use the width override classes to reduce the width of an input in relation to its parent container, for example, to two-thirds.
Fluid width inputs will resize with the viewport.
<div class="govuk-form-group">
<label class="govuk-label" for="full">
Full width
</label>
<input class="govuk-input govuk-!-width-full" id="full" name="full" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="three-quarters">
Three-quarters width
</label>
<input class="govuk-input govuk-!-width-three-quarters" id="three-quarters" name="three-quarters" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="two-thirds">
Two-thirds width
</label>
<input class="govuk-input govuk-!-width-two-thirds" id="two-thirds" name="two-thirds" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="one-half">
One-half width
</label>
<input class="govuk-input govuk-!-width-one-half" id="one-half" name="one-half" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="one-third">
One-third width
</label>
<input class="govuk-input govuk-!-width-one-third" id="one-third" name="one-third" type="text">
</div>
<div class="govuk-form-group">
<label class="govuk-label" for="one-quarter">
One-quarter width
</label>
<input class="govuk-input govuk-!-width-one-quarter" id="one-quarter" name="one-quarter" type="text">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Full width"
},
classes: "govuk-!-width-full",
id: "full",
name: "full"
}) }}
{{ govukInput({
label: {
text: "Three-quarters width"
},
classes: "govuk-!-width-three-quarters",
id: "three-quarters",
name: "three-quarters"
}) }}
{{ govukInput({
label: {
text: "Two-thirds width"
},
classes: "govuk-!-width-two-thirds",
id: "two-thirds",
name: "two-thirds"
}) }}
{{ govukInput({
label: {
text: "One-half width"
},
classes: "govuk-!-width-one-half",
id: "one-half",
name: "one-half"
}) }}
{{ govukInput({
label: {
text: "One-third width"
},
classes: "govuk-!-width-one-third",
id: "one-third",
name: "one-third"
}) }}
{{ govukInput({
label: {
text: "One-quarter width"
},
classes: "govuk-!-width-one-quarter",
id: "one-quarter",
name: "one-quarter"
}) }}
Hint text
Use hint for help that’s relevant to the majority of users, like how their information will be used, or where to find it.
<div class="govuk-form-group">
<label class="govuk-label" for="event-name">
Event name
</label>
<span id="event-name-hint" class="govuk-hint">
The name you’ll use on promotional material.
</span>
<input class="govuk-input" id="event-name" name="event-name" type="text" aria-describedby="event-name-hint">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Event name"
},
hint: {
text: "The name you’ll use on promotional material."
},
id: "event-name",
name: "event-name"
}) }}
Use the autocomplete attribute
Use the autocomplete
attribute on text inputs to help users complete forms more quickly. This lets you specify an input’s purpose so browsers can autofill the information on a user’s behalf if they’ve entered it previously.
For example, to enable autofill on a postcode field, set the autocomplete
attribute to postal-code
. See how to do this in the HTML and Nunjucks tabs in the following example.
<div class="govuk-form-group">
<label class="govuk-label" for="postcode">
Postcode
</label>
<input class="govuk-input govuk-input--width-10" id="postcode" name="postcode" type="text" autocomplete="postal-code">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Postcode"
},
classes: "govuk-input--width-10",
id: "postcode",
name: "postcode",
autocomplete: "postal-code"
}) }}
If you are working in production and there is a relevant input purpose, you’ll need to use the autocomplete
attribute to meet WCAG 2.1 Level AA.
You will not normally need to use the autocomplete
attribute in prototypes, as users will not generally be using their own devices.
Do not disable copy and paste
Users often need to copy and paste information into a text input, so do not stop them from doing this.
How and when to spellcheck a user’s input
Sometimes, browsers will spellcheck the information a user puts into a text input. If a user enters something which is recognised as a spelling error, sighted users will see a red line under the word.
If you are asking users for information which is not appropriate to spellcheck, like a reference number, name, email address or National Insurance number, disable the spellcheck.
To do this set the spellcheck
attribute to false
as shown in this example.
<div class="govuk-form-group">
<label class="govuk-label" for="name">
Reference number
</label>
<input class="govuk-input" id="name" name="name" type="text" spellcheck="false">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Reference number"
},
id: "name",
name: "name",
attributes: {
spellcheck: "false"
}
}) }}
Browsers do not consistently spellcheck user’s input by default. If you are asking a question where spellcheck would be useful, set the spellcheck
attribute to true
.
Error messages
Error messages should be styled like this:
<div class="govuk-form-group govuk-form-group--error">
<label class="govuk-label" for="event-name">
Event name
</label>
<span id="event-name-hint" class="govuk-hint">
The name you’ll use on promotional material.
</span>
<span id="event-name-error" class="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span> Enter an event name
</span>
<input class="govuk-input govuk-input--error" id="event-name" name="event-name" type="text" aria-describedby="event-name-hint event-name-error">
</div>
{% from "input/macro.njk" import govukInput %}
{{ govukInput({
label: {
text: "Event name"
},
id: "event-name",
name: "event-name",
hint: {
text: "The name you’ll use on promotional material."
},
errorMessage: {
text: "Enter an event name"
}
}) }}
Make sure errors follow the guidance in error message and have specific error messages for specific error states.
If the input is empty
Say ‘Enter [whatever it is]’.
For example, ‘Enter your first name’.
If the input is too long
Say ‘[whatever it is] must be [number] characters or less’.
For example, ‘Address line 1 must be 35 characters or less’.
If the input is too short
Say ‘[whatever it is] must be [number] characters or more’.
For example, ‘Full name must be 2 characters or more’.
If the input is too long or too short
Say ‘[whatever it is] must be between [number] and [number] characters’.
For example, ‘Last name must be between 2 and 35 characters’.
If the input uses characters that are not allowed and you know what the characters are
Say ‘[whatever it is] must not include [characters]’.
For example, ‘Town or city must not include è and £’.
If the input uses characters that are not allowed and you do not know what the characters are
Say ‘[whatever it is] must only include [list of allowed characters]’.
For example, ‘Full name must only include letters a to z, hyphens, spaces and apostrophes’.
If the input is not a number
Say ‘[whatever it is] must be a number [optional example]’.
For example, ‘Hours worked a week must be a number, like 30’.
If the input is not a whole number
Say ‘[whatever it is] must be a whole number [optional example]’.
For example, ‘Hours worked a week must be a whole number, like 30’.
If the number is too low
Say ‘[whatever it is] must be [lowest] or more’.
For example, ‘Hours worked a week must be 16 or more’.
If the number is too high
Say ‘[whatever it is] must be [highest] or less’.
For example, ‘Hours worked a week must be 99 or less’.
If the input must be between 2 numbers
Say ‘[whatever it is] must be between [lowest] and [highest]’.
For example, ‘Hours worked a week must be between 16 and 99’.
If the input is not an amount of money and the field allows decimals
Say ‘[whatever it is] must be an amount of money [optional example that includes decimals and non-decimals]’.
For example, ‘How much you earn an hour must be an amount of money, like 7.50 or 8’.
If the input is not an amount of money and the field needs decimals
Say ‘[whatever it is] must be an amount of money [optional example that includes decimals]’.
For example, ‘How much you earn an hour must be an amount of money, like 7.50 or 8.00’.
If the input is an amount of money that needs decimals
Say ‘[whatever it is] must include pence, like 123.45 or 156.00’.
For example, ‘How much you earn a week must include pence, like 123.45 or 156.00’.
If the input is an amount of money that must not have decimals
Say ‘[whatever it is] must not include pence, like 123 or 156’.
For example, ‘How much you earn a week must not include pence, like 123 or 156’.
Help improve this page
To help make sure the Text input page is useful, relevant and up to date, you can:
- share research or feedback about the Text input component on GitHub
- propose a change to this page - read more about how to propose changes in GitHub
Need help?
If you’ve got a question about the GOV.UK Design System you can contact the team: