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.
<div>
, and give it the same ID as the for
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.<fieldset>
tag, and give it the same ID as the for
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.Research:
<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?
From a semantic viewpoint, your research has already given you the answer.
Use fieldset
and legend
.
When it comes to semantics ignore default styling, be it native HTML or any css framework you are using.
The fieldset
and legend
tags are designed for exactly this purpose. If you want to be pedantic, theoretically the entire contents of the form should also be wrapped in a fieldset
tag with a legend
as that is you "superset" of fields.
Making minimal changes to your HTML you would want something like:
<form class="ui form new-form" action="#" method="get">
<div class="ui form">
<fieldset>
<legend>What fruits do you like?</legend>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" name="fruits" value="apples" id="fruit_apples">
<label for="fruit_apples">apples</label>
</div>
</div>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" name="fruits" value="bananas" id="fruit_bananas">
<label for="fruit_bananas">bananas</label>
</div>
</div>
<div class="field">
<div class="ui checkbox">
<input type="checkbox" tabindex="0" name="fruits" value="dylan-carty" id="fruit_pears">
<label for="fruit_pears">pears</label>
</div>
</div>
</fieldset>
</div>
</form>
Now you need to tweak the CSS to something that works for you.
Now comes the tricky part. Web development and programming in general is a balancing act. It's good to know what are considered "best practices", but it's also important to know these are guidelines, not rules. It's up to you to work out what to keep and what to ignore from these guidelines in order to keep your work maintainable and deliverable.
What would I do? Given the info provided, use fieldset
and legend
.