I am making a lengthy form which contains, among other things, questions where users may check multiple answers. The basic code is the following (I am using Semantic UI for basic styling):
<form class="ui form new-form" action="#" method="get">
<div class="ui form">
<label>What fruits do you like?</label>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" name="fruits" value="apples">
<label>apples</label>
</div>
</div>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" name="fruits" value="bananas">
<label>bananas</label>
</div>
</div>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" name="fruits" value="dylan-carty">
<label>pears</label>
</div>
</div>
</div>
</form>
I want to apply my labels using for/id for accessibility reasons.
In this case, the top label ("What fruits do you like?") refers to the entire set of check-boxes. If this were a select/option set, I understand you would apply the ID to the containing <select>
tag. I see there being three possible solutions.
- Put all the checkboxes in a
<div>
, and give it the same ID as thefor
value in the "What fruits do you like?" label. Give each checkbox label an ID that matches its input. This leaves Semantic UI's existing styling for checkboxes alone, but I don't know semantic it is. - Put all the checkboxes in a
<fieldset>
tag, and give it the same ID as thefor
value in the "What fruits do you like?" label. Give each checkbox label an ID that matches its input. This seems more semantic to me, but since Semantic UI gives<fieldset>
a style, I then need to override that style. - Wrap the "What fruits do you like?" label around the entire set of checkboxes, as shown in Method 1 here.
Research:
- Searched for questions containing "multiple checkboxes", but most deal with cases where each checkbox is distinct, not part of a larger group, or with how to select them
- Searched in general for "best practices checkbox group id" and did come up with this, which suggests putting a
<legend>
inside a<fieldset>
. However, I'd prefer not to use<label>
for all my other inputs, and switch to a<legend>
tag solely for checkbox groups.
Is there a generally accepted "best practice" in this scenario?