Sections
Input elements are used to gather information from users.
Base style
Section titled Base styleInputs are normally paired with a label, but there are times when they can be used without a label. Placeholder text should primarily be used as a content prompt and only provided when needed.
<div class="d-flex gy4 fd-column">
<div class="flex--item">
<label class="d-block s-label" for="example-item1">
Full name
<p class="s-description mt2">This will be shown only to employers and other Team members.</p>
</label>
</div>
<div class="d-flex ps-relative">
<input class="s-input" id="example-item1" type="text" placeholder="Enter your input here" />
</div>
</div>
<div class="d-flex gy4 fd-column ps-relative">
<label class="flex--item s-label" for="example-item2">Display name</label>
<div class="d-flex ps-relative">
<input class="s-input" id="example-item2" type="text" placeholder="Enter your input here" disabled />
@Svg.Lock.With("s-input-icon fc-black-400")
</div>
</div>
<div class="d-flex gy4 fd-column ps-relative is-readonly">
<label class="flex--item s-label" for="example-item3">Legal Name</label>
<div class="d-flex ps-relative">
<input class="s-input" id="example-item3" type="text" placeholder="Enter your input here" readonly />
@Svg.Lock.With("s-input-icon fc-black-400")
</div>
</div>
Accessibility
Section titled AccessibilityThe 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
Section titled Validation statesValidation 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.
Validation classes
Section titled Validation classesClass | 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. |
Validation guidance
Section titled Validation guidanceIn 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.
Similarly to using for
with labels, validation messages below inputs should be associated with their respective fields using the aria-describedby
attribute for accessible behavior.
Validation examples
Section titled Validation examplesWarning
Section titled Warning<div class="d-flex gy4 fd-column has-warning">
<label class="flex--item s-label" for="example-warning">Username</label>
<div class="d-flex ps-relative">
<input class="s-input" id="example-warning" type="text" placeholder="" aria-describedby="example-warning-desc" />
@Svg.Alert.With("s-input-icon")
</div>
<p id="example-warning-desc" class="flex--item s-input-message mb0">Caps lock is on! <a>Having trouble entering your username?</a></p>
</div>
Error
Section titled Error<div class="d-flex gy4 fd-column has-error">
<label class="flex--item s-label" for="example-error">Username</label>
<div class="d-flex ps-relative">
<input class="s-input" id="example-error" type="text" placeholder="e.g. johndoe111" aria-describedby="example-error-desc" aria-invalid="true" />
@Svg.AlertCircle.With("s-input-icon")
</div>
<p id="example-error-desc" class="flex--item s-input-message mb0">You must provide a username. <a>Forgot your username?</a></p>
</div>
Success
Section titled Success<div class="d-flex gy4 fd-column has-success">
<label class="flex--item s-label" for="example-success">Username</label>
<div class="d-flex ps-relative">
<input class="s-input" id="example-success" type="text" aria-describedby="example-success-desc" />
@Svg.Checkmark.With("s-input-icon")
</div>
<p id="example-success-desc" class="flex--item s-input-message mb0">That name is available! <a>Why do we require a username?</a></p>
</div>
Icons
Section titled IconsSearch
Section titled SearchStacks 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.
<div class="ps-relative">
<label class="v-visible-sr" for="example-search">Search</label>
<input class="s-input s-input__search" id="example-search" type="text" placeholder="Search…" />
@Svg.Search.With("s-input-icon s-input-icon__search")
</div>
Credit Card
Section titled Credit Card<div class="ps-relative">
<label class="v-visible-sr" for="example-creditcard">Credit Card</label>
<input class="s-input s-input__creditcard" id="example-creditcard" type="text" />
@Svg.CreditCard.With("s-input-icon s-input-icon__creditcard")
</div>
Sizes
Section titled SizesName | 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
Section titled Input fillsInput fills are used to visually connect input text boxes with related content.
Prepended inputs
Section titled Prepended inputs<div class="d-flex gy4 fd-column">
<label class="flex--item s-label" for="website-url">Website URL</label>
<div class="d-flex">
<div class="flex--item s-input-fill order-first">https://</div>
<div class="d-flex fl-grow1 ps-relative">
<input class="flex--item s-input blr0" type="text" id="website-url" placeholder="www.stackoverflow.com" />
</div>
</div>
</div>
Appended inputs
Section titled Appended inputs<div class="d-flex gy4 fd-column">
<label class="flex--item s-label" for="min-salary">Minimum Salary</label>
<div class="d-flex">
<div class="d-flex ai-center order-last s-input-fill">
<div class="d-flex gx4 ai-center">
<input class="flex--item s-checkbox" type="checkbox" id="need-visa" />
<label class="flex--item s-label s-label__sm fw-normal" for="need-visa">Need Visa Sponsorship</label>
</div>
</div>
<div class="d-flex fl-grow1 ps-relative">
<input class="flex--item s-input brr0" type="number" id="min-salary" placeholder="e.g. 125,000" />
</div>
</div>
</div>