Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML HTML Forms Choosing Options Checkboxes

Chloe Milne
Chloe Milne
323 Points

Why would you use the <label> attribute as opposed to something like <h1>? Are there any main differences/reasons?

Why would you use the <label> attribute as opposed to something like h1? Are there any main differences/reasons?

When we don't associate the label with anything in particular (aka label for="") what would be the benefit to using it at all? Is it purely because of the css rules associated with "label"?

3 Answers

Semantic markup encourages us to use tags/elements that describe the meaning of the content. In a form, the word(s) outside the INPUT (data entry box) are more clearly described as a LABEL than they are a heading like an H1 or H2, or a generic piece of text like a P.

In addition, there is the helpful behavior you mentioned, which is really quite nice. When associated with an INPUT, a user "click" on the words in the LABEL will move their insertion point inside the data entry box. An H1 or P won't do that.

Evan Agee
Evan Agee
13,088 Points

I think the main reason for using a label tag is semantic markup. Whereas h1 is meant to indicate the weight of a piece of content in the overall doc outline, labels indicate that a piece of content is a label for a form element on the page.

Jason Neel
Jason Neel
11,073 Points

If that's the argument, using a label in this case is for semantic's sake, why not use another fieldgroup/legend combination? After all, you are defining a group of fields (checkmark or radio inputs) for one particular value that could then be described by the fieldgroup's legend.

But I realize there could be many solutions to this pattern.