开发者

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

开发者 https://www.devze.com 2023-01-06 01:41 出处:网络
Where to putaround other html element or not? Option1- put 开发者_如何学编程around input element :

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消