Skip to content

Label

A Label provides a descriptive title for a form input. Having labels is essential when filling out a form, since each field is associated with its label.

Guidelines

Using labels

The Label component will be placed above each form Field. An optional indicator, a tooltip, and a description could also be included next to the label text.

Specifications

Specification of Label.

Label is used within Field. It can also be used on its own for cases when the Field component cannot or should not be used (e.g. to label a SearchInput). The Label may include the following elements:

  1. Icon (optional)
    A start icon can be included to visually enhance the label's context. This icon is fully customizable and any icon from the provided list can be used. Use a simple icon that is easily comprehensible to users.
  2. Label text
    A clear and descriptive title for the form field that tells the user what information to enter.
  3. Field indicator (optional)
    Subtle text indicating that the field associated with the label is not required.
  4. Description (optional)
    Subtle descriptive text that provides additional information about the field.

The Label will be bold by default. However, it can be customized to regular weight when needed in specific cases:

  • Bold labels should be used when grouping two or more form fields under a title with a larger text size.
  • Regular labels should be used when grouped under a title with the same text size as the form field label. This prevents hierarchy issues and ensures consistency.

Label and description can wrap onto multiple lines as needed, without strict length limits. However, it's best to keep them concise for better form readability. If the label spans multiple lines, the optional indicator will appear next to the last line of the label.

Refer to the Label component in Codex Figma.

Interaction states

Label includes only two states:

Interaction states of Label: default and disabled.

  1. Default
  2. Disabled

Best practices

Consider the following recommendations when using labels within form fields. For more detailed information about form fields, consult the guidelines for constructing forms.

Usage

Two examples where a Label is used within Fields.

Do:
  • Use the label as a title for any Field within a form.

Label being used on top of a ToggleButtonGroup.

Don't:
  • Use the label component as a title for non-form items such as toggle buttons.
  • Use labels as standalone elements on a page.

Icon

Field using a start icon next to the Label.

Do:
  • Use a start icon next to the label to emphasize the required input in the form field.

Field featuring two icons: one positioned next to the label and the other inside the TextInput.

Don't:
  • Use another icon in the label's section if the field already contains an icon.

Label and description

Field with a short label and a lengthy description.

Do:
  • Keep the Field’s label short, clear, and easy to scan.
  • Utilize the label’s description for additional information if needed.

Field with a lengthy label.

Don't:
  • Make the Field’s label text excessively large, which could make it difficult to scan.

Optional fields

Two Fields: one featuring the "(optional)" indicator.

Do:
  • Only indicate the optional Fields in the form.
  • Ensure that the entire word "optional" is displayed for translation purposes.

Two Fields: one required, marked with an asterisk, and an optional one indicated with "(opt)".

Don't:
  • Mark required labels with an asterisk.
  • Use abbreviations in the optional indicator.

Label style

Fieldset with each field labeled as regular.

Do:
  • Customize the label of Fields to regular weight when grouped within a fieldset.

Fieldset with each field labeled as bold.

Don't:
  • Avoid using bold labels in the Fields within a fieldset to maintain hierarchy and clarity.

Keyboard navigation

KeyFunction
EnterIf the focus is placed on one of the interactive elements within the Label, it activates them.

Demos

TIP

You should consider using the Field component, which includes the Label component and handles accessibility features like ARIA attributes internally. If you need more custom behavior, you can use the Label component on its own.

Configurable

Short description text
NameValue
Props
icon
optionalFlag
visuallyHidden
isLegend
disabled
Slots
default
description
View
Reading direction
Note: For icon properties, the relevant icon also needs to be imported from the @wikimedia/codex-icons package. See the usage documentation for more information.

Basic usage

When using the Label component with an input, make sure to add an id attribute to the input and use that to set the for attribute on the label. These attributes will be applied on the proper element within each component (in the example below, that's the <label> for the Label component, and the <input> for the TextInput).

Visually-hidden label

In rare instances, you may not need a visible label if it's very clear from context how to use an input. To achieve this, you can use the Label component to create a semantic label for assistive technology, but add the visuallyHidden prop to visually hide the label.

Note that this will hide the entire output of the component, including the description.

With rich description text

You can include markup in the description slot. Only links and text formatting are recommended as description text should be concise.

Label vs. legend

This component outputs a <label> element by default. When the isLegend prop is set to true, it will instead output a <legend> element. Use this within a <fieldset> element.

There is a named slot in the Label component, description, for including more information about the field. It's important to note that, when isLegend is true, the description will be rendered inside the <legend> element to ensure it will be read by assistive technology.

Below are examples of a label and a legend with an associated description.

Enter your preferred name
Notifications Choose how often you'd like to receive notifications

Vue usage

Props

Prop nameDescriptionTypeDefault
iconIcon before the label text.

Do not use this if including a start icon within the input component.
Icon|nullnull
optionalFlagText to indicate that the field is optional.

For example, this might be '(optional)' in English. This text will be placed next to the label text.
string''
visuallyHiddenWhether the label should be visually hidden.booleanfalse
isLegendWhether this component should output a <legend> element.

Always set this to true when this component is used inside a <fieldset> element. Do not set it to true otherwise.
booleanfalse
inputIdThe ID of the input/control this label is for.

Will be added as the for attribute of the <label>. Not needed for <legend>.
string''
descriptionIdThe ID of the description element.

This ID can be used for the aria-describedby attribute of the input.
string''
disabledWhether this label is for a disabled field or input.booleanfalse

Slots

NameDescriptionBindings
defaultLabel text.
descriptionShort description text.

CSS-only version

Markup structure

Examples are shown with inputs to demonstrate the ARIA attributes required to connect the label and input.

Label

Use a <label> element for a single input.

Short description text

Legend

Inside a <form>, use a <legend> element within a <fieldset> for input groups.

When outputting a <legend> rather than a <label>, the markup of this component is quite different:

  • There is no wrapper <div><legend> must be a direct child of <fieldset>
  • The description is included inside the <legend>
  • The for and aria-describedby attributes are not needed
Legend text (optional) Short description text

With icon

To add an icon to the label:

  • Add an empty <span> before the label text with the classes .cdx-label__label__icon and a custom class that you can target to add the CSS-only icon
  • Apply the CSS-only icon mixin to that <span>

This works for <label> and <legend>.

Enter your signature using plain text or wikitext

Visually hidden

Occasionally, there may be a use case for the label to be visually hidden. In these cases, it should still be present in the DOM with appropriate elements and ARIA attributes.

To visually hide the label, add the .cdx-label--visually-hidden class to the outer element. This works for <label> and <legend>.

Disabled

Add the .cdx-label--disabled class to the outer element to get disabled styles. This works for <label> and <legend>.