This question already has answers here:
Radio buttons and label to display in same line
(15 answers)
Closed 7 years ago.
I have HTML form which contains labels and input (radio buttons). I used clear:both; and float:left; for aligning the labels on left and input on the same line front of it, and it worked but this also affect the radio buttons. I have two labels followed by radio button and then again one label and followed by radio button. How can I align these all (2 labels + radio button + label + radio button) all in one line?
I tried to use float:none and display:inline; but it did not work. The labels and radio buttons appear in the bottom of the label.
form label {
width: 225px;
font-weight: bold;
float: left;
clear: both;
}
form {
width: 70%;
}
form input {
float: left;
}
form input #yes,
form input #no {
display: inline;
float: none;
}
form label[for="yes"],
form label[for="no"] {
display: inline;
}
<form>
<label for="fname">First name:</label>
<input id="fname" name="fname" type="text">
<label for="lname">Last name:</label>
<input id="lname" name="lname" type="text">
<label for="email">What is your email address?</label>
<input id="email" name="email" type="email">
<label for="when">When did it happen?</label>
<input id="when" name="when" type="text">
<label for="howlong">How long were you gone?</label>
<input id="howlong" name="howlong" type="text">
<label for="howmany">How many did you see?</label>
<input id="howmany" name="howmany" type="text">
<label for="desc">Describe them:</label>
<input id="desc" name="desc" type="text">
<label for="whatdo">What did they do to you?</label>
<input id="whatdo" name="whatdo" type="text">
<label for="seen">Have you seen my dog Fang?</label>
<label for="yes">Yes</label>
<input id="yes" name="seen" type="radio" value="yes">
<label for="no">No</label>
<input id="no" name="seen" type="radio" value="no">
</form>
I want to achieve this with CSS not HTML. There are answers that explain how to do it by restructuring the HTML, but I want to keep the same HTML structure.
Just need to reset a couple of the rules on those label and input elements.
form label[for="yes"],
form label[for="yes"] + input,
form label[for="no"],
form label[for="no"] + input {
clear: none;
width: auto;
}
form label {
width: 225px;
font-weight: bold;
float: left;
clear: both;
}
form {
width: 70%;
}
form input {
float: left;
}
form label[for="yes"],
form label[for="yes"] + input,
form label[for="no"],
form label[for="no"] + input {
clear: none;
width: auto;
}
<form>
<label for="fname">First name:</label>
<input id="fname" name="fname" type="text">
<label for="lname">Last name:</label>
<input id="lname" name="lname" type="text">
<label for="email">What is your email address?</label>
<input id="email" name="email" type="email">
<label for="when">When did it happen?</label>
<input id="when" name="when" type="text">
<label for="howlong">How long were you gone?</label>
<input id="howlong" name="howlong" type="text">
<label for="howmany">How many did you see?</label>
<input id="howmany" name="howmany" type="text">
<label for="desc">Describe them:</label>
<input id="desc" name="desc" type="text">
<label for="whatdo">What did they do to you?</label>
<input id="whatdo" name="whatdo" type="text">
<label for="seen">Have you seen my dog Fang?</label>
<label for="yes">Yes</label>
<input id="yes" name="seen" type="radio" value="yes">
<label for="no">No</label>
<input id="no" name="seen" type="radio" value="no">
</form>
Or, simply select all the radio input elements.
form label[for="yes"],
form label[for="no"],
form input[type="radio"] {
clear: none;
width: auto;
}
form label {
width: 225px;
font-weight: bold;
float: left;
clear: both;
}
form {
width: 70%;
}
form input {
float: left;
}
form label[for="yes"],
form label[for="no"],
form input[type="radio"] {
clear: none;
width: auto;
}
<form>
<label for="fname">First name:</label>
<input id="fname" name="fname" type="text">
<label for="lname">Last name:</label>
<input id="lname" name="lname" type="text">
<label for="email">What is your email address?</label>
<input id="email" name="email" type="email">
<label for="when">When did it happen?</label>
<input id="when" name="when" type="text">
<label for="howlong">How long were you gone?</label>
<input id="howlong" name="howlong" type="text">
<label for="howmany">How many did you see?</label>
<input id="howmany" name="howmany" type="text">
<label for="desc">Describe them:</label>
<input id="desc" name="desc" type="text">
<label for="whatdo">What did they do to you?</label>
<input id="whatdo" name="whatdo" type="text">
<label for="seen">Have you seen my dog Fang?</label>
<label for="yes">Yes</label>
<input id="yes" name="seen" type="radio" value="yes">
<label for="no">No</label>
<input id="no" name="seen" type="radio" value="no">
</form>
form label {
width: 225px;
font-weight: bold;
float: left;
clear: both;
}
form {
width: 70%;
}
form input {
float: left;
}
form .radio{
display: inline-block;
float: left;
width: 25px;
clear:none
}
<form>
<label for="fname">First name:</label>
<input id="fname" name="fname" type="text">
<label for="lname">Last name:</label>
<input id="lname" name="lname" type="text">
<label for="email">What is your email address?</label>
<input id="email" name="email" type="email">
<label for="when">When did it happen?</label>
<input id="when" name="when" type="text">
<label for="howlong">How long were you gone?</label>
<input id="howlong" name="howlong" type="text">
<label for="howmany">How many did you see?</label>
<input id="howmany" name="howmany" type="text">
<label for="desc">Describe them:</label>
<input id="desc" name="desc" type="text">
<label for="whatdo">What did they do to you?</label>
<input id="whatdo" name="whatdo" type="text">
<label for="seen">Have you seen my dog Fang?</label>
<label class="radio" for="yes">Yes</label>
<input class="radio" id="yes" name="seen" type="radio" value="yes">
<label class="radio" for="no">No</label>
<input class="radio" id="no" name="seen" type="radio" value="no">
</form>
Well, you have clear: both in your form label rule, so every label will begin in a new line. Add an additional class to the last three labels whose CSS rule contains clear: none
I think this is what you want, no css needed, just use a table
<form>
<table>
<tr><td>
<strong>First Name:</strong>
</td></tr>
<tr><td>
<input id="fname" name="fname" type="text">
</td></tr>
<tr><td>
<strong>Last Name:</strong>
</td></tr>
<tr><td>
<input id="lname" name="lname" type="text">
</td></tr>
<tr><td>
<strong>What is your email address?</strong>
</td></tr>
<tr><td>
<input id="email" name="email" type="text">
</td></tr>
<tr><td>
<strong>When did it happen?</strong>
</td></tr>
<tr><td>
<input id="when" name="when" type="text">
</td></tr>
<tr><td>
<strong>How long where you gone?</strong>
</td></tr>
<tr><td>
<input id="howlong" name="howlong" type="text">
</td></tr>
<tr><td>
<strong>How many did you see?</strong>
</td></tr>
<tr><td>
<input id="howmany" name="howmany" type="text">
</td></tr>
<tr><td>
<strong>Describe them:</strong>
</td></tr>
<tr><td>
<input id="desc" name="desc" type="text">
</td></tr>
<tr><td>
<strong>What did they do?</strong>
</td></tr>
<tr><td>
<input id="whatdo" name="whatdo" type="text">
</td></tr>
<tr><td>
<strong>Have you seen my dog Fang?</strong>
</td></tr>
<tr><td>
<input id="yes" name="seen" type="radio" value="yes">Yes
<input id="no" name="seen" type="radio" value="no">No
</td></tr>
</table>
</form>
Related
I have a form on a page in which the fieldsets create another scrollbar in the page, and makes the form scrollable. This is so weird, and its even weirder that I cant find any sort of information or solution on this anywhere. It should just continue on in the page, I have no size restriction for the form.
Here is my HTML for the form
<form>
<fieldset>
<legend>Customer Information:</legend>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="area">Please describe your concern:</label><br>
<textarea name="area" id="area" rows="5" cols="30"></textarea><br><br>
</fieldset><br>
<fieldset>
<legend>Product Information:</legend>
<label for="pname">Product name:</label>
<input type="text" id="pname" name="pname"><br><br>
<label for="date">Date of Purchase:</label>
<input type="date" id="date" name="date"><br><br>
<label>Select your warranty plan:</label> <br>
<input type="radio" id="none" name="plan" value="none">
<label for="none">None</label><br>
<input type="radio" id="six" name="plan" value="six">
<label for="css">6 Months</label><br>
<input type="radio" id="twelve" name="plan" value="twelve">
<label for="javascript">12 Months</label><br>
<input type="radio" id="eighteen" name="plan" value="eighteen">
<label for="css">18 Months</label><br>
<input type="radio" id="twentyfour" name="plan" value="twentyfour">
<label for="javascript">24 Months</label><br><br>
<label>Select which THE BOARD SHOP locations you have shopped at: </label><br>
<input type="checkbox" id="Toronto" name="Toronto" value="Toronto">
<label for="Toronto"> Toronto</label><br>
<input type="checkbox" id="Montreal" name="Montreal" value="Montreal">
<label for="Montreal"> Montreal</label><br>
<input type="checkbox" id="California" name="California" value="California">
<label for="California"> California</label><br>
<input type="checkbox" id="New York" name="New York" value="New York">
<label for="New York"> New York</label><br>
<input type="checkbox" id="Cairo" name="Cairo" value="Cairo">
<label for="Cairo"> Cairo</label><br>
</fieldset><br>
<input type="submit" value="Submit" id="submit">
</form>
and the CSS
form {
margin: 0 auto;
width: 50%;
}
textarea {
resize: none;
}
label,
legend,
input {
font-family: "Kanit", sans-serif;
}
I'm just learning how forms, inputs, and fieldsets are working, so I'm trying to make replicate the image above with my own code. So far, I have been able to figure out most of it, but I don't know how to properly put the button in line with the bottom of the third div. I tried a span tag with vertical-align: bottom, but that did not work. Also, I tried to make a div and use vertical-align bottom, which didn't work either. I think I just have a poor understanding of vertical-align, so if you could help it would be greatly appreciated.
<body style="background-color:#EEEEEE">
<form name="data" action="https://www.hats.com" method="POST">
<fieldset style="width: 750px;">
<legend>Ordering Information</legend>
First Name: <input type="text" name="first" placeholder="Joey" size="25">
      Last Name: <input type="text" name="last" placeholder="Shmoey" size="25">
