Aligning form input elements of varying lengths - html

I need to create a form using only HTML and CSS. The form fields are of different lengths depending on which field it is.
I've tried just about every solution I've found on SO and other sites that use things like display modes but those all seem to be aimed at forms where all the input fields are the same size. I've also tried to use a table to make the form but I don't know enough about them to make that work. I am still open to using a table if anyone is willing to teach me how to have it work.
form {
margin-top: 25px;
}
.form {
margin-left: 10px;
background-color: #fff;
float: left;
width: 620px;
}
.form p {
margin-left: 10px;
}
.form h3,
.summary h3 {
font-size: 36px;
background-color: #fff;
width: auto;
padding-top: 35px;
}
.form label {
font-family: Arial, sans-serif;
font-size: 14px;
display: block;
margin-left: 10px;
color: #8f8f8f;
}
input {
float: left;
margin-left: 10px;
background-color: #f9f9f9;
border: 1px solid #cdcdcd;
height: 36px;
border-radius: 2px;
}
.form span {
color: #861919;
}
.name {
width: 288px;
}
.bigbar {
width: 448px;
}
.smallbar {
width: 128px;
}
<form action="#" method="POST">
<label for="firstname">First Name <span>*</span></label>
<input type="text" id="firstname" required class="name" />
<label for="lastname">Last Name <span>*</span></label>
<input type="text" id="lastname" required class="name" />
<label for="address">Street Address <span>*</span></label>
<input type="text" id="address" required class="bigbar" />
<label for="apt">Apt/Unit/Suite #</label>
<input type="text" id="apt" class="smallbar" />
<label for="city">City <span>*</span></label>
<input type="text" id="city" required class="bigbar" />
<label for="province">Province <span>*</span></label>
<input type="text" id="province" required class="smallbar" />
<label for="code">Postal Code <span>*</span></label>
<input type="text" id="code" required class="smallbar" />
<label for="phone">Phone Number <span>*</span></label>
<input type="tel" id="phone" required class="bigbar" />
<button type="submit" id="submit">Continue Checkout</button>
</form>
At the end it's supposed to look like this: https://puu.sh/DQhyH/2aed3ce204.png
but as far as I'm able to get it with my knowledge it looks like this: https://puu.sh/DQhAs/eb0a1eeb5b.png (can't post images due to just making my account)
so it's almost there but the fields aren't aligned properly.

This could be done fairly easy with a table using colspan attribute on td elements. However, tables are intended for the display of data, and should be not used for layout (this article lists several reasons why).
You need to look at your desired result and form a grid with containing div elements. You have several "rows", so put the content of each row inside a div.row that takes the full width. Your content within the rows is either 1,2, or 3 "columns" wide out of 4 total. So you make div.col1, div.col2, div.col3 with appropriate widths and inline-block display. Then just make sure you put four columns in each row. The containing divs now determine the widths so we set inputs and labels to width:100% to take the full width of their respective parents.
div.field {
display: inline-block;
padding: .5em;
}
div.row,
input,
label,
button {
width: 100%;
}
form {
margin-top: 25px;
width: 620px;
}
form label {
font-family: Arial, sans-serif;
font-size: 14px;
display: block;
margin-left: 10px;
color: #8f8f8f;
}
input {
/*float: left;*/
margin-left: 10px;
background-color: #f9f9f9;
border: 1px solid #cdcdcd;
height: 36px;
border-radius: 2px;
}
form span {
color: #861919;
}
.col2 {
width: 288px;
}
.col3 {
width: 448px;
}
.col1 {
width: 128px;
}
<form action="#" method="POST">
<div class="row">
<div class="col2 field">
<label for="firstname">First Name <span>*</span></label>
<input type="text" id="firstname" required/>
</div>
<div class="col2 field">
<label for="lastname">Last Name <span>*</span></label>
<input type="text" id="lastname" required/>
</div>
</div>
<div class="row">
<div class="col3 field">
<label for="address">Street Address <span>*</span></label>
<input type="text" id="address" required class="bigbar" />
</div>
<div class="col1 field">
<label for="apt">Apt/Unit/Suite #</label>
<input type="text" id="apt" />
</div>
</div>
<div class="row">
<div class="col3 field">
<label for="city">City <span>*</span></label>
<input type="text" id="city" required/>
</div>
<div class="col1 field">
<label for="province">Province <span>*</span></label>
<input type="text" id="province" required/>
</div>
</div>
<div class="row">
<div class="col1 field">
<label for="code">Postal Code <span>*</span></label>
<input type="text" id="code" required/>
</div>
<div class="col3 field">
<label for="phone">Phone Number <span>*</span></label>
<input type="tel" id="phone" required/>
</div>
</div>
<div class="row">
<div class="col1 field"></div>
<div class="col2 field">
<button type="submit" id="submit">Continue Checkout</button>
</div>
<div class="col1 field"></div>
</div>
</form>

