Where to put <label> around other html element or not?

大憨熊 提交于 2019-12-11 09:55:32

问题


Where to put around other html element or not?

Option1- put around input element :

<label>Url:
        <input type="text" size="350"/>
</label>

Option2:

<label>Url:</label>
<input type="text" size="350"/>

Thanks


回答1:


The second is definitely better, it allows you to style separately.

You can also use the "for" attribute to bind it to an input field:

http://www.w3schools.com/tags/att_label_for.asp




回答2:


The latter. This way you can set style, width etc. without affecting <input>. It's also better semantically: The label is a label, and input is an input.




回答3:


Firstly, in the first example the <label> is automatically linked to the <input>, while in the second example they are not (you must set the for and id attributes to emulate the former's behaviour).

Other than that, it's a matter of situation and preference. Personally I usually go for the former as there's less markup needed.



来源:https://stackoverflow.com/questions/3186952/where-to-put-label-around-other-html-element-or-not

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