What is the correct method for using label elements?

During the “Learn HTML by Building a Cat Photo App” we are taught two different methods of using label elements with inputs.

  1. Nesting the entire input element within a label element.
<label><input type="radio"> Indoor</label>
  1. Nesting just the text of an input element in a label, whilst also using a “for” tag.
<input id="loving" type="checkbox"> <label for="loving">Loving</label>

Which of these two methods is best practice?
What is the functional difference between the two?

you can use it almost interchangeably, if im not mistaken, except if your “label” is at some place and “form control” is at some other place.

even though its recommended and consider best practice to keep both “label” and “form control” next to each other rather than far apart

just a tid bit: its mostly depends on your preference, whether you go with nesting or not, but if you are not then you have to use “for and id” to reference them correctly

try this amazing mdn article detailing about its use cases, happy learning :slight_smile: <label>: The Input Label element - HTML: HyperText Markup Language | MDN

2 Likes

Thank you so much for your clear explanation :slight_smile:

1 Like

glad i could help :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.