<br> <br> Street Address: <input type="text" name="address" placeholder="1234 Sesame Street" size="30">
State: <input type="text" name="state" placeholder="PA" maxlength="2" size="2">
City: <input type="text" name="city" placeholder="York">
Zip:<input type="text" name="zip" placeholder="17402" maxlength="5" size="8">
</fieldset>
<fieldset style="width: 750px;">
<legend>Payment and Shipping</legend>
<div style="width: 250px; float: left;">Payment:<br>
<input type="radio" name="payment" value="Visa">Visa<br>
<input type="radio" name="payment" value="MasterCard">MasterCard<br>
<input type="radio" name="payment" value="Paypal">Paypal</div>
<div style="width: 250px; float: left;"> Shipping:<br>
<input type="radio" name="shipping" value="Priority $7.99">Priority %7.99<br>
<input type="radio" name="shipping" value="Standard $3.99">Standard $3.99<br>
<input type="radio" name="shipping" value="Overnight $15.99">Overnight $15.99</div>
<div style="float: left;">Discounts:<br>
<input type="checkbox" name="discounts" value="AAA">AAA<br>
<input type="checkbox" name="discounts" value="AARP">AARP<br>
<input type="checkbox" name="discounts" value="Haltz Member">Hatz Member</div>
<button type="button">Join Now!</button>
</fieldset>
</form>
</body>
3 issues:
1: you were missing a " on your body tag
2: when you use float:left you take the element out of the flow of the dom. In this case it's better to use display:inline-block
3: add display:block, margin-left: your a tag
NOTE: your form looks pretty good.
a{
display:inline-block;
margin-left:10px;
}
<body style="background-color:#EEEEEE">
<form name="data" action="https://www.hats.com" method="POST">
<fieldset style="width: 750px;">
<legend>Ordering Information</legend>
First Name: <input type="text" name="first" placeholder="Joey" size="25">
      Last Name: <input type="text" name="last" placeholder="Shmoey" size="25">