Related

How to align your form element with CSS?

So I have created a form with different type of elements (including radio, checkbox, text input etc.) and I cannot figure out how to make the whole form look neat. Ideally I want all labels aligned on the left side, except labels for radio buttons which should be aligned with the input fields.
My pen:
https://codepen.io/andreas-soteriou/pen/NWwEywR?editors=1100
I coloured the labels and inputs, for me to visualise as I am fairly new to this!
<header>
<h1 id="title">Print selection</h1>
<p id="description">Tailor your prints</p>
</header>
<body>
<main>
<form id="survey-form">
<h2 class="selection">start your selection here</h2>
<label id="label-input" for="name">Name:</label>
<input class="input" id="name" type="text" name="name" placeholder="First Last" required>
<br>
<label id="email-input" for="email">E-mail:</label>
<input id="email" type="text" name="email" placeholder="first.last#gmail.com" required>
<br>
<label id="number-input" for="number">Age:</label>
<input id="number" type="number" name="number" min="18" max="99" required>
<br>
<label for="dropdown">Select frame:</label>
<select id="dropdown" name="frames">
<option value="gold frame">Gold</option>
<option value="metallic frame">Metallic</option>
<option value="wooden frame">Wooden</option>
</select>
<br>
<!--RADIO--->
<label>Size of print:</label>
<br>
<input id="print_size" type="radio" name="print_size" value="10x10">
<label for="print_size">10x10 199:-</label>
<input id="print_size" type="radio" name="print_size" value="20x20">
<label for="print_size">20x20 299:-</label>
<input id="print_size" type="radio" name="print_size" value="40x40">
<label for="print_size">40x40 399:-</label>
<input id="print_size" type="radio" name="print_size" value="80x80">
<label for="print_size">80x80 599:-</label>
<!--CHECKBOX--->
<div>
<label>Additional features:</label>
<br>
<input id="feature1" type="checkbox" name="feature1" value="polished_glass">
<label for="feature1">Polished glass +100:-</label>
<br>
<input id="feature2" type="checkbox" name="feature2" value="3d_print">
<label for="feature2">3D-print +500:-</label>
</div>
<!--TEXTAREA--->
<div>
<label for="final_inputs">Additional input:</label>
<textarea id="final_inputs" name="final_inputs" rows="5" cols="20"></textarea>
</div>
<!--SUBMIT--->
<div>
<button type"submit" id="submit" value="submit">Submit order</button>
</div>
</form>
</main>
</body>
header, body {
color: black;
font-family: copperplate, sans-serif;
}
header {
text-align: center;
background-color: #C0C0C080;
margin: auto;
padding: 10px;
text-transform: uppercase;
}
body {
background-image: url("https://images.unsplash.com/photo-1504870712357-65ea720d6078?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1528&q=80");
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
#survey-form {
text-align: center;
color: dark-grey;
font-size: 12px;
padding: 10px;
width: 80%;
background-color: #C0C0C099;
margin: auto;
margin-top: 80px;
border-radius: 25px;
}
.selection {
margin-top: 1px;
margin-bottom: 15px;
}
label,input, select {
display: inline-block;
}
label {
width: 20%;
text-align: right;
background-color:red;
margin-right: 2px;
}
input {
width: 40%;
text-align: left;
background-color:blue;
margin-top: 2px;
}
header,
body {
color: black;
font-family: copperplate, sans-serif;
}
header {
text-align: center;
background-color: #C0C0C080;
margin: auto;
padding: 10px;
text-transform: uppercase;
}
body {
background-image: url("https://images.unsplash.com/photo-1504870712357-65ea720d6078?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1528&q=80");
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
#survey-form {
text-align: center;
color: dark-grey;
font-size: 12px;
padding: 10px;
width: 80%;
background-color: #C0C0C099;
margin: auto;
margin-top: 80px;
border-radius: 25px;
}
.selection {
margin-top: 1px;
margin-bottom: 15px;
}
label,
input,
select {
display: inline-block;
}
label {
width: 20%;
text-align: right;
background-color: red;
margin-right: 2px;
}
input {
width: 40%;
text-align: left;
background-color: blue;
margin-top: 2px;
}
<header>
<h1 id="title">Print selection</h1>
<p id="description">Tailor your prints</p>
</header>
<body>
<main>
<form id="survey-form">
<h2 class="selection">start your selection here</h2>
<label id="label-input" for="name">Name:</label>
<input class="input" id="name" type="text" name="name" placeholder="First Last" required>
<br>
<label id="email-input" for="email">E-mail:</label>
<input id="email" type="text" name="email" placeholder="first.last#gmail.com" required>
<br>
<label id="number-input" for="number">Age:</label>
<input id="number" type="number" name="number" min="18" max="99" required>
<br>
<label for="dropdown">Select frame:</label>
<select id="dropdown" name="frames">
<option value="gold frame">Gold</option>
<option value="metallic frame">Metallic</option>
<option value="wooden frame">Wooden</option>
</select>
<br>
<!--RADIO--->
<label>Size of print:</label>
<br>
<input id="print_size" type="radio" name="print_size" value="10x10">
<label for="print_size">10x10 199:-</label>
<input id="print_size" type="radio" name="print_size" value="20x20">
<label for="print_size">20x20 299:-</label>
<input id="print_size" type="radio" name="print_size" value="40x40">
<label for="print_size">40x40 399:-</label>
<input id="print_size" type="radio" name="print_size" value="80x80">
<label for="print_size">80x80 599:-</label>
<!--CHECKBOX--->
<div>
<label>Additional features:</label>
<br>
<input id="feature1" type="checkbox" name="feature1" value="polished_glass">
<label for="feature1">Polished glass +100:-</label>
<br>
<input id="feature2" type="checkbox" name="feature2" value="3d_print">
<label for="feature2">3D-print +500:-</label>
</div>
<!--TEXTAREA--->
<div>
<label for="final_inputs">Additional input:</label>
<textarea id="final_inputs" name="final_inputs" rows="5" cols="20"></textarea>
</div>
<!--SUBMIT--->
<div>
<button type "submit" id="submit" value="submit">Submit order</button>
</div>
</form>
</main>
</body>
Here's a simple starting point using CSS Grid
We are going to use two different grid layouts using the fieldset element and our grid container.
For simple Label | Input pairs the grid's first column will be the label taking 25% of the available space with the input occupying the rest.
For the radio button or check box groups, we will add a class to the fieldset then use three columns with 25% for the group label, auto for the input width, with the input label occupying the rest
fieldset {
border: none;
/*Set up base grid*/
display: grid;
/*Set Columns, first column is 25% the second takes up the rest*/
grid-template-columns: 25% 1fr;
row-gap: 0.5em;
}
/*Label styling*/
fieldset label {
text-align: right;
padding-right: 0.25em;
}
/*Additional set up for button group*/
fieldset.button-group {
/*For out button group rows we want the first col 25% ,
control minimum space, then next col take the rest*/
grid-template-columns: 25% auto 1fr;
}
/*Addditional stylings for the button/checkbox labels*/
fieldset.button-group label:not(:first-of-type) {
text-align: left;
}
/*Bump the buttons & check boxes to second column*/
fieldset.button-group input {
grid-column-start: 2;
}
<header>
<h1 id="title">Print selection</h1>
<p id="description">Tailor your prints</p>
</header>
<main>
<form id="survey-form">
<h2 class="selection">start your selection here</h2>
<fieldset>
<label id="label-input" for="name">Name:</label>
<input class="input" id="name" type="text" name="name" placeholder="First Last" required>
<label id="email-input" for="email">E-mail:</label>
<input id="email" type="text" name="email" placeholder="first.last#gmail.com" required>
<label id="number-input" for="number">Age:</label>
<input id="number" type="number" name="number" min="18" max="99" required>
<label for="dropdown">Select frame:</label>
<select id="dropdown" name="frames">
<option value="gold frame">Gold</option>
<option value="metallic frame">Metallic</option>
<option value="wooden frame">Wooden</option>
</select>
</fieldset>
<!--RADIO--->
<fieldset class="button-group">
<label>Size of print:</label>
<input id="print_size_199" type="radio" name="print_size" value="10x10">
<label for="print_size_199">10x10 199:-</label>
<input id="print_size_299" type="radio" name="print_size" value="20x20">
<label for="print_size_299">20x20 299:-</label>
<input id="print_size_399" type="radio" name="print_size" value="40x40">
<label for="print_size_399">40x40 399:-</label>
<input id="print_size_599" type="radio" name="print_size" value="80x80">
<label for="print_size_599">80x80 599:-</label>
</fieldset>
<!--CHECKBOX--->
<fieldset class="button-group">
<label>Additional features:</label>
<input id="feature1" type="checkbox" name="feature1" value="polished_glass">
<label for="feature1">Polished glass +100:-</label>
<input id="feature2" type="checkbox" name="feature2" value="3d_print">
<label for="feature2">3D-print +500:-</label>
</fieldset>
<!--TEXTAREA--->
<fieldset>
<label for="final_inputs">Additional input:</label>
<textarea id="final_inputs" name="final_inputs" rows="5" cols="20"></textarea>
</fieldset>
<!--SUBMIT--->
<div>
<button type "submit" id="submit" value="submit">Submit order</button>
</div>
</form>
</main>

