将标签标签包裹在表单项目上还是使用“for”属性在HTML中? [英] Is it better to wrap the label tag around a form item or use the "for" attribute in HTML?

查看:117
本文介绍了将标签标签包裹在表单项目上还是使用“for”属性在HTML中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你们可以同时使用,但使用一种更好吗?如果是这样,为什么?

I know that you can use both but is it better to use one over the other? If so, why?

for属性的示例

Example of "for" attribute:

<input type="text" id="male"><label for="male">Male</label>

示例:

<label>Age:<input type="text"></label>


推荐答案

语义上,两种可能性都是相同的。但取决于你想要的布局,这两种可能性都有其优点和缺点。例如,如果您希望标签位于完全不同的位置,则将输入放入标签是没有意义的。但是如果你想通过css做一个悬停效果,那设置e。 G。标签和输入周围区域的背景,最好将输入放入标签中。

Semantically, both possibilities are the same. But depending on what layout you want, there are advantages and disadvantages for the two possibilites. For example, if you want that the label is at an entirely different place, it would not make any sense to put the input into the label. But if you want to be able to make a hover-effect via css, that sets e. g. a background for both the label and the area around the input, it would be better to put the input into the label.

这篇关于将标签标签包裹在表单项目上还是使用“for”属性在HTML中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