<br> <br> Street Address: <input type="text" name="address" placeholder="1234 Sesame Street" size="30">
State: <input type="text" name="state" placeholder="PA" maxlength="2" size="2">
City: <input type="text" name="city" placeholder="York">
Zip:<input type="text" name="zip" placeholder="17402" maxlength="5" size="8">
</fieldset>
<fieldset style="width: 750px;">
<legend>Payment and Shipping</legend>
<div style="width: 250px; float: left;">Payment:<br>
<input type="radio" name="payment" value="Visa">Visa<br>
<input type="radio" name="payment" value="MasterCard">MasterCard<br>
<input type="radio" name="payment" value="Paypal">Paypal</div>
<div style="width: 250px; float: left;"> Shipping:<br>
<input type="radio" name="shipping" value="Priority $7.99">Priority %7.99<br>
<input type="radio" name="shipping" value="Standard $3.99">Standard $3.99<br>
<input type="radio" name="shipping" value="Overnight $15.99">Overnight $15.99</div>
<div style="display:inline-block;">Discounts:<br>
<input type="checkbox" name="discounts" value="AAA">AAA<br>
<input type="checkbox" name="discounts" value="AARP">AARP<br>
<input type="checkbox" name="discounts" value="Haltz Member">Hatz Member</div>
<button type="button">Join Now!</button>
</fieldset>
</form>
</body>
But a better way is to use flex:
#container{
display:flex;
justify-content:space-around;
align-items:bottom;
align-items:flex-end;
}
<body style="background-color:#EEEEEE">
<form name="data" action="https://www.hats.com" method="POST">
<fieldset style="width: 750px;">
<legend>Ordering Information</legend>
First Name: <input type="text" name="first" placeholder="Joey" size="25">
      Last Name: <input type="text" name="last" placeholder="Shmoey" size="25">