Input field multiple want 1 input field go to the top right side how?

I got some problem i needed help i have created a page where there multiple font control however in my prototype design on right side that a field where input are in top and right position how can i do it
I have created a snippet to test it out
.StyleForm {
margin-top: 230px;
padding-left: 50px;
}
.StyleFormRest {
padding-left: 50px;
}
.StyleFormRestRight {
text-align: right;
padding-right: 500px;
margin-bottom: 5px;
align-items: right;
}
#EmailDisplay,
#AddressDisplay,
#DescriptionDisplay {
width: 875px;
}
.createMargin {
margin-bottom: 15px;
}
.Email,
.ContactName,
.ContactNo,
.Address,
.Description {
font-size: 18px;
}
#PriceModeDisplay {
text-align: right;
}
.StyleFormRestRight .form-control {
display: inline-block;
}
<div class="mb-3 StyleForm">
<label for="Email" class="form-label"><b class="Email">Email</b></label>
<input type="email" class="col-6 form-control" id="EmailDisplay" disabled readonly>
</div>
<form class="row g-3 StyleFormRest">
<div class="col-4">
<label for="Contact Name" class="visually-hidden"><b class="ContactName">Contact Name</b></label>
<input type="text" class="form-control" id="ContactNameDisplay" disabled readonly>
</div>
<div class="col-2 createMargin">
<label for="Contact No" class="visually-hidden"><b class="ContactNo">Contact No.</b></label>
<input type="text" class="form-control" id="ContactNoDisplay" disabled readonly>
</div>
</form>
<div class="mb-3 StyleFormRest">
<label for="Address" class="form-label"><b class="Address">Address</b></label>
<input type="text" class="col-6 form-control" id="AddressDisplay" disabled readonly>
</div>
<div class="mb-3 StyleFormRest">
<label for="Description" class="form-label"><b class="Description">Description</b></label>
<textarea class="form-control" id="DescriptionDisplay" rows="3" disabled readonly></textarea>
</div>
<div class="mb-3 StyleFormRestRight">
<label for="PriceMode" class="form-label"><b class="PriceMode">Price Mode</b></label>
<input type="text" class="col-2 form-control pull-right" id="PriceModeDisplay" disabled readonly>
</div>
My snippet is showing something off as this is how my html page look like currently
So as u can see my price mode i have settle to make it at the right side however is there a way for this to go top of the page as what show in the prototype design ?
Prototype Design
I hope I understood what you want right.
You can simply create two columns by wrapping everything on the left in a div and wrapping what is on the right in another div and wrap both divs with another div with display flex
<div class="container">
<div>...here goes the content on the left...</div>
<div>...here goes the content on the right...</div>
</div>
.container{
display:flex;/*This is the new style*/
}
.StyleForm {
margin-top: 230px;
padding-left: 50px;
}
.StyleFormRest {
padding-left: 50px;
}
.StyleFormRestRight {
text-align: right;
padding-right: 500px;
margin-bottom: 5px;
align-items: right;
}
#EmailDisplay,
#AddressDisplay,
#DescriptionDisplay {
width: 875px;
}
.createMargin {
margin-bottom: 15px;
}
.Email,
.ContactName,
.ContactNo,
.Address,
.Description {
font-size: 18px;
}
#PriceModeDisplay {
text-align: right;
}
.StyleFormRestRight .form-control {
display: inline-block;
}
<div class="container"><!-- This is the container -->
<div><!-- This contains whats on the right -->
<div class="mb-3 StyleForm">
<label for="Email" class="form-label"><b class="Email">Email</b></label>
<input type="email" class="col-6 form-control" id="EmailDisplay" disabled readonly>
</div>
<form class="row g-3 StyleFormRest">
<div class="col-4">
<label for="Contact Name" class="visually-hidden"><b class="ContactName">Contact Name</b></label>
<input type="text" class="form-control" id="ContactNameDisplay" disabled readonly>
</div>
<div class="col-2 createMargin">
<label for="Contact No" class="visually-hidden"><b class="ContactNo">Contact No.</b></label>
<input type="text" class="form-control" id="ContactNoDisplay" disabled readonly>
</div>
</form>
<div class="mb-3 StyleFormRest">
<label for="Address" class="form-label"><b class="Address">Address</b></label>
<input type="text" class="col-6 form-control" id="AddressDisplay" disabled readonly>
</div>
<div class="mb-3 StyleFormRest">
<label for="Description" class="form-label"><b class="Description">Description</b></label>
<textarea class="form-control" id="DescriptionDisplay" rows="3" disabled readonly></textarea>
</div>
</div>
<div><!-- This contains whats on the left -->
<div class="mb-3 StyleFormRestRight">
<label for="PriceMode" class="form-label"><b class="PriceMode">Price Mode</b></label>
<input type="text" class="col-2 form-control pull-right" id="PriceModeDisplay" disabled readonly>
</div>
</div>
</div>

