Skip to content

Label

The right typography on <label>, and one consistent way to mark required and optional fields. If you are setting up a whole field, Field is usually simpler. It sets for, id and aria-describedby for you.

AttributeEffect
(omitted)An ordinary label
data-required="symbol" * in the danger colour
data-required="text" (required) in the danger colour
data-optional="" (optional) in a muted colour
aria-disabled="true"Muted text colour

Choose one of data-required and data-optional and stick to it throughout the form. If you set both, the system shows only one: they are the same ::after, and the builder function drops data-optional when the field is required.

for has to match the id of the field. Without the wiring the label is not read out when the field gets focus, and a click on the text does not move focus there.

ClassWhat it is
.lt-labelThe label. The markers are added with ::after.

The marker behind data-required and data-optional is text in ::after, and the text is read from a variable. It can then be replaced, or translated, without changing the markup:

VariableDefault
--lt-label-required-symbol" *"
--lt-label-required-text" (required)"
--lt-label-optional-text" (optional)"
:root:lang(nb) {
--lt-label-required-text: " (påkrevd)";
--lt-label-optional-text: " (valgfri)";
}

The variables also apply to .lt-legend in Fieldset. See Translation.

See Customising for how the layer works.

  • data-required is only visual. Set required on the field itself, or the screen reader will not know it has to be filled in.
  • The asterisk and “(required)” are added with ::after. Content in ::after counts towards the field’s name, so a screen reader may read it after the label. It is required on the field that says it has to be filled in.
  • Never hide the label completely. If you need it gone visually, use .lt-sr-only from sr-only.css rather than placeholder, which disappears as soon as the user types.