Just to avoid confusion the correct method for labelling an input is to simply use a <label>
.
So you should have
<label for="firstName">First Name</label>
<input id="firstName" name="firstName" type="text"/>
The way to link labels and inputs is using the for
attribute and point that at the input's ID.
The added benefit of this is that if you click on the label
it will focus the corresponding input
, which other solutions will not.
Should you for some reason require an input without a label then the following example illustrates how to do this correctly. (please do not do this if you can avoid it, labels are important for people with anxiety disorders to be able to check that they have filled in the correct field - however I know that sometimes 'designers' just won't budge and you have to workaround them....)
In this example we 'visually hide' the label using CSS and add placeholder
text to the element. Just to reiterate this is a last resort for those designers who will not listen about accessibility and you should use visible labels.
At least doing it this way the input will function correctly for screen reader users.
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
white-space: nowrap; /* added line */
}
<label for="firstName" class="visually-hidden">First Name</label>
<input id="firstName" name="firstName" placeholder="First Name" type="text"/>
Edit
Finally got chance to test this, works in Firefox, Internet Explorer and not in Chrome for announce on hover.
However if the label is visible it does work fine (if you hover the label, it does not announce if you hover the input itself even with a visible label), it also works fine for if you focus
the input
.
Final thoughts - show the labels (third time I said this in one answer. hehe), problem solved, no need to make it complicated.
Also, I am not sure why you think this is important, if someone uses a screen reader to assist while using a mouse they will click on an input, I have never come across anyone who would find not having a form field read on hover an accessibility issue if it works correctly once you click into the field.
Also the only people possibly affected by this are:-
- screen reader users,
- who use the mouse,
- who have a sight impediment,
- who use Google Chrome,
- who also use NVDA,
- and do not use a screen magnifier.
pretty specific, so not likely an issue.