A <label> element should have text content and be associated with a control.
When a label element lacks text content or an associated control, it can lead to several issues:
Control elements include:
<input><meter><output><progress><select><textarea>This rule accepts some server-side and template constructs when they generate the association, the label text, or the control at runtime.
<label asp-for="..."> counts as an explicit association. If its body is empty or whitespace-only, ASP.NET Core generates the
label text.[for],
:for, v-bind:for, [innerText], [innerHTML], [textContent], v-text,
v-html, th:for, th:text, th:utext, and th:attr assignments to for,
aria-label, or aria-labelledby.@Html.TextBoxFor(...) and related
form helpers, <partial>, <vc:...>, @await Html.PartialAsync(...), @{ await
Html.RenderPartialAsync(...); }, and Thymeleaf fragment insertion with th:insert, th:include, or
th:replace.<fmt:message>.Make sure the <label> has text that describes the control.
An implicit association wraps the control element inside the <label>. An explicit association keeps the label and control
separate, and links them with the label’s for attribute and the control’s id. Both approaches are compliant. If you lack a
control element, add one.
<!-- Non-compliant: label without text --> <label for="favorite-sport"></label> <input id="favorite-sport" type="text" /> <!-- Non-compliant: missing "for" attribute on label --> <label>Favorite food</label> <input id="favorite-food" type="text" />
<!-- Explicit association -->
<label for="favorite-sport">Favorite sport</label>
<input id="favorite-sport" type="text" />
<!-- Implicit association -->
<label>
Favorite food
<input id="favorite-food" type="text" />
</label>