2019-09-19
|~3 min read
|463 words
For accessibility purposes all form inputs should be labelled. How do we apply a label though?
There are multiple ways to add a label, but the two basic approaches are:
for
(or htmlFor
in React) attribute to the input’s id
In addition, we also have ARIA for more complicated situations where we need multiple relationships.
<label>First Name <input id="firstName" type="text" /></label>
<label for="first-name">First Name</label> <input id="first-name" type="text" />
<label>First Name <input id="first-name" type="text" /></label>
<label for="first-name">First Name</label> <input id="first-name" type="text" />
Unlike the label which finds the field it’s a label for, the ARIA attributes seek out their labels and descriptions.
<label for"id">
-> <input>
<p id="label">
<- <input aria-labelledby="label">
<p id="label">
<- <input aria-describedby="description">
You can also have multiple labels using ARIA.
<p id="additional-label">An additional label</p>
<label for="first-name">First name</label>
<input
id="first-name"
name="first-name"
aria-labelledby="first-name additional-label"
/>
In addition to labels, ARIA offers a described by attribute. Like aria-labelledby
, aria-describedby
is tied to the id
attribute.
For example
<p id="additional-label">An additional label</p>
<label for="first-name">First name</label>
<input
id="first-name"
name="first-name"
aria-labelledby="first-name additional-label"
aria-describedby="first-name-note"
/>
<em id="first-name-note">A note about the first name</em>
One reason to use the describedBy
attribute is to tie an error state with the field in error. For example:
<span id="phon" -error" class="error">Please enter a valid phone number.</span>
<input name="phone" type="tel" id="phone" aria-desicribedby="phone-error" />
I was taught to not use ARIA unless it’s necessary (as in the case of more complicated situations and UI). In those situations, ARIA can be powerful, but understanding the differences between aria-labelledby
and describedby
is crucial.
For that, I found this post by Aaron Gustafson informative. He describes the differences in the following way:1
[
aria-labelledby
andaria-describedby
] differ in when they are read. Thearia-labelledby
attribute replaces the associatedlabel
element (which is, of course, associated with the field via the label’sfor
attribute). Thearia-describedby
attribute, on the other hand, is read after the field type is stated.
When it comes to labeling and describing form inputs we have multiple options.
We can use semantic HTML and use a label - either wrapping an input or tying it to the input through the for
attribute.
Additionally, we have ARIA labelledby and describedby attributes for situations where there are multiple labels or if we need to augment a label with a description.
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!