CSS Flexbox with Equal Sized Child Element - html

I'm doing one of the project for FreeCodeCamp's certificate and I'm having trouble getting the FlexBox's child elements to have equal size (height and width).
I have tried multiple use of Flex-Grow, Shrink, etc. But it seems like because of the length of the contents inside the element, they are not equally sized.
My Codepen: https://codepen.io/jyjang703/pen/PooeGxv
Basically, I want the third and 4th box of the FlexBox to be same size...
Any help would be appreciated!
<header>
<h1>Self-Happiness Survery</h1>
<h4>For anyone with regrets, griefs, and looking for changes...</h4>
</header>
<main>
<div>
<fieldset class="PersonInfo">
<form action="">
<label>First Name:</label>
<input type="text" name="First Name">
<br>
<br>
<label>Last Name:</label>
<input type="text" name="Last Name">
<br>
<br>
<label>Email:</label>
<input type="text" name="Email">
<br>
<br>
<label>Number:</label>
<input type="tel" name="Number" minlength="1" maxlength="10">
<br>
<br>
</form>
</fieldset>
<fieldset class="PersonInfo_2">
<form action="">
<label>Age:</label>
<input type="text" name="First Name">
<br>
<br>
<label>Sex:</label>
<select>
<option disabled selected value> -- select an option -- </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br>
<br>
<label>Occupation:</label>
<input type="text" name="Email">
<br>
<br>
<label>Current Martial Status:</label>
<input type="radio" name="sex" value="male"> Male
<input type="radio" name="sex" value="female"> Female
<br>
<br>
</form>
</fieldset>
<fieldset class="Checkbox_1">
<form action="">
<label>What is causing your inner pain?</label>
<br>
<br>
<input type="checkbox" name="Pain"> Breakup
<input type="checkbox" name="Pain"> Current Job
<input type="checkbox" name="Pain"> Friendship
<input type="checkbox" name="Pain"> Family Issue
<br>
<br>
<input type="checkbox" name="Pain"> Future
<input type="checkbox" name="Pain"> Self Confidence
<input type="checkbox" name="Pain"> Loneliness
<input type="checkbox" name="Pain"> Financial Stability
</form>
</fieldset>
<fieldset class="Checkbox_2">
<form action="">
<label>What is causing your inner pain?</label>
<br>
<br>
<input type="checkbox" name="Pain"> Breakup
<input type="checkbox" name="Pain"> Current Job
<input type="checkbox" name="Pain"> Friendship
<input type="checkbox" name="Pain"> Family Issue
<br>
<br>
<input type="checkbox" name="Pain"> Future
<input type="checkbox" name="Pain"> Self Confidence
<input type="checkbox" name="Pain"> Loneliness
<input type="checkbox" name="Pain"> Financial Stability
</form>
</fieldset>
</div>
<fieldset class="Additional_Information">
<h3>Please provide any additional information</h3>
<form action="">
<textarea rows="4" cols="50" placeholder="Additional Life Storeis Here...">
</textarea>
</form>
</fieldset>
</main>
Below is CSS:
body{
background-color: #f3e9fc;
}
header{
text-align: center;
font-size: 1.5rem;
}
div{
margin: auto;
display: flex;
justify-content: space-between;
}
label{
display: inline-block;
float: left;
width: 150px;
text-align: left; /*Change to right here if you want it close to the inputs*/
}
.Additional_Information{
text-align: center;
}

.PersonInfo,.Checkbox_1,.PersonInfo_2,.Checkbox_2
{
width:25%;
}
I added this at the end of your css in codepen which made it all the same size

Use flex-basis:0 and flex-grow:1 on the child element of flexbox.
Basis is like minimum size. Then flex-grow gives the size equally to all elements.

Isn't this what you want? 1st & 2nd fieldsets are the same size and 3rd & 4th are the same size.
fieldset {
flex-grow: 1;
}

If you want equal width for the child element, You need to set the width.
div {
width: 80%;
display: flex;
margin: 0 auto;
}
.PersonInfo,.Checkbox_1,.PersonInfo_2,.Checkbox_2 {
flex-basis: 25%;
}

Related

How to put button in line with the bottom of the "Discounts" div?

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">
&nbsp &nbsp &nbsp 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">
&nbsp &nbsp &nbsp 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">
&nbsp &nbsp &nbsp 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>

Can't get my form to properly submit using CGI script. Also when submitted to my database my css isn't formatting my html doc

