If an element is wrapped by a label, does the label require the “for” attribute?

前端 未结 2 511
夕颜
夕颜 2021-01-18 23:31

Say I have a set of radio s. I\'m not a caveman, so I know I need to associate with those s. I

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 00:13

    By the specifications, you don’t need the for attribute when the control element is wrapped inside a label element. This principle also applies to all modern browsers, though some very old versions of IE supported only the explicit association with for attributes.

    People may still prefer to use the for attribute on logical grounds: a control is logically not part of a label, so it should be placed outside it. And then you need the for attribute in order to benefit from label markup at all.

    The for attribute is necessarily when the control cannot be a descendant of a label element, e.g. when you have labels in one column of a table element, controls in another column.

提交回复
热议问题