Explain Codes LogoExplain Codes Logo

Can an input field have two labels?

html
accessibility
form-design
screen-readers
Nikita BarsukovbyNikita Barsukov·Dec 3, 2024
TLDR

Yes, an input field can have multiple labels. To do this, provide distinct descriptions for each label, and associate all of them with the same input's id. This adds clarity and improves accessibility in complex forms.

<label for="user-email">Work Email</label> <label for="user-email">Preferred Contact</label> <input type="email" id="user-email" name="email">

The essence of multiple labels

Multiple labels give users a clear understanding of form controls when dealing with intricate data. For instance, an email address input field can have labels like "Login ID" and "Subscription Address," painting a vivid picture of dual utility.

Screen readers and label workflow

For a user relying on screen readers, using multiple labels can be a double-edged sword. While they can add context, screen readers might only announce the first label. To circumvent this, each label should be capable of standing on its own. Alternatively, use <fieldset> to create a shared context.

<fieldset> <legend>Email Address</legend> <label for="user-email">Login ID</label> <input type="email" id="user-email" name="email"> <label for="user-email">Subscription Address</label> </fieldset>

Construction and best practices of multi-label design

User guidance in form layouts

Strive for clarity when using multiple labels to avoid user confusion. By using clear and concise language, and offering processing instructions, you can make sure users understand the connections between each label and the input field.

<label for="user-email">Login ID:</label> <br> <span>Also key in your newsletter subscription here.</span> <br> <label for="user-email">Subscription Address:</label> <input type="email" id="user-email" name="email">

Cultivating accessible error handling

Consider error messages as third-string labels─they serve readability and accessibility. For users with accessibility needs, having error messages that are accessible and user-friendly matters. A summary of errors at the top of the form can acting as a roadmap for navigating inputs.

Correctly linking labels and inputs

Link each label to its input field using the for attribute, which connects to the input's id. Remember, missing pairs equals lost accessibility─like a pirate who lost his pair of boots; it's just not fun!

<label for="user-name">First Name</label> <input type="text" id="user-name" name="username"> <label for="user-name">Given Name</label>

Evaluating the pros and cons

Before embarking on a multiple-labels journey, assess the trade-offs. Does it confuse or enlighten your users? Implements with care, else it's like ordering a pizza with extra toppings─sounds cool, but things can get messy!