Should I put input elements inside a label element?

前端 未结 14 1830
长情又很酷
长情又很酷 2020-11-22 07:24

Is there a best practice concerning the nesting of label and input HTML elements?

classic way:



        
相关标签:
14条回答
  • 2020-11-22 07:55

    Behavior difference: clicking in the space between label and input

    If you click on the space between the label and the input it activates the input only if the label contains the input.

    This makes sense since in this case the space is just another character of the label.

    <p>Inside:</p>
    
    <label>
      <input type="checkbox" />
      |&lt;----- Label. Click between me and the checkbox.
    </label>
    
    <p>Outside:</p>
    
    <input type="checkbox" id="check" />
    <label for="check">|&lt;----- Label. Click between me and the checkbox.</label>

    Being able to click between label and box means that it is:

    • easier to click
    • less clear where things start and end

    Bootstrap checkbox v3.3 examples use the input inside: http://getbootstrap.com/css/#forms Might be wise to follow them. But they changed their minds in v4.0 https://getbootstrap.com/docs/4.0/components/forms/#checkboxes-and-radios so I don't know what is wise anymore:

    Checkboxes and radios use are built to support HTML-based form validation and provide concise, accessible labels. As such, our <input>s and <label>s are sibling elements as opposed to an <input> within a <label>. This is slightly more verbose as you must specify id and for attributes to relate the <input> and <label>.

    UX question that discusses this point in detail: https://ux.stackexchange.com/questions/23552/should-the-space-between-the-checkbox-and-label-be-clickable

    0 讨论(0)
  • 2020-11-22 08:02

    Personally I like to keep the label outside, like in your second example. That's why the FOR attribute is there. The reason being I'll often apply styles to the label, like a width, to get the form to look nice (shorthand below):

    <style>
    label {
      width: 120px;
      margin-right: 10px;
    }
    </style>
    
    <label for="myinput">My Text</label>
    <input type="text" id="myinput" /><br />
    <label for="myinput2">My Text2</label>
    <input type="text" id="myinput2" />
    

    Makes it so I can avoid tables and all that junk in my forms.

    0 讨论(0)
  • 2020-11-22 08:02

    As most people have said, both ways work indeed, but I think only the first one should. Being semantically strict, the label does not "contain" the input. In my opinion, containment (parent/child) relationship in the markup structure should reflect containment in the visual output. i.e., an element surrounding another one in the markup should be drawn around that one in the browser. According to this, the label should be the input's sibling, not it's parent. So option number two is arbitrary and confusing. Everyone that has read the Zen of Python will probably agree (Flat is better than nested, Sparse is better than dense, There should be one-- and preferably only one --obvious way to do it...).

    Because of decisions like that from W3C and major browser vendors (allowing "whichever way you prefer to do it", instead of "do it the right way") is that the web is so messed up today and we developers have to deal with tangled and so diverse legacy code.

    0 讨论(0)
  • 2020-11-22 08:03

    If you include the input tag in the label tag, you don't need to use the 'for' attribute.

    That said, I don't like to include the input tag in my labels because I think they're separate, not containing, entities.

    0 讨论(0)
  • 2020-11-22 08:03

    See http://www.w3.org/TR/html401/interact/forms.html#h-17.9 for the W3 recommendations.

    They say it can be done either way. They describe the two methods as explicit (using "for" with the element's id) and implicit (embedding the element in the label):

    Explicit:

    The for attribute associates a label with another control explicitly: the value of the for attribute must be the same as the value of the id attribute of the associated control element.

    Implicit:

    To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. In this case, the LABEL may only contain one control element.

    0 讨论(0)
  • 2020-11-22 08:03

    I greatly prefer to wrap elements inside my <label> because I don't have to generate the ids.

    I am a Javascript developer, and React or Angular are used to generate components that can be reused by me or others. It would be then easy to duplicate an id in the page, leading there to strange behaviours.

    0 讨论(0)
提交回复
热议问题