Skip to content

Label

Label
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">

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:

Around its controlA label that holds its control 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.

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.

sl:label declares no props; every attribute passes through.