Make name appear on the same line nicely

I am trying to place the firstname and the lastname on the same line.
I looked over this question:
html form - make inputs appear on the same line
But because of my old css (used in app), the fields aren't correctly displayed. I need a little space between the two fields.
I made a fiddle:
https://jsfiddle.net/up5bL2xn/
EDIT: My html code:
<div class="parent-split-row">
<div class="child-split-row">
<label class="form-label">
First Name
</label>
<input type="text"
id="js-input-first-name"
name="first_name"
class="form-input"
value="{{ form.first_name.vars.value|default }}"
placeholder="First Name"
onkeyup="VALIDATIONS.onKeyUp('js-coach-license-registration-form','js-input-first-name')"
onmouseup="VALIDATIONS.onMouseUp('js-coach-license-registration-form','js-input-first-name')"
autofocus>
<div id="js-input-first-name__error"
class="form-validation">
Please fill in your First Name
</div>
</div>
<div class="child-split-row">
<label class="form-label">
Last Name
</label>
<input type="text"
id="js-input-last-name"
name="last_name"
class="form-input"
value="{{ form.last_name.vars.value|default }}"
placeholder="Last Name"
onkeyup="VALIDATIONS.onKeyUp('js-coach-license-registration-form','js-input-last-name')"
onmouseup="VALIDATIONS.onMouseUp('js-coach-license-registration-form','js-input-last-name')"
autofocus>
<div id="js-input-last-name__error"
class="form-validation">
Please fill in your Last Name
</div>
</div>
</div>
Your help would be appreciated.
Thank you.
Here you go! just added a padding-right on the first name and divided the label and input in two different div's :)
.name{
float: left;
padding-right: 4%;
}
<div class="name">
<label for="First_Name">First Name:</label>
<input name="first_name" id="First_Name" type="text" />
</div>
<div class="lastname">
<label for="Name">Last Name:</label>
<input name="last_name" id="Last_Name" type="text" />
</div>
try this. ;)
.parent-split-row { display: flex; }
use "&nbsp" after your first div class="child-split-row" or else add padding right to your css for child-split-row class
.child-split-row {
padding-right:10px;
}
.parent-split-row {
display: table;
}
.child-split-row {
display: table-cell;
}
.form-label {
display: block;
width: 100%;
color: #1f1f1f;
margin-top: 7px;
margin-bottom: 10px;
padding-left: 2px;
}
.form-input {
font-family: Aileron;
font-weight: 500;
display: block;
width: 100%;
padding: 8px 10px;
color: #1f1f1f;
background-color: #d6d5d6;
border: 1px solid #d6d5d6;
border-radius: 3px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
overflow: hidden;
height: 34px;
}
<div class="parent-split-row">
<div class="child-split-row">
<label class="form-label">
First Name
</label>
<input type="text"
id="js-input-first-name"
name="first_name"
class="form-input"
placeholder="First Name">
</div>&nbsp&nbsp
<div class="child-split-row">
<label class="form-label">
Last Name
</label>
<input type="text"
id="js-input-last-name"
name="last_name"
class="form-input"
placeholder="Last Name">
</div>
</div>
Add this to your CSS file:
.child-split-row:not(:last-child) {
padding-right: 10px;
}
That's one way of doing it.