<br> <br> Street Address: <input type="text" name="address" placeholder="1234 Sesame Street" size="30">
State: <input type="text" name="state" placeholder="PA" maxlength="2" size="2">
City: <input type="text" name="city" placeholder="York">
Zip:<input type="text" name="zip" placeholder="17402" maxlength="5" size="8">
</fieldset>
<fieldset style="width: 750px;">
<legend>Payment and Shipping</legend>
<div id='container'>
<div >Payment:<br>
<input type="radio" name="payment" value="Visa">Visa<br>
<input type="radio" name="payment" value="MasterCard">MasterCard<br>
<input type="radio" name="payment" value="Paypal">Paypal</div>
<div > Shipping:<br>
<input type="radio" name="shipping" value="Priority $7.99">Priority %7.99<br>
<input type="radio" name="shipping" value="Standard $3.99">Standard $3.99<br>
<input type="radio" name="shipping" value="Overnight $15.99">Overnight $15.99</div>
<div> Discounts:<br>
<input type="checkbox" name="discounts" value="AAA">AAA<br>
<input type="checkbox" name="discounts" value="AARP">AARP<br>
<input type="checkbox" name="discounts" value="Haltz Member">Hatz Member</div>
<button type="button">Join Now!</button> </div>
</fieldset>
</form>
</body>
You should use label tags here for all your field label.
Each radio button list should also bee in it's own fieldset
Also don't use tags like br or elements like for spacing, user margin or padding instead.
Let's now encapsulate the radio buttons and the labels in a label tag. That will help up align the link. Don't use a button in a link, it's invalid HTML and can result in odd behavior. Instead, style the link appropriately, I'd keep it looking like a link as it is an actual link to an external site opening in a new tab.
body {
background-color: #EEEEEE
}
.flex {
display:flex;
}
label.wide {
padding-right: 1em;
}
label {
white-space:nowrap;
padding-bottom:0.5em;
display:inline-block;
}
fieldset fieldset {
border: none;
padding-bottom:0;
}
#paymentShippingFields fieldset label {
display: block;
padding-bottom:0;
}
#paymentShippingFields fieldset {
border:none;
width:33%;
}
<form name="data" action="https://www.hats.com" method="POST">
<fieldset style="width: 750px;">
<legend>Ordering Information</legend>
<fieldset id="nameField">
<label class="wide">First Name: <input type="text" name="first" placeholder="Joey" size="25"></label>
<label>Last Name: <input type="text" name="last" placeholder="Shmoey" size="25"></label>
</fieldset>
<fieldset id="addressField">
<label>Street Address: <input type="text" name="address" placeholder="1234 Sesame Street" size="30"></label>
<label>State: <input type="text" name="state" placeholder="PA" maxlength="2" size="2"></label>
<label>City: <input type="text" name="city" placeholder="York"></label>
<label>Zip: <input type="text" name="zip" placeholder="17402" maxlength="5" size="8"></label>
</fieldset>
</fieldset>
<fieldset style="width: 750px;" id="paymentShippingFields">
<legend>Payment and Shipping</legend>
<div class="flex">
<fieldset>
<legend>Payment:</legend>
<label><input type="radio" name="payment" value="Visa">Visa</label>
<label><input type="radio" name="payment" value="MasterCard">MasterCard</label>
<label><input type="radio" name="payment" value="Paypal">Paypal</label>
</fieldset>
<fieldset>
<legend>Shipping:</legend>
<label><input type="radio" name="shipping" value="Priority $7.99">Priority %7.99</label>
<label><input type="radio" name="shipping" value="Standard $3.99">Standard $3.99</label>
<label><input type="radio" name="shipping" value="Overnight $15.99">Overnight $15.99</label>
</fieldset>
<fieldset>
<legend>Discounts:</legend>
<label><input type="checkbox" name="discounts" value="AAA">AAA</label>
<label><input type="checkbox" name="discounts" value="AARP">AARP</label>
<label>
<input type="checkbox" name="discounts" value="Haltz Member">Hatz Member
Join Now!
</label>
</fieldset>
</div>
</fieldset>
</form>
I want this checkbox to go under the text boxes aligned.
I have tried this below code CSS code and it helped with aligning text boxes but not the checkboxes
label {
width:135px;
clear:left;
text-align:right;
padding-right:10px;
}
input, label {
float:left;
}
<label><input type="checkbox" name="Yes" data-field-
type="Boolean"> Will Attend</label>
<label for="Email Address">Email Address* </label><input type="text" name="Email Address" data-field-type="Text" required="required" data-validation-message="Required field.">
<label for="First Name">First Name</label> <input type="text" name="First Name" data-field-type="Text">
<label for="Last Name">Last Name</label> <input type="text" name="Last Name" data-field-type="Text">
I expect checkbox to be aligned with Textboxes.
Your input[type=checkbox] is inside the <label> take it out like the rest of the inputs and it works, as shown.
Although, for an option like yes/no, a radio button would be more suitable.
label {
width: 135px;
clear: left;
text-align: right;
padding-right: 10px;
}
input,
label {
float: left;
}
<label for="Email Address">Email Address* </label>
<input type="text" name="Email Address" data-field-type="Text" required="required" data-validation-message="Required field.">
<label>Will Attend</label>
<input type="checkbox" name="Yes" data-field-type="Boolean">
<label>Won't Attend</label>
<input type="checkbox" name="No" data-field-type="Boolean">
<label for="First Name">First Name</label>
<input type="text" name="First Name" data-field-type="Text">
<label for="Last Name">Last Name</label>
<input type="text" name="Last Name" data-field-type="Text">
You mean all label right? Just add blank <label> and clear: none;
label {
width: 135px;
clear: left;
text-align: right;
padding-right: 10px;
}
input,
label {
float: left;
}
.Checkbox{
clear:none;
}
<label for="Email Address">Email Address* </label><input type="text" name="Email Address" data-field-type="Text" required="required" data-validation-message="Required field.">
<label for="First Name">First Name</label> <input type="text" name="First Name" data-field-type="Text">
<label for="Last Name">Last Name</label> <input type="text" name="Last Name" data-field-type="Text">
<label> <!-- Just Blank--></label>
<label class="Checkbox"><input type="checkbox" name="Yes" data-field-
type="Boolean"> Will Attend</label>
I want to align the input fields shown below, with keep using <label> in a correct way.
The code is as below:
<form action="add.php" method="POST" enctype="multipart/form-data">
<label for="refNo">Field1 name (long)</label>
<input id="refNo" type="text" name="refNo" value="" /><br>
<label for="name">Field2 name</label>
<input id="name" type="text" name="name" value="" /><br>
<input type="submit" value=":: Add ::" name="addBtn" />
</form>
I am thinking to separate <label> tags in a <div> and the input fields in another and then with some floating manipulation I make the intended alignment, is this a correct way?
How about this
form > label {
min-width: 185px;
display: inline-block;
}
<form action="add.php" method="POST" enctype="multipart/form-data">
<label for="refNo">Field1 name (long)</label>
<input id="refNo" type="text" name="refNo" value="" /><br>
<label for="name">Field2 name</label>
<input id="name" type="text" name="name" value="" /><br>
<input type="submit" value=":: Add ::" name="addBtn" />
</form>
<style>
form{
width: 300px;
float: left;
}
form > label{
width: 50%;
float: left;
margin-bottom: 10px;
}
form > input{
float: right;
width: 50%;
margin-bottom: 10px;
}
</style>
<form action="add.php" method="POST" enctype="multipart/form-data">
<label for="refNo">Field1 name (long)</label>
<input id="refNo" type="text" name="refNo" value="" /><br>
<label for="name">Field2 name</label>
<input id="name" type="text" name="name" value="" /><br>
<input type="submit" value=":: Add ::" name="addBtn" />
</form>
For simple UI use this (Using Table):
<form action="add.php" method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="refNo">Field1 name (long)</label>
</td>
<td>
<input id="refNo" type="text" name="refNo" value="" />
</td>
</tr>
<tr>
<td>
<label for="name">Field2 name</label>
</td>
<td>
<input id="name" type="text" name="name" value="" /><br>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value=":: Add ::" name="addBtn" />
</td>
</tr>
</table>
</form>
For More good looking and advanced UI try using Bootstrap.
I would like to produce the following form style:
Name Email
[.................] [.................]
Subject
[.................]
Message
[.........................................]
[.........................................]
[.........................................]
[.........................................]
The HTML code I have is:
<form name="message" method="post">
<section>
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
<label for="email">Email</label>
<input id="email" type="text" value="" name="email">
</section>
<section>
<label for="subject">Subject</label>
<input id="subject" type="text" value="" name="subject">
<label for="message">Message</label>
<input id="message" type="text" value="" name="message">
</section>
</form>
At the moment it is producing:
Name [...................]
Email [...................]
Subject [...................]
Message
[.........................................]
[.........................................]
[.........................................]
[.........................................]
What would be the best way to do this? I keep getting in a muddle my floats!
I'd make both the input and label elements display: block , and then split the name label & input, and the email label & input into div's and float them next to each other.
input, label {
display:block;
}
<form name="message" method="post">
<section>
<div style="float:left;margin-right:20px;">
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
</div>
<div style="float:left;">
<label for="email">Email</label>
<input id="email" type="text" value="" name="email">
</div>
<br style="clear:both;" />
</section>
<section>
<label for="subject">Subject</label>
<input id="subject" type="text" value="" name="subject">
<label for="message">Message</label>
<input id="message" type="text" value="" name="message">
</section>
</form>
Probably a bit late but this worked for me.
i simply used column flex-direction on the label and input elements
HTML
<form id="survey-form">
<label for="name">Name</label>
<input type="text" id="name">
<label>Email</label>
<input type="email" id="email">
</form>
CSS
label,input{
display:flex;
flex-direction:column;
}
You could try something like
<form name="message" method="post">
<section>
<div>
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
</div>
<div>
<label for="email">Email</label>
<input id="email" type="text" value="" name="email">
</div>
</section>
<section>
<div>
<label for="subject">Subject</label>
<input id="subject" type="text" value="" name="subject">
</div>
<div class="full">
<label for="message">Message</label>
<input id="message" type="text" value="" name="message">
</div>
</section>
</form>
and then css it like
form { width: 400px; }
form section div { float: left; }
form section div.full { clear: both; }
form section div label { display: block; }
I know this is an old one with an accepted answer, and that answer works great.. IF you are not styling the background and floating the final inputs left. If you are, then the form background will not include the floated input fields.
To avoid this make the divs with the smaller input fields inline-block rather than float left.
This:
<div style="display:inline-block;margin-right:20px;">
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
</div>
Rather than:
<div style="float:left;margin-right:20px;">
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
</div>
I'd prefer not to use an HTML5 only element such as <section>. Also grouping the input fields might painful if you try to generate the form with code. It's always better to produce similar markup for each one and only change the class names. Therefore I would recommend a solution that looks like this :
CSS
label, input {
display: block;
}
ul.form {
width : 500px;
padding: 0px;
margin : 0px;
list-style-type: none;
}
ul.form li {
width : 500px;
}
ul.form li input {
width : 200px;
}
ul.form li textarea {
width : 450px;
height: 150px;
}
ul.form li.twoColumnPart {
float : left;
width : 250px;
}
HTML
<form name="message" method="post">
<ul class="form">
<li class="twoColumnPart">
<label for="name">Name</label>
<input id="name" type="text" value="" name="name">
</li>
<li class="twoColumnPart">
<label for="email">Email</label>
<input id="email" type="text" value="" name="email">
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" type="text" value="" name="subject">
</li>
<li>
<label for="message">Message</label>
<textarea id="message" type="text" name="message"></textarea>
</li>
</ul>
</form>
There is no need to add any extra div wrapper as others suggest.
The simplest way is to wrap your input element inside a related label tag and set input style to display:block.
Bonus point earned: now you don't need to set the labels for attribute. Because every label target the nested input.
<form name="message" method="post">
<section>
<label class="left">
Name
<input id="name" type="text" name="name">
</label>
<label class="right">
Email
<input id="email" type="text" name="email">
</label>
</section>
</form>
https://jsfiddle.net/Tomanek1/sguh5k17/15/
Using flex-direction: column; on the label elements will place the labels above their boxes, however it will also lock all the boxes in a long column. To get more than one box per line, with the label above the boxes you must pair them with divs. Here is an example of both:
#survey-form1 label {
display:flex;
flex-direction:column;
}
#survey-form2 {
display: flex;
flex-direction: row;
}
.inputPair {
display: flex;
flex-direction: column;
margin-right: 10px
}
<form id="survey-form1">
<label for="name1">Name</label>
<input type="text" id="name1">
<label for="email1">Email</label>
<input type="email" id="email">
</form>
<form id="survey-form2">
<div class="inputPair">
<label for="name2">Name2</label>
<input type="text" id="name2">
</div>
<div class="inputPair">
<label for="email2">Email2</label>
<input type="email" id="email2">
</div>
</form>
10 minutes ago i had the same problem of place label above input
then i got a small ugly resolution
<form>
<h4><label for="male">Male</label></h4>
<input type="radio" name="sex" id="male" value="male">
</form>
The disadvantage is that there is a big blank space between the label and input, of course you can adjust the css
Demo at:
http://jsfiddle.net/bqkawjs5/
OR....you can use flexbox with flex-direction: column on the imputs and they will arrange like bliss.