Components
Label
Use: Best practice WebExamples
Examples shown here use the va-text-input component to demonstrate form labels. Labels are used consistently across standard form components.
Default
View Label default in Storybook
Required
View Label required in Storybook
Usage
When to use label
- All form inputs must have a label. All standard form inputs must have an associated label. Thus the label elements is built into our standard form components.
How this component works
- Show whether a field is required or optional. Mark required fields using the required property on the web component.
- Show “(*Required)” on required fields, with one exception. You can leave out the visible “(*Required)” text on single-purpose inputs and on simple forms where every field is needed to complete the task. In these cases, the context already makes it clear that people must fill in the field, so the extra text adds clutter without adding information. Examples include:
- A search field with a “Search” button
- A newsletter sign-up with only an email address field
- A sign-in form with only a username and password
- A password reset form with only an email address field
Keep the
requiredproperty on these fields so assistive technology still announces them as required. - Hide the visible text only in components that support it. The Text input and Select components have a
hide-required-textproperty. Other form components don’t have this property yet.
Error handling
View va-text-input with error in Storybook
- In all cases, only show error validation messages or stylings after a user has interacted with a particular field.
- The form field and its corresponding
<label>are wrapped in a container which provides a thick border and padding to visual indicate an error message to users who might have difficulty perceiving contrast. It also changes the border of the input to red as a secondary indicator. - An error message is placed between the label and the form field.
- The form field receives an
aria-describedbyattribute that references theidof the error message. - Prepending
<span class="sr-only">Error</span>to error messages alerts screen readers clearly that an error exists.
Hint text
When a detailed form input label is not enough to convey the meaning or purpose of the input, we have three options for including additional context:
Default hint text
View Label with hint text in Storybook
This should be used in the case where the needed clarification is a little longer, but less than two sentences. These should ideally not wrap more than twice for a total of three lines.
Inline within the label
View Label with inline hint text in Storybook
This should be used in the case where the needed clarification is very short.
With Details
View va-details default in Storybook
Using the Details component should be limited to brief, optional contextual help that some users may not need — for example, up to a short paragraph or a small list.
We want to avoid this variation when possible. Use of this component for this purpose is a last resort when attempts at reducing the content have failed. If a field needs a lot of explanation (multiple sentences, multiple paragraphs, or special formatting like long lists), use an Accordion or move the content to a distinct page with explanation on the page itself.
Checkbox and radio button labels
Don’t add HTML elements, such as links, images, bold text, or lists, inside checkbox or radio button labels. Keep each label as plain text.
When a label contains extra elements, people have to process more than the choice they’re making. This can cause problems for:
- Screen reader users. Screen readers read the entire label as the option name. Extra elements can make the name long or confusing, and they can break up the reading order.
- Keyboard and touch users. A link inside a label creates a second interactive target within the option. It’s harder to tell whether selecting it will choose the option or open the link.
- Comprehension. Formatting and extra content inside the label compete with the choice itself.
Use one of these alternatives instead:
- Add hint text. Put short supporting details in the hint text for the option or the group.
- Add the content before the question. Place longer explanations or links in text above the checkbox or radio button group, so people read them before they choose.
- Use a Details component. Place long or optional information in an expandable Details component near the group.
- Rewrite the label. Shorten the label so it’s clear without supporting content.
Content considerations
Standardized labels by form pattern
We’re in the process of standardizing form field labels and their related hint text and error messages. We’ll add to this list as we build out the set of standardized labels.
Ask uses for…
Provide feedback
Share your feedback, report issues, or suggest improvements for the Label component. Your input helps us make the design system better for everyone.
- Get immediate support in #platform-design-system for technical or urgent issues.
- Explore all feedback channels for additional ways to share your input.