2.23.0
Labels describe form elements and indicate whether they’re required or optional.
See Forms for guidance on composing full forms.
Use to describe a form input.
<label class="mds-form__label" for="associated-element-id">
First Name
</label>
<mds-label for="associated-element-id">First Name</mds-label>
Always wrap grouped form elements in a field group container. See the Forms composition section for more guidelines on constructing forms.
<!-- HTML -->
<div class="mds-form__field-group">
<label class="mds-form__label" for="first-name"> First Name </label>
<input class="mds-form__input" type="text" id="first-name" placeholder="First Name" />
</div>
<!-- Web Component -->
<mds-field-group>
<mds-label for="first-name"> First Name </mds-label>
<mds-input id="first-name" placeholder="First Name"></mds-input>
</mds-field-group>
Use to indicate an input is required.
<label class="mds-form__label" for="associated-element-id">
First Name
<abbr class="mds-form__label-required-indicator" title="This field is required." aria-label="This field is required.">*</abbr>
</label>
<mds-label for="associated-element-id" required>First Name</mds-label>
Use to indicate an input is optional.
<label class="mds-form__label" for="associated-element-id">
First Name<span class="mds-form__label-optional">
(Optional)
</span>
</label>
<mds-label for="associated-element-id" optional>First Name</mds-label>
Sizing affects text size. The default size is medium, and you can use modifier classes or props to make the label smaller or larger.
<label class="mds-form__label mds-form__label--small" for="associated-element-id">
First Name
<abbr class="mds-form__label-required-indicator" title="This field is required." aria-label="This field is required.">*</abbr>
</label>
<mds-label for="associated-element-id" required size="small">First Name</mds-label>
Class
|
Applies to
|
Outcome
|
---|---|---|
|
|
Applies small styles to the label text. |
|
|
Applies large styles to the label text. |
When setting props as attributes on a custom HTML element, use kebab-case
instead of camelCase
.
Prop
|
Type
|
Validation
|
Default
|
Description
|
---|---|---|---|---|
|
String |
— |
— |
A space-separated list of class names that will be appended to the default |
|
String |
Required |
— |
The |
|
String |
— |
|
If |
|
String |
— |
|
Can be used to override the default |
|
Boolean |
— |
|
If |
|
String |
— |
|
If |
|
String |
One of: |
|
Alters the size of the label. |
|
String |
Required |
— |
The text for the label. Can also be passed via the default slot. |
Pass text in between the <mds-label></mds-label>
tags to set the label’s text content. This can be overridden with the text
prop.
Slot Name
|
Description
|
---|---|
|
If |
Setting size
, text
, for
, and required
via props:
<mds-label size="large" text="First Name" for="first-name-input" required></mds-label>
Setting for
and optional
via props. Using named slots for mds-label-optional-text
:
<mds-label for="profession-select" optional>
Profesión
<slot name="mds-label-optional-text">(Opcional)</slot>
</mds-label>
label
with each form element. See the Forms composition section for examples.for
attribute on each label
with a value matching the id
attribute on the corresponding input
, textarea
or select
.placeholder
text in place of a label
.