Inputs
Input elements are used to gather information from users.
Inputs are normally paired with a label, but there times when they can be used without a label. Placeholder text should primarily be used as a content prompt and only provided when needed.
The best accessibility is semantic HTML. Most screen readers understand how to parse inputs if they're correctly formatted. When it comes to inputs, there are a few things to keep in mind:
- All inputs should have an
id
attribute. - Be sure to associate the input’s label by using the
for
attribute. The value here is the input’sid
. - If you have a group of related inputs, use the
fieldset
andlegend
to group them together.
For more information, please read Gov.UK's article, "Using the fieldset and legend elements".
Validation states provides the user feedback based on their interaction (or lack of interaction) with an input. These styles are applied by applying the appropriate class to the wrapping parent container.
Class | Applies | Definition |
---|---|---|
.has-warning |
Parent wrapper for input | Used to warn users that the value they've entered has a potential problem, but it doesn't block them from proceeding. |
.has-error |
Parent wrapper for input | Used to alert users that the value they've entered is incorrect, not filled in, or has a problem which will block them from proceeding. |
.has-success |
Parent wrapper for input | Used to notify users that the value they've entered is fine or has been submitted successfully. |
In most cases, validation states shouldn’t be shown until after the user has submitted the form. There are certain exceptions where it can be appropriate to show a validation state without form submission—after a sufficient delay. For example, validating the existence of a username can occur after the user has stopped typing, or when they’ve deselected the input.
Once the user is presented validation states, they can be cleared as soon as the user interacts with the form field. For example, the error state for an incorrect password should be cleared as soon as the user focuses the input to re-enter their password.
Stacks provides helper classes to consistently style an input used for search. First, wrap your search input in an element with relative positioning. Then, and add s-input__search
to the input itself. Finally, be sure to add s-input-icon
and s-input-icon__search
to the search icon.
Name | Size | Class | Example |
---|---|---|---|
Small | 12px | .s-input__sm |
|
Default | 13px | N/A | |
Medium | 17px | .s-input__md |
|
Large | 21px | .s-input__lg |
|
Extra Large | 27px | .s-input__xl |
Input fills are used to visually connect input text boxes with related content.
The most common input is type="text"
, yet there are many other input types that can be used depending on the content type being gathered. Below is a list of available types and when you should consider using them:
Type | Description |
---|---|
color |
Allows user to select a color and returns the hex value for that color. The color picker used is native to the OS or the browser. |
date |
Allows user to enter a date (year, month, and day) without a time. |
datetime-local |
Allows user to enter a date (year, month, and day) and a time, with no time zone. |
email |
Allows user to enter an email address. Some mobile devices will deliver custom keyboards specifically for entering email addresses. |
file |
Allows user to select a file. To define the type of files that can be selected, use the accept="" attribute. |
month |
Allows user to enter a month and year with no time zone. |
number |
Limits user to entering only numbers. Some mobile devices will deliver a custom keyboard. |
password |
Single-line text input whose value is obsecured. Use the minlength and maxlength attributes to specify the value length. |
range |
Allows user to enter a number who exact value isn't important. |
search |
Single-line text input for entering search strings. Some mobile devices will deliver a custom keyboard. |
tel |
Allows user to enter a telephone number. Some mobile devices will deliver a custom keyboard. |
text |
Single-line text input field. |
time |
Allows user to enter a time value with no time zone. |
url |
Allows user to enter a website URL. Some mobile devices will deliver a custom keyboard. |
week |
Allows user to enter a date consisting of a week-year number and a week number with no time zone. |
There are a number of attributes available for inputs. These attributes allow you to help the user understand what data type or format you are requesting.
Attribute | Description |
---|---|
accept |
If the type is file , then this attribute will indicate the file types that will be accepted. The value must be a comma-separated list. |
autocomplete |
Indicates if the control value can be automatically completed by the browser. For a full list of possible values, please visit MDN Inputs page. |
autofocus |
Lets you specify which form control should have input focus on page load. Only one element can have this attribute and it cannot be set on hidden elements. |
capture |
If the type is file , then this Boolean attribute will indicate that the capture of media directly from the device's environment using a media capture mechanicism is preferred (e.g. taking a photo using a camera). |
disabled |
Boolean attribute that indicates the input is not available for interaction. |
form |
This attribute associates the form element with the input element. The input value must be the form's id value within the same document. If this isn't specified, the input will use the closest ancestor form element. This attribute allows you to place inputs anywhere on the page, not just as a descendant within a form element. An input can only be associated with one form. |
max |
Maximum numeric or date-time value for this item, which cannot be lesser than the minimum value. |
maxlength |
If type is text , email , search , password , tel , or url ; this attribute sets the maximum number of characters that can be entered. |
min |
Minimum numeric or date-time value for this item, which cannot be greater than the minimum value. |
minlength |
If type is text , email , search , password , tel , or url ; this attribute sets the minimum number of characters that must be entered. |
multiple |
If type is email or file , this attribute allows for more than one value. |
name |
Name of the control which is submitted with the form data. |
placeholder |
Provides a hint to the user of what can be entered into the control. |
readonly |
Indicates that the control cannot be modified by the user. This is ignored if the type attribute is hidden , range , color , checkbox , radio , or a button type. |
required |
Specifies that the user must provide a value for this control before submitting the form. This is ignored if the type attribute is hidden , image , or a button type. |
selectionDirection |
Specifies the direction in which a selection occurs. |
selectionEnd |
The offset into the element's text content of the last selected character. If there's no selection, this value indicates the offset to the character following the current text input cursor position (that is, the position the next character typed would occupy). |
selectionStart |
The offset into the element's text content of the first selected character. If there's no selection, this value indicates the offset to the character following the current text input cursor position (that is, the position the next character typed would occupy). |
size |
The control's initial size. |
spellcheck |
Setting the value to true indicates that the element needs to have its spelling and grammar checked. |
step |
Works with min and max attributes to limit the increments at which a numeric or date-time value can be set. |
tabindex |
Position of the element in the tab navigation order for the current document. |
value |
Initial value for the control. |