HTML Guide
A label
element is not allowed as a descendant of a button
element.
The label
element represents a caption in a user interface. The caption can be associated with a specific form control, known as the label element’s labeled control, either using the for
attribute, or by putting the form control inside the label element itself.
Learn more:
Related W3C validator issues
The label element may only contain one labelable descendant.
For example:
<label for="age">
Age
<select id="age">
<option>young</option>
<option>old</option>
</select>
</label>
The label element may contain only one labelable descendant.
For example:
<label for="age">
Age
<select id="age">
<option>young</option>
<option>old</option>
</select>
</label>
A button element, or an element with the role=button attribute, is not allowed to be nested inside an <a> element.
A button element cannot contain a descendant element with the attribute tabindex.
When nesting an input element inside a label that has a for attribute, the id attribute of the input is required to match it.
The label element represents a caption in a user interface. The caption can be associated with a specific form control, known as the label element’s labeled control, either using the for attribute, or by putting the form control inside the label element itself.
When the input is inside the label, there’s no need to specify a for attribute as there can only be one input, as in this example:
<label>
Age
<input type="text" name="age">
</label>
However, if the for attribute is specified, then it must match the id of the input like this:
<label for="user_age">
Age
<input type="text" name="age" id="user_age">
</label>
When nesting a select element inside a label that has a for attribute, the id attribute of the select is required to match it.
The label element represents a caption in a user interface. The caption can be associated with a specific form control, known as the label element’s labeled control, either using the for attribute, or by putting the form control inside the label element itself.
When the select is inside the label, there’s no need to specify a for attribute as there can only be one select, as in this example:
<label>
Age
<select>
<option>young</option>
<option>old</option>
</select>
</label>
However, if the for attribute is specified, then it must match the id of the select like this:
<label for="age">
Age
<select id="age">
<option>young</option>
<option>old</option>
</select>
</label>
The aria-expanded attribute can only be true, false, or undefined.
This attribute indicates whether a grouping element is expanded or collapsed.
An element like <h1>, <h2>, etc., used to define a heading, does not accept the button role.
The following HTML code is invalid because the <h2> element can’t have role="button"
<h2 role="button">Some heading</h2>
Instead, you can nest the <h2> inside a <div> with that role. In this case however, browsers automatically apply role presentation to all descendant elements of any button element as it is a role that does not support semantic children.
<div role="button">
<h2>Some heading</h2>
</div>
A <li> element, used to define a list item, does not accept the button role.
This HTML code is invalid because the <li> elements can’t have role="button":
<ul>
<li role="button">One</li>
<li role="button">Two</li>
</ul>
The for attribute on a label element can’t be an empty string. This attribute is intended to specify which form element a label is associated with, and it must reference the ID of an existing form element. An empty string is neither a valid ID nor a meaningful association.
Explanation
- Invalid HTML: <label for=""></label>
The for attribute expects the value to be the ID of a form element, such as an input, textarea, select, etc.
How to Fix
- Identify the Form Element: Find the form element (input, textarea, select, etc.) that the label is supposed to be associated with.
- Assign an ID to the Form Element: Ensure the form element has a unique ID.
- Modify the Label’s for Attribute: Set the for attribute of the label to match the ID of the form element.
Example
Before Fix
<form>
<label for="">Username:</label>
<input type="text" name="username">
</form>
After Fix
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</form>