Label not associated to input

Travis Johnston picture Travis Johnston · Nov 28, 2018 · Viewed 12.5k times · Source

Using the built in Chrome Lighthouse to run accessibility audit on a WP site showed the signup field as being invalid due to no associated label. I've run into this before and I can't understand why it doesn't think it's associated. I can get it to pass by adding an aria-label tag to the input directly but I shouldn't have to do that.

Here is a code snippet that is created from Subscribe2

<label for="s2email">Your email:</label>
<br>
<input type="text" name="email" id="s2email" value="Enter email address..." size="20" onfocus="if (this.value === 'Enter email address...') {this.value = '';}" onblur="if (this.value === '') {this.value = 'Enter email address...';}">

Can be found here: https://blog.collaborative.org/

Answer

Tsundoku picture Tsundoku · Nov 29, 2018

When you look at the code sample in isolation, it looks fine: it has a label explicitly associated with the input element using the for and id attributes.

However, when you inspect the site, it looks different: due to the style rule .s2_form_widget label { display: none; } the label is invisible. (You can see this when you use the Inspector in Firefox, move to the label in the DOM tree and check the associated styles.)

Note that display: none does not only hide the label visually but also for screen readers. As a result, the input element has no label from the point of view of a screen reader or another assistive technology and fails the requirement that form controls need explicit labels. (As an alternative, you might try hiding the label off-screen with a negative margin.)