Using “label for” on radio buttons

2019-01-30 20:42发布

When using the "label for" parameter on radio buttons, to be 508 compliant*, is the following correct?

 <label for="button one"><input type="radio" name="group1" id="r1" value="1" /> button one</label> 

or is this?

 <input type="radio" name="group1" id="r1" value="1" /><label for="button one"> button one</label>

Reason I ask is that in the second example, "label" is only encompassing the text and not the actual radio button.

*Section 508 of the Rehabilitation Act of 1973 requires federal agencies to provide software and website accessibility to people with disabilities.

2条回答
成全新的幸福
2楼-- · 2019-01-30 20:58

You almost got it. It should be this:

<input type="radio" name="group1" id="r1" value="1" /><label for="r1"> button one</label>

The value in for should be the id of the element you are labeling.

查看更多
We Are One
3楼-- · 2019-01-30 21:02

Either structure is valid and accessible, but the for attribute should be equal to the id of the input element:

<input type="radio" ... id="r1" /><label for="r1">button text</label>

or

<label for="r1"><input type="radio" ... id="r1" />button text</label>

The for attribute is optional in the second version (label containing input), but IIRC there were some older browsers that didn't make the label text clickable unless you included it. The first version (label after input) is easier to style with CSS using the adjacent sibling selector +:

input[type="radio"]:checked+label {font-weight:bold;}
查看更多
登录 后发表回答