On my end, the html works fine, everything is formatted how it is suppose to be. However, when I go to submit to the database, it is not formatted correctly. Also, we are using a CGI script in order to basically send the email to whatever email is in the recipient, but on my database it does not work. Any ideas on what could be causing problems? Here is all of my code:
body {
background-color: silver;
}
h1 {
text-align: center;
color: red;
}
legend {
color: blue;
}
fieldset {
color: brown;
}
p {
text-align: center;
color: maroon;
}
<h1> Music Survey </h1>
<p> This form is being created to see what type of music you like, while also getting information on the overall importance of music in your life </p>
<form method="post" action="https://www.website.com">
<input name="recipient" type="hidden" value="instructor#mail.com">
<input name="required" type="hidden" value="email">
<input name="return_link_url" type="hidden" value="http://student.mccinfo.net/~me/">
<input name="return_link_title" type="hidden" value="Home">
<input name="bgcolor" type="hidden" value="#EBAC9F">
<input name="title" type="hidden" value="Thank you for taking the time to complete the form.">
<input type="hidden" name="text_color" value="#000000">
<input name="font_face" type="hidden" value="Arial,Helvetica">
<label>First Name</label><br>
<input type="text" name="first_name"><br><br>
<label>Last Name</label><br>
<input type="text" name="last_name"><br><br>
<label>Email</label><br>
<input type="email" name="email"><br><br>
<fieldset>
<legend> Select Your Favorite Types of Music</legend>
<input type="checkbox" name="pop"> Pop <br>
<input type="checkbox" name="classical"> Classical <br>
<input type="checkbox" name="rock"> Rock <br>
<input type="checkbox" name="folk"> Folk <br>
<input type="checkbox" name="Rap"> Rap <br>
<input type="checkbox" name="other"> Other <br>
</fieldset><br>
<fieldset>
<legend>Select how often you purchase Music Cds:</legend>
<input type="radio" name="weekly"> Weekly <br>
<input type="radio" name="few"> A few CDs each year <br>
<input type="radio" name="monthly"> Monthly <br>
<input type="radio" name="never"> Never Purchase <br>
</fieldset>
<br>
</form>
<div id="heads">Select the method you use most often to purchase Music:</div>
<select>
<option label=" "> </option>
<option value="online">Online</option>
<option value="retail">Retail Store</option>
<option value="other">Other</option>
</select>
<br><br>
<div id="head">What role does music play in your life?</div>
<textarea name="textbox" cols="60"> </textarea><br>
<br>
<input type="submit">Submit</input>
<input type="reset">Reset</input>

Placing form elements on new lines without <br>

I'm trying to create a form to use for my work, I guess my question is more of a why does this happen.
<div class="checkbox">
<input type="radio" name="transport_method" >Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method" >Day Trip
</div>
my css class of "checkbox" looks like this
.checkbox {
float: left;
display: inline;
}
now my code at the next element
<div>First name:<br>
<input type="text" name="firstname"><br>
</div><br><br><br>
I have to add 3 <br>'s to get the "First name:" to be on a new line. I started with only 2 radio buttons and then I only needed 2 <br>'s. Is there a way to format my css to not need any <br>'s?
I think I need the <br>'s (correct me if I'm wrong) due to the fact that html file is reading the radio buttons as new lines and displaying them on one line, therefore the <br>'s fix that issue, but I don't like using them nor do I think it is semantically correct.
Let's start with a nicely marked up form
The form elements
The radio buttons can be wrapped in a <fieldset> element
The labels can all be marked up with <label> elements. The for attribute links to its input via the matching id attribute. One benefit of this is that users can click/touch on the label.
That gives us this:
<form>
<fieldset class="checkbox">
<input type="radio" name="transport_method" id="delivery">
<label for="delivery">Delivery</label>
<input type="radio" name="transport_method" id="pick-up">
<label for="pick-up">Store Pick-Up</label>
<input type="radio" name="transport_method" id="day-trip">
<label for="day-trip">Day Trip</label>
</fieldset>
<fieldset class="names">
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last name:</label>
<input type="text" name="lastname" id="lastname">
</fieldset>
</form>
Bring each text input onto a new line
The default display value for inputs is display: inline which brings them all onto one line. Use display: block on text inputs to knock them down:
input[type=text] {
display: block;
}
We want the radio buttons to remain on the one line, so they can be left at their default display: inline. More information on display.
Full example
Bring it all together with a little bit more CSS:
input[type=text] {
display: block;
margin: 5px 0;
}
input[type=radio] + label {
margin-right: 10px;
}
label,
input[type=radio] {
cursor: pointer;
}
fieldset {
border: none;
}
form {
background: #FFF9C4;
width: 500px;
font-family: sans-serif;
}
<form>
<fieldset class="checkbox">
<input type="radio" name="transport_method" id="delivery">
<label for="delivery">Delivery</label>
<input type="radio" name="transport_method" id="pick-up">
<label for="pick-up">Store Pick-Up</label>
<input type="radio" name="transport_method" id="day-trip">
<label for="day-trip">Day Trip</label>
</fieldset>
<fieldset class="names">
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last name:</label>
<input type="text" name="lastname" id="lastname">
</fieldset>
</form>
Try like this: Demo
<div class="checkbox">
<input type="radio" name="transport_method">Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method">Day Trip</div>
<div class="clear"></div>
<div>First name:
<input type="text" name="firstname">
</div>
.clear{clear:both} instead of <br/>
EDIT: If you dont want to create new class you can use like this too :
Updated dmo
.checkbox::after {
display:block;
clear:both;
content:"";
}
<div class="checkbox">
<input type="radio" name="transport_method" >Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method" >Day Trip
</div>
<div class="clear"></div>
<div>
First name:
<br>
<input type="text" name="firstname"><br>
</div>
<div class="clear"></div>
in css
.clear{
clear:both
}
It's as simple as this:
.checkbox{display:block}
And if you mean to have those checbox inputs floated to left, then use
.checkbox input{display:inline-block}
And there you go, no floats, no br tags, nothing weird
Using the new class amit made
use .clear{clear:both} instead of
on the following element, in my case
<div >First name:<br>
<input type="text" name="firstname"><br>
</div>
turned into
<div class="clear">First name:<br>
<input type="text" name="firstname"><br>
</div>

