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 Radio Buttons

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

what's the reason he used<input> element 1st and then used <label> and gave for and class to it!

opposite to what he has done previously, and din't separate them either... wouldn't it be easier if he had? shall we think it's a best practise while using type="radio" or something else? i just wrote the way i thought was better for me to understand , and it worked exactly the same!!

<label>Age:</label>
                <label for="under_13" class="light">Under 13</label>
                <input type= "radio" id="under_13 "value="under_13" name="user_age">
                <br>
                <label for="over_13" class= "light">Over 13</label>
                <input type="radio" id="over_13" name="user_age" value="over_13">

1 Answer

Steven Parker
Steven Parker
231,275 Points

For some input types, it looks better to place the label before the control. For radio buttons, it's often preferable to have the label after the control.

I don't think this would fall under "best practice", it's really more about what kind of look you want the form to have.

Not separating the elements also affects how they are shown on the page. That keeps the browser from moving the label further away from the control.

The class assignment is unrelated to the element order, it's just there to facilitate styling (and/or scripting). What ties a label with an input is the "for" property of the label matching with the "id" property of the input.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

Thanks steven. i understand but the way i see <label> is the way they appear in the <form> and on the web,and as i am writing my markup ,in my head it just fits better... do you think i should keep it like above markup or follow nick in the video?

Steven Parker
Steven Parker
231,275 Points

It produces a different look. Which look do you want to present? Leave it as you have it if you prefer to have the control come after the text.

But I think for general use, most people expect a radio button group to have the controls line up vertically on the left.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

yes i think i got the hang of it now after writing couple of times! and you are right normally looks better to have it before text vertically! thanks steven