TL;DR: Regardless how small a component, always make sure you consider a variety of different users and their abilities while implementing. On first glance, input elements such as a checkbox may seem like an easy fix - but there are still a few things to consider in order to make sure they are accessible.
This series will tackle different kinds of input elements and how to make them more accessible, starting with the basics: using the correct input type and associating a label.
Let's have a look.
When using input elements in HTML, we want to make sure to always set the correct input type for the corresponding element. The default input type is text and may work in a range of cases, but being as specific as possible helps all your users - and comes with a bunch of inherent functionality. for example defines a numeric input field, opens the number pad on mobiles instead of the keyboard and you can restrict the range by using minimum and maximum numbers. masks the corresponding input and therefore protects the users privacy. Depending on the browser, opens a date picker,... and so on. By choosing the correct input type, we make our own life easier, being able to use certain inherit functionalities in HTML, and improve user experience at the same time.
Regardless of what type of input you are using, make sure you associate a label to your input field. Usually, this can be done with a
<label for="firstname">First name:</label> <input type="text" name="firstname"> <h3> aria-label vs. aria-labelledby </h3> <p>In a case in which a visual label is not an option or can't be associated with input element in the above mentioned way, aria-labels can also be used to identify form controls. If you are familiar with ARIA a little bit, you may be aware that the preferred option, if possible, is to work without aria whenever possible - however, in the real life of course, this isn't always feasible. </p> <h4> aria-label </h4> <p>An aria-label can be used when no visible text label is present, for example, when an icon button is used without any text.<br> </p> <pre class="brush:php;toolbar:false"><button aria-label="Search"><img src="search-icon.svg" alt=""></button>
When another element is used as reference for labelling the input, aria-labelledby comes in handy.
<div role="dialog"> <p>Whenever possible, try to use semantic HTML to provide accessible names for your input elements. Only if you've made sure it's not possible in this specific case, revert to using aria attributes. </p> <p>Once we've got these basics down, like using semantic structure, adding the correct input type and associating labels, we can have a closer look at the other aspects of making our input elements more accessible, such as disabled input elements, focus style & color contrasts and expected keyboard navigation. </p> <p><strong>Resources:</strong></p>
The above is the detailed content of Accessible Input Elements | the Basics. For more information, please follow other related articles on the PHP Chinese website!