2.16.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"> 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"> 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"> 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"> 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. |
Prop
|
Type
|
Validation
|
Default
|
Description
|
---|---|---|---|---|
|
String |
— |
— |
A space-separated list of class names that will be appended to the default |
|
String |
Required |
— |
The |
|
String |
— |
— |
The |
|
String |
— |
|
If true, sets optional text to the right of the label with a default text of |
|
String |
— |
|
Can be used to override the default |
|
Boolean |
— |
|
If true, the required indicator is visible to the right of the label. |
|
String |
— |
|
If |
|
String |
Enum: |
|
Alters the size of the label. |
|
String |
Required |
— |
Text for the label. |
optionalText
would be written as optional-text
.Any text or markup inside the <mds-label></mds-label>
will be used as the label’s text content.
Slot Name
|
Description
|
---|---|
|
Provided that |
Setting size, label text, for, and required status:
<!-- Using Props -->
<mds-label size="large" text="First Name" for="first-name-input" required="true"></mds-label>
<!-- Using Slots -->
<mds-label size="large" for="first-name-input" required="true"> First Name </mds-label>
Setting custom optional text:
<!-- Using Props -->
<mds-label text="Profesión" for="profession-select" optional="true" optional-text="(Opcional)"></mds-label>
<!-- Using Slots -->
<mds-label for="profession-select" optional="true"> 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
.