Single label for two inputs
Solution 1
In this case I think the best markup would be to wrap the inputs in a fieldset
, use a legend
for "Dates", use labels
for both inputs
and hide the labels:
HTML
<fieldset>
<legend>Dates</legend>
<label for="startdate">Start Date</label>
<input type="text" name="startdate" id="startdate" placeholder="Start Date">
<label for="enddate">End Date</label>
<input type="text" name="enddate" id="enddate" placeholder="End Date">
</fieldset>
CSS
fieldset {
border: none;
}
label {
margin-left: -999em;
}
input {
display: block;
}
Also see: WCAG 2, H71: Providing a description for groups of form controls using fieldset and legend elements
Solution 2
I think your best bet would to be to include a label with each input. Then for the label that you don't want to actually display on the page can simply be set off page using CSS via
.hide {
position: absolute;
left: -9999em;
}
...or something similar
Solution 3
The issue here is that you can only specify one for
for each label. I would imagine, for accessibility purposes it would be best to show two labels
one for each date. In this way anyone using a screen reader, etc. will get a valid label for each input.
<label for="x">x name</label><input name="x"/>
<label for="y">y name</label><input name="y"/>
AverageMarcus
Updated on July 25, 2022Comments
-
AverageMarcus almost 2 years
I'm currently adding some date input to a form. I have a 'Start Date' and 'End Date' input but only want to use a single label ('Dates') for both inputs.
Is it possible to do this? What are the accessibility concerns?
My current thinking is to have a label 'Dates' that is shown then have two hidden labels for each input for screen readers etc. Is this the way to go? Are there any examples of large websites doing this kind of thing (Government websites if possible)?
This is a project that may be user by a government agency so there are strict rules on it complying with accessibility.
-
AverageMarcus about 11 yearsDo you suggest using hidden labels that are just for screen readers?
-
Liam about 11 yearsnot 100% sure what the recommendations are on hidden labels. I wouldn't be surprised if some readers wouldn't read hidden values, I suppose this would depend on your definition of hidden also!
-
AverageMarcus about 11 yearsSurely
Display:none
would be better thanposition:absolute
? -
AverageMarcus about 11 yearsReally? This is news to me. Do you have any examples?
-
Billy Moat about 11 years@AverageMarcus - Here you go: css-tricks.com/places-its-tempting-to-use-display-none-but-dont
-
Liam about 11 yearsThe reader (or the person using the reader) would get confused if you had elements that are shown dynamically. As it would read all the hidden elements whether they were applicable or not.
-
AverageMarcus about 11 yearsWow. Thanks, guess I might need to re-think this a little bit.
-
AverageMarcus about 11 yearsIm going to add this additional link as reference for others: alistapart.com/article/now-you-see-me
-
Jukka K. Korpela about 11 yearsThis is the most accessibility-friendly approach, and avoids creating the problem in the first place. It is conceptually simple and understandable, which is an important accessibility feature too. People with cognitive difficulties need to have a simple and understandable association between a text that explains expected input and a control for the actual input. For this reason, labels should not be hidden in visual rendering.
-
simontemplar over 10 yearsIsn't the point of display: none; to hide it? Surely if you don't want a sighted person to see it, you don't want a screen reader to read it either. It seems like an unnecessary overreach in accessibility to not properly hide something you want hidden, because... you don't want it hidden for certain people? That makes no sense.