Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single label for two inputs

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.

like image 908
AverageMarcus Avatar asked Feb 13 '13 12:02

AverageMarcus


1 Answers

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;
}

Fiddle here

Also see: WCAG 2, H71: Providing a description for groups of form controls using fieldset and legend elements

like image 104
steveax Avatar answered Oct 24 '22 12:10

steveax