Labels and Inputs
Using input and form controls to create accessible forms
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The Approachable Guide to Accessible Components course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The Approachable Guide to Accessible Components, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:14] Labels and inputs are cornerstones of the web and are imperative to be able to retrieve information from the end user. In this lesson, we'll cover how to pair labels and inputs and how they work in tandem to provide an accessible experience for all.
[00:15 - 00:25] Labels and inputs go together like two P's in a pod, as the two elements typically need to be used in conjunction with one another. Inputs play a crucial role on gathering information in our ubiquitous throughout the web.
[00:26 - 00:33] For example, like the contact form that you see here. Very important, labels provide context and guidance for the associated input.
[00:34 - 00:47] Inputs that rely solely on attributes like placeholder text or icons to convey their purpose exclude users who rely on assistive technology. When labels and inputs are properly associated, they offer significant accessibility advantages, including convenient interaction.
[00:48 - 01:04] So when you associate a label with an input, it allows users to simply click on the label to interact with the corresponding input. The expanded target area for input focus offers a benefit to all users, including those utilizing touchscreen devices, as it facilitates easier interaction of the input element.
[01:05 - 01:13] There's also enhanced screen reader support. Directly associating labels with inputs ensures that screen readers announce the label when the associated input receives focus.
[01:14 - 01:25] This provides users with clear and contextual information about the purpose and function of the input. And as you can see, the screen reader is reading out the labels here for the contact form.
[01:26 - 01:37] Now we'll go through some examples of the two different ways to associate a label with an input. The first approach is what we call explicit association.
[01:38 - 01:45] And to achieve this, you need to do two steps. The first is assign an ID attribute to the input element.
[01:46 - 02:00] In this case, we'll add an ID with the value of email-address. Second, we need to apply the four attribute of the label element, ensuring that the value of the four attribute matches the ID value of the corresponding input element.
[02:01 - 02:14] On the label here, we'll add a four attribute and give it the same value that we gave it to the ID, which was email-address. Alternatively, you can nest the input elements directly within the label tag.
[02:15 - 02:25] When this approach is used, the four and ID attributes are no longer required as the association is implicit. As you see here, we'll add a new label.
[02:26 - 02:42] And this time, we'll add an input with a type of checkbox, and we'll just nest that input within the label, forming that implicit association. Now we'll take a look at an accessible-friendly way of hiding a label.
[02:43 - 02:52] Occasionally, you may want the ability to hide a label. For example, in certain situations where the purpose of the content may be evident from the visual context.
[02:53 - 03:14] While the label can technically be visually hidden, we have to ensure that we think about this from an accessibility standpoint, so that individuals using assistive technology are taken into consideration and have a comfortable user experience. Although there are one-line styles like Display None and Visibility Hidden, which will achieve the desired visual effect, this is not suitable for screen-meter users.
[03:15 - 03:25] A commonly employed technique uses CSS to keep information accessible to assist ive technology, but visually hidden. This approach presents the label within a one-by-one pixel area.
[03:26 - 03:34] I show in the example using the CSS class visually hidden. So we would just add a class while visually hidden, like I said.
[03:35 - 03:45] And then we would take that class and apply it to the respective label element. So in this case, we'll apply it to the first label element for email address.
[03:46 - 03:57] And if you notice when we do that, the label gets removed from the UI. And when you apply this technique properly, you still include the label element in the HTML.
[03:58 - 04:02] You just apply the class and then that removes it from the DOM. And we'll see.
[04:03 - 04:14] So if we take a look and actually inspect this input, you'll notice that the label is still in the DOM. The class are just being applied to it.
[04:15 - 04:28] And if we look at the accessibility tree, it still notices that the email address label is there, which is exactly what we want. And if we take a look at MDN itself, they are employing the same technique with their search input.
[04:29 - 04:31] They have a label. They have a class called visually hidden, just like we do.
[04:32 - 04:41] The label is search MDN, but you don't see it, but it's still accessible to the accessibility tree. Again, this is a very common technique that's used.