CSS Checkbox stacking

I'm trying to stack the checkboxes to the left side of the page. Any help would be much appreciated. Here's what I have so far:
<body>
<h1>Fitness Survey</h1>
<form class="pure-form" name="survey" method="post"action="mailto:email#youraddress.com" enctype="text/plain">
<fieldset>
<legend>Do you belong to a gym or fitness center?</legend>
<p>
<input type="radio" name="gym" value="yes">Yes
<input type="radio" name="gym" value="no">No
<br>
</p>
</fieldset>
<fieldset>
<legend>How do you stay in shape?</legend>
<p>
<input type="checkbox" name="exercises" value="classes">Fitness classes
<input type="checkbox" name="exercises" value="weights">Weights
<input type="checkbox" name="exercises" value="jogging">Jogging
<input type="checkbox" name="exercises" value="cardio machines">Cardiovascular machines
<input type="checkbox" name="exercises" value="swimming">Swimming
<input type="checkbox" name="exercises" value="team sports">Team sports
<input type="checkbox" name="exercises" value="other">Other
<br>
</p>
</fieldset>
<fieldset>
<legend>How often do you exercise?</legend>
<p>
<input type="radio" name="frequency" value="once per week">Once per week
<input type="radio" name="frequency" value="2-3 per week">2-3 times per week
<input type="radio" name="frequency" value="4-6 per week">4-6 times per week
<input type="radio" name="frequency" value="every day">Every day<br>
</p>
</fieldset>
<fieldset>
<legend><strong>Why do you exercise?</strong></legend>
<p>
<input type="checkbox" name="why" value="pleasure">I enjoy it
<input type="checkbox" name="why" value="fitness">I want to keep fit<br>
</p>
</fieldset>
<p>
<input type="submit"><input type="reset">
</p>
</form>
</body>
</html>
And the CSS is:
fieldset {
width: 400px;
border: 2px ridge #ff0000;
padding: 10px;
margin-bottom: 10px;
}
legend {
font-family: Georgia, "Times New Roman", serif;
font-weight: bold;
}
input {
font-family: Arial, sans-serif;
}
JSFiddle with the example
In order to improve semantics of your markup and solve your particular problem, I would suggest to:
Update markup for your inputs to have label element, like:
<fieldset>
<legend>Do you belong to a gym or fitness center?</legend>
<label><input type="radio" name="gym" value="yes" />Yes</label>
<label><input type="radio" name="gym" value="no" />No</label>
</fieldset>
Note absence of <br> and p elements. Those are not needed and are not adding any value to your code's semantics. Wrapping your inputs and text with label makes the text clickable (allowing to select a checkbox/radiobutton by clicking that text and not only the input itself) and associated with appropriate field.
In your CSS add the following:
.pure-form label {display: block; clear: left}
.pure-form input {float: left;}

Styling Form with Label above Inputs

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.