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

笑着哭i 提交于 2019-12-01 19:51:28

According to the HTML5 spec - "If the for attribute is not specified, but the label element has a labelable element descendant, then the first such descendant in tree order is the label element's labeled control."

http://www.w3.org/TR/html5/forms.html#category-label

So basically, no it is not required as long as it is wrapping any of these elements: button, input (if the type attribute is not in the hidden state), keygen, meter, output, progress, select, or textarea

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!