Blocks besides each others

I have a given HTML which I can't touch:
<fieldset>
<label for="username" class="text">
Dein Benutzername<span class="req">*</span>
</label>
<div class="div_text">
<input name="username" type="text" id="username" value="" class="textbox" required="">
</div>
<label for="first_name" class="text">
Vorname<span class="req">*</span>
</label>
<div class="div_text">
<input name="first_name" type="text" id="first_name" value="" class="textbox" required="">
</div>
<label ....
and I need to output that one with 2 same-width columns where the label is above the corresponding div-block and the next input-div-block and their corresponding label is besides it.
I added an image to show exactly what I want it to be. I run into problems because the label-tag sits within the same hierarchy-level as the div-block and I therefore can't use inline-block nor - due to my lack of knwoledge - flexbox as it's a mix between column and row in flex-direction
Could somebody please guide me on that one?
You can still use flexbox and put the elements into a new order. It is not a elegant way but the only solution i can
think of if you cant touch the HTML
.text,
.div_text {
width: 50%;
display: block;
}
fieldset {
display: flex;
flex-wrap: wrap;
}
label {
display: block;
}
label[for=username] {
order: 1;;
}
label[for=first_name] {
order: 2;
}
fieldset div:nth-of-type(1){
order: 3;
}
fieldset div:nth-of-type(2){
order: 4;
}
label[for=sur_name] {
order: 5;
}
label[for=example] {
order: 6;
}
fieldset div:nth-of-type(3){
order: 7;
}
fieldset div:nth-of-type(4){
order: 8;
}
<fieldset>
<label for="username" class="text">Dein Benutzername<span class="req">*</span></label>
<div class="div_text">
<input name="username" type="text" id="username" value="" class="textbox" required="">
</div>
<label for="first_name" class="text">Vorname<span class="req">*</span></label>
<div class="div_text">
<input name="first_name" type="text" id="first_name" value="" class="textbox" required="">
</div>
<label for="sur_name" class="text">Nachname<span class="req">*</span></label>
<div class="div_text">
<input name="sur_name" type="text" id="sur_name" value="" class="textbox" required="">
</div>
<label for="example" class="text">Weiters Beispiel<span class="req">*</span></label>
<div class="div_text">
<input name="example" type="text" id="example" value="" class="textbox" required="">
</div>
</fieldset>
Hoping you are not using any css framework, please have a look at the below-working snippet with custom markup and css, hope it helps :)
body {
font-size: 14px;
font-family: sans-serif;
}
fieldset {
clear: both;
max-width: 500px;
border: 1px dashed deeppink;
}
legend {
color: #fff;
font-size: 14px;
padding: 5px 10px;
background: deeppink;
margin: 0 0 10px 15px;
}
.form-group {
width: 45%;
float: left;
padding: 0 2.5%;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"] {
width: 100%;
font-size: 14px;
padding: 5px 3px;
margin-bottom: 10px;
border: 1px solid #000;
box-sizing: border-box;
}
<fieldset>
<legend>Fieldset Container</legend>
<div class="form-group">
<label for="labelX">Label x</label>
<input id="labelX" type="text" placeholder="Label X" />
</div>
<div class="form-group">
<label for="labelY">Label y</label>
<input id="labelY" type="text" placeholder="Label Y" />
</div>
<div class="form-group">
<label for="labelZ">Label z</label>
<input id="labelZ" type="text" placeholder="Label Z" />
</div>
<div class="form-group">
<label for="labelXX">Label xx</label>
<input id="labelXX" type="text" placeholder="Label XX" />
</div>
</fieldset>

How do I place two text fields side by side?

I am creating a form for a landing page, so I want to keep the text input field side by side so I don't make the form very long.I trying to do something like this :
But it seems really difficult right now, this is what my form current looks like:
.teaser-right {
float: right;
width: 45%;
margin: 3% 0 0 0
}
#calltoaction-form {
background: #f2f2f2;
padding-bottom: 10px;
width: 800px;
position: right;
bottom: 0
}
<div id="calltoaction-form" class="teaser-form">
<div class="form-title">
<h3>sample form</h3>
</div>
<form id="contact_form" action="_contact-us.php" method="post">
<div class="form-header">
<h4>Personal details</h4>
</div>
<div class="form-section">
<input id="name" name="name" type="text" placeholder="Your name">
</div>
<div class="form-section">
<input id="email" name="email" type="text" placeholder="Your email">
</div>
Add this to your CSS:
.form-section{
display:inline-block;
}
To put 2 input fields beside each other you can use of float. if you float the elements you need to make sure you use the clearfix hack on the parent div. Otherwise the parent div won't have a height.
Another option is to use display: inline-block If you use display inline-block you need to reset the font-size on the parent to 0. The elements that where you use display: inline-block need to get the font-size: 12px; for example. With display inline-block you can get a slight height difference.
Example:
float: left;
https://jsfiddle.net/ymma61hu/3/
Clearfix hack:
https://css-tricks.com/snippets/css/clear-fix/
Example 2:
display: inline-block;
https://jsfiddle.net/vh49gtzL/
Hi just put input elements inside same div see fiddle. https://jsfiddle.net/v5omym1a/
<div id="calltoaction-form" class="teaser-form">
<div class="form-title">
<h3>sample form</h3>
</div>
<form id="contact_form" action="_contact-us.php" method="post">
<div class="form-header">
<h4>Personal details</h4>
</div>
<div class="form-section">
<input id="name" name="name" type="text" placeholder="Your name">
<input id="email" name="email" type="text" placeholder="Your email">
</div>
I cleaned up your markup a bit. You don't need to wrap your <h3> and <h4> in div-s - those are block level elements as well and can be perfectly styled.
#calltoaction-form {
background: #f2f2f2;
padding-bottom: 10px;
width: 800px;
position: right;
bottom: 0;
overflow: hidden;
}
.form-section {
float: left;
width: 50%;
}
label {
display: block;
margin-bottom: .25em;
}
<div id="calltoaction-form" class="teaser-form">
<h3 class="form-title">sample form</h3>
<form id="contact_form" action="_contact-us.php" method="post">
<h4 class="form-header">Personal details</h4>
<div class="form-section">
<label for="name">Name</label>
<input id="name" name="name" type="text" placeholder="Your name">
</div>
<div class="form-section">
<label for="email">Email</label>
<input id="email" name="email" type="text" placeholder="Your email">
</div>
</form>
</div>
.form-section {
float: left;
margin: 1%;
width: 48.5%;
}
.form-section input {
width: 100%;
}
.form-section:first-child {
margin-left: 0;
}
.form-section:last-child {
margin-right: 0;
}
#calltoaction-form {
background: #f2f2f2;
padding-bottom: 10px;
width: 100%;
position: right;
bottom: 0
}
<div id="calltoaction-form" class="teaser-form">
<div class="form-title">
<h3>sample form</h3>
</div>
<form id="contact_form" action="_contact-us.php" method="post">
<div class="form-header">
<h4>Personal details</h4>
</div>
<div class="form-container">
<div class="form-section">
<input id="name" name="name" type="text" placeholder="Your name">
</div>
<div class="form-section">
<input id="email" name="email" type="text" placeholder="Your email">
</div>
</div>