Should I put input elements inside a label element?

前端 未结 14 1828
长情又很酷
长情又很酷 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:45

    One thing you need to consider is the interaction of checkbox and radio inputs with javascript.

    Using below structure:

    <label>
      <input onclick="controlCheckbox()" type="checkbox" checked="checkboxState" />
      <span>Label text</span>
    </label>
    

    When user clicks on "Label text" controlCheckbox() function will be fired once.

    But when input tag is clicked the controlCheckbox() function may be fired twice in some older browsers. That's because both input and label tags trigger onclick event attached to checkbox.

    Then you may have some bugs in your checkboxState.

    I've run into this issue lately on IE11. I'm not sure if modern browsers have troubles with this structure.

    0 讨论(0)
  • 2020-11-22 07:47

    Both are correct, but putting the input inside the label makes it much less flexible when styling with CSS.

    First, a <label> is restricted in which elements it can contain. For example, you can only put a <div> between the <input> and the label text, if the <input> is not inside the <label>.

    Second, while there are workarounds to make styling easier like wrapping the inner label text with a span, some styles will be in inherited from parent elements, which can make styling more complicated.

    0 讨论(0)
  • 2020-11-22 07:48

    I prefer

    <label>
      Firstname
      <input name="firstname" />
    </label>
    
    <label>
      Lastname
      <input name="lastname" />
    </label>
    

    over

    <label for="firstname">Firstname</label>
    <input name="firstname" id="firstname" />
    
    <label for="lastname">Lastname</label>
    <input name="lastname" id="lastname" />
    

    Mainly because it makes the HTML more readable. And I actually think my first example is easier to style with CSS, as CSS works very well with nested elements.

    But it's a matter of taste I suppose.


    If you need more styling options, add a span tag.

    <label>
      <span>Firstname</span>
      <input name="firstname" />
    </label>
    
    <label>
      <span>Lastname</span>
      <input name="lastname" />
    </label>
    

    Code still looks better in my opinion.

    0 讨论(0)
  • 2020-11-22 07:48

    A notable 'gotcha' dictates that you should never include more than one input element inside of a <label> element with an explicit "for" attribute, e.g:

    <label for="child-input-1">
      <input type="radio" id="child-input-1"/>
      <span> Associate the following text with the selected radio button: </span>
      <input type="text" id="child-input-2"/>
    </label>
    

    While this may be tempting for form features in which a custom text value is secondary to a radio button or checkbox, the click-focus functionality of the label element will immediately throw focus to the element whose id is explicitly defined in its 'for' attribute, making it nearly impossible for the user to click into the contained text field to enter a value.

    Personally, I try to avoid label elements with input children. It seems semantically improper for a label element to encompass more than the label itself. If you're nesting inputs in labels in order to achieve a certain aesthetic, you should be using CSS instead.

    0 讨论(0)
  • 2020-11-22 07:48

    I usually go with the first two options. I've seen a scenario when the third option was used, when radio choices where embedded in labels and the css contained something like

    label input {
        vertical-align: bottom;
    }
    

    in order to ensure proper vertical alignment for the radios.

    0 讨论(0)
  • 2020-11-22 07:52

    Referring to the WHATWG (Writing a form's user interface) it is not wrong to put the input field inside the label. This saves you code because the for attribute from the label is no longer needed.

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