Both methods are equally valid and accessible as long as you correctly use the for/id
attributes to associate input
and label
. Even if the input
is nested inside the label, it will still be accessible to screen readers (refer to the top answer in the linked question by @AramKocharyan).
Screen readers focus on reading labels, legends, and buttons when a user interacts with form elements like inputs, selects, or textareas. Most screen readers prioritize filling out forms efficiently rather than reading all content on a webpage.
In many forms, you'll see the label on the left, the input in the center, and additional help information on the right:
E-mail [ ] ex: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="402a2f282e242f2500242f2d21292e6e232f2d">[email protected]</a>
- Placing an
input
outside of the label
means any hints or cues may not be conveyed effectively to screen reader users since they are not part of the associated label or submit button.
If the input
is nested within a label
, both the label and hints can be included inside the label
element:
<label for="email">
<strong>E-mail</strong>
<input type="text" id="email" name="whatever">
<span>ex: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ef4f1f6f0faf1fbdefaf1f3fff7f0b0fdf1f3">[email protected]</a></span>
</label>
This ensures that the hint is spoken along with the corresponding label to assist AT users.
Note: It's important to style the strong
and span
differently – like making strong
bold and right-aligned, and span
italic and left-aligned. While the span
isn't necessary for styling purposes (you can style the label
directly and adjust rules for strong
), using span
simplifies things :)
This technique works well for conveying errors and providing hints in forms:
<p class="error>
<label for="email">
<strong>E-mail</strong>
<input type="text" id="email" name="wrong" value="aaa">
<span>ERROR: Please enter a valid email address. Example: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f050007010b000a2f0b00020e0601410c0002">[email protected]</a></span>
</label>
</p>
.error strong {
color: red;
font-weight: bold;
}
.error input {
border: 1px solid red;
}
.error span {
color: red;
}
By including error messages within the form structure, screen readers can convey this critical information effectively to visually impaired users beyond just visual cues like color changes.