Label
The text that names a form control. A click on it focuses or toggles the control.
Thymeleaf source
<sl:label for="label-email">Email</sl:label><sl:input type="email" id="label-email" placeholder="you@example.com"/>Rendered HTML
<label class="label" for="label-email">Email</label><input class="input" type="email" id="label-email" placeholder="you@example.com">When to use
Section titled “When to use”- A control outside a field, such as a checkbox or a switch with its text next to it.
Use something else for:
- A control in a form, with its description and errors: a field, whose field label does this for you.
Pointing at a control
Section titled “Pointing at a control”Give the label a for with the control’s id. A control with th:field="*{email}" gets id="email" from Spring, so
for="email" matches it without writing the id yourself. Clicking the label focuses the control, and a screen
reader announces the label’s text as the control’s name.
A label can also hold its control, and then needs no for:
Thymeleaf source
<sl:label><input type="checkbox" name="newsletter"> Send me the newsletter</sl:label>Rendered HTML
<label class="label"> <input type="checkbox" name="newsletter"> Send me the newsletter</label>In a field, use <sl:field-label>: it points at the field’s control by itself.
Disabled
Section titled “Disabled”A label fades with a disabled control it holds, or one right before or after it, including a control disabled by a
<fieldset disabled> around it. There is nothing to add to the label.
Attributes
Section titled “Attributes”sl:label declares no props; every attribute passes through.
Styling hooks
Section titled “Styling hooks”The classes and attributes your own CSS can select to restyle the component, as explained in your own CSS rules.
Parts
| Class | Element |
|---|---|
.label | <label> |