Examples
The FormLabel
component provides the label
(or legend
) for a field, along with any associated hint text and error message.
Code
React
Name | Type | Default | Description |
---|---|---|---|
children required | React.ReactNode | Label text or HTML. | |
className | string | Additional classes to be added to the root element. | |
component | 'label' | 'legend' | 'label' | The root HTML element used to render the label |
errorMessage | React.ReactNode | Enable the error state by providing an error message. | |
errorMessageClassName | string | Additional classes to be added to the error message | |
errorId | string | The ID of the error message applied to this field. | |
fieldId | string | The ID of the field this label is for. This is used for the label's | |
hint | React.ReactNode | Additional hint text to display | |
id | string | A unique | |
inversed | boolean | Set to | |
requirementLabel | React.ReactNode | Text showing the requirement (ie. "Optional", or "Required"). In most cases, this should be used to indicate which fields are optional. See the form guidelines for more info. | |
textClassName | string | Additional classes to be added to the label text. | |
This component passes any additional props to its underlying <label> or <legend> element as attributes. See the corresponding MDN documentation for <label> and <legend> for a list of valid attributes. |
Styles
The following Sass variables can be overridden to customize Form components:
Guidance
Form legend
- Use a single legend for fieldset (this is required). One example of a common use of fieldset and legend is a question with radio button options for answers. The question text and radio buttons are wrapped in a fieldset, with the question itself being inside the legend tag.
- Avoid links in legends when possible. Links inside legends are not read by some screen readers, namely JAWS. If you need a pattern like this, put the link outside of the legend element, but keep all other useful hint text in the legend.
Form label
- Each form field should have a
<label>
. Never use a field's placeholder attribute as the primary way to label the field. - Each form field should have a clear, brief, and descriptive label.
- Labels should have a
for
attribute, referencing the corresponding input's uniqueid
attribute. Only one label can be associated to each unique form element. - Labels should be placed above their fields.
- Label text should be short and in sentence case.
- Avoid colons at the end of labels.
- Don't use a field's placeholder attribute as the primary way to label the field.
Content
- Describe what the user needs to do when the field is selected
- Use a phrase or complete sentence
- Start with an action verb and pair it with a noun to help it be clearer. Example: Enter your age.
- Use the fewest words possible (i.e. don't use "please")
- Use sentence case
- Be consistent with punctuation at the end of a phrase or sentence
- Be consistent with phrase and sentence structure
Hint text
- Place hint text within the field's
<label>
element. - Place hint text where appropriate depending on what content it's helping to explain
- Use hint text for supporting contextual help, which will always be shown.
- Hint text should sit between a form label and the input.
- Use lower visual style (i.e., lighter gray than the form label or the input option) so it won't compete for users' attention with the field label.
- Use hint text to indicate which inputs are optional when there are more required fields than optional.
- Hint text can also appear after an input box when the text is being used to show the value someone should be entering in the field (e.g., income amount input box displays text after the input box to highlight the amount should be "monthly income".)
Content
- Use when appropriate to support a form label (Goes below the form label in a lighter weight font)
- Describe how to best answer the question/form label
- Use the fewest words possible (i.e. don't use "please")
- Use sentence case
- End with a period
Learn More
Component maturity
For more information about how we tested and validated our work for each checklist item, read our component maturity documentation.