I see all over the web two ways that people code checkboxes, which one is correct?
<input id="a" type="checkbox"/><label for="a">checkbox</label><label for="b"><input id="b" type="checkbox">checkbox</label>
They both work fine in Chrome, is one more cross browser than the other? Is there any difference?
Both are perfectly correct, valid and accessible as long as you associate
inputandlabelwithfor/idattributes, even when theinputis right into the label` (see the best answer to the question linked by @AramKocharyan and my comment there)Screen readers will read the associated label content when its user focus on a form element (input, select or textarea). When in a form, chances are they’ll only read labels, legends and buttons (I mean reset, submit, image or button) because JAWS and alike have special modes; roughly the aim is to fill a form, not to read the content as with the rest of a web page.
You’ll often find forms with a
labelon the left,inputon center and some help on the right:inputoutside thelabelelement, the hint will be coded with aspanfor example. It won’t be heard by screen readers because it’s not part of alabelor submit button.With an
inputinside alabelelement, you can put both the label and the hint into thelabelelement:That way, the hint will be read to the AT user along with the real label.
Note: of course you’ll style the
strongandspandifferently, say resp. bold right-aligned and italic left-aligned.spanisn’t necessary for styling (just stylelabeland cancel half of rules forstrong) but it’s more simple (simpler?) 🙂It’s as useful for errors as for hints:
That way, the error is read to screen readers (and color that they can’t see or barely see isn’t the only way of conveying information to them with the help of this text).