Hi,
How do I align the input field such that they start on the same vertical line? Is there a CSS method I can use instead of manually editing each input field?
This is the html code:
<form id="survey-form">
<div id="form-group">
<label for="name" id="name-label"> Name: </label>
<input type="text" id="name" name="name" placeholder="full name" required></input><br>
</div>
<div id="form-group">
<label for="email" id="email-label"> Email: </label>
<input type="email" id="email" name="email" placeholder="validated email" required></input><br>
</div>
<div id="form-group">
<label for="number" id="number-label"> Contact: </label>
<input type="number" id="number" name="contact" min="00000000" max="99999999" placeholder="phone number"></input><br>
</div>
</form>
For CSS, I currently only have:
input {
margin-bottom: 15px;
width: 10%;
}
I was wondering how I can code it in CSS such that the text input fields align? For example, if I use padding the text fields will still not aligned as the length of the label differs.
Firstly, you don't need to close your input as </input>.
Also, in the last input, you forgot your <div id="form-group">.
Now, the idea to make all the input align is simply to make all your labels as an inline-block element with a specific width. That way, it will push out all the input and align them after the width ends.
input {
margin-bottom: 15px;
width: 10%;
}
#form-group label {
display: inline-block;
width: 60px;
}
<form id="survey-form">
<div id="form-group">
<label for="name" id="name-label"> Name: </label>
<input type="text" id="name" name="name" placeholder="full name" required><br>
</div>
<div id="form-group">
<label for="email" id="email-label"> Email: </label>
<input type="email" id="email" name="email" placeholder="validated email" required><br>
</div>
<div id="form-group">
<label for="number" id="number-label"> Contact: </label>
<input type="number" id="number" name="contact" min="00000000" max="99999999" placeholder="phone number">
</div>
</form>
Use flex property to align the label and input box
#form-group{
display:flex;
align-items:center;
margin-bottom:20px;
}
#form-group label{
flex:1;
flex-basis:20%;
max-width:20%;
}
#form-group input {
flex:2;
flex-basis:20%;
max-width:20%;
}
Working Demo
#form-group{
display:flex;
align-items:center;
margin-bottom:20px;
}
#form-group label{
flex:1;
flex-basis:20%;
max-width:20%;
}
#form-group input {
flex:2;
flex-basis:20%;
max-width:20%;
}
<form id="survey-form">
<div id="form-group">
<label for="name" id="name-label"> Name: </label>
<input type="text" id="name" name="name" placeholder="full name" required>
</div>
<div id="form-group">
<label for="email" id="email-label"> Email: </label>
<input type="email" id="email" name="email" placeholder="validated email" required>
</div>
<div id="form-group">
<label for="number" id="number-label"> Contact: </label>
<input type="number" id="number" name="contact" min="00000000" max="99999999" placeholder="phone number">
</div>
</form>
Related
I am attempting to align the two cells for the address input. The one that is inline I can shift, but the ones that are not I do know know how to shift right. If I could get some creative feed back I would appreciate it.
.EMBody {
position: relative;
background-color: navajowhite;
}
.EMSpace {
display: inline-block;
width: 100px;
}
.EMAdj {
display: inline-block;
margin-right: 20%;
}
input[type="text"] {
width: 60%;
}
<section class="EMBody">
<div>
<label class="EMSpace">Full Name:</label>
<input type="text" placeholder="Enter your name">
</div>
<div>
<label class="EMSpace">Address:</label>
<input type="text" placeholder="Street address">
<input type="text" class="EMAdj" placeholder="City">
<input type="text" class="EMAdj" placeholder="Zip">
</div>
<div>
<label class="EMSpace">Phone Number:</label>
<input type="text" placeholder="9999999999">
</div>
<div>
<label class="EMSpace">Email:</label>
<input type="text" placeholder="email#email.com">
</div>
<div>
<label class="EMSpace">Brief Message: </label>
<textarea cols="25" rows="5" class="message" placeholder="Please give a brief discription of your Iguana issues. Green or Spiny Tailed? In the attics, flowerbed, canal?"></textarea>
</div>
</section>
use nth-child :
.EMBody {
position: relative;
background-color: navajowhite;
}
.EMSpace {
display: inline-block;
width: 100px;
}
.EMAdj {
display: inline-block;
margin-right: 20%;
}
input[type="text"] {
width: 60%;
}
input:nth-child(3),input:nth-child(4)
{
margin-left:104px;
}
<section class="EMBody">
<div>
<label class="EMSpace">Full Name:</label>
<input type="text" placeholder="Enter your name">
</div>
<div>
<label class="EMSpace">Address:</label>
<input type="text" placeholder="Street address">
<input type="text" class="EMAdj" placeholder="City">
<input type="text" class="EMAdj" placeholder="Zip">
</div>
<div>
<label class="EMSpace">Phone Number:</label>
<input type="text" placeholder="9999999999">
</div>
<div>
<label class="EMSpace">Email:</label>
<input type="text" placeholder="email#email.com">
</div>
<div>
<label class="EMSpace">Brief Message: </label>
<textarea cols="25" rows="5" class="message" placeholder="Please give a brief discription of your Iguana issues. Green or Spiny Tailed? In the attics, flowerbed, canal?"></textarea>
</div>
</section>
The easiest fully responsive solution would be to use CSS-Grid. THis would allow you to cut the code down a lot.
simply use a 2 column layout and let the label for the address span 3 rows.
.EMBody {
display: grid;
grid-template-columns: min-content auto;
column-gap: 10px;
}
.EMBody {
white-space: nowrap;
}
.EMBody label:nth-of-type(2) {
grid-row: span 3;
}
<section class="EMBody">
<label class="EMSpace">Full Name:</label>
<input type="text" placeholder="Enter your name">
<label class="EMSpace">Address:</label>
<input type="text" placeholder="Street address">
<input type="text" class="EMAdj" placeholder="City">
<input type="text" class="EMAdj" placeholder="Zip">
<label class="EMSpace">Phone Number:</label>
<input type="text" placeholder="9999999999">
<label class="EMSpace">Email:</label>
<input type="text" placeholder="email#email.com">
<label class="EMSpace">Brief Message: </label>
<textarea cols="25" rows="5" class="message" placeholder="Please give a brief discription of your Iguana issues. Green or Spiny Tailed? In the attics, flowerbed, canal?"></textarea>
</section>
<h4>Full name</h4>
<input type="text" name="fname" id="fn" />
<input type="text" name="lname" id="ln" /><br />
<label for="fname">First Name</label>
<label for="lname">Last Name</label>
In the above code the label for first name and last name appears side by side, how to place the label for last name just below the input field of last name, without using pre tag
You can achieve this using simple Flexbox which is kind of professional work and if you did not know much about Flexbox. Here is a simple tutorial on CSS Flexbox
.maindiv{
display:flex;
flex-direction:row;
align-items:center
}
.inputdiv{
display:flex;
flex-direction:column;
}
<h4>Full name</h4>
<div class="maindiv">
<div class='inputdiv'>
<input type = "text" name="fname" id = "fn" />
<label for="fname">First Name</label>
</div>
<div class='inputdiv'>
<input type = "text" name="lname" id = "ln" />
<label for="lname">Last Name</label>
</div>
</div>
Using flexbox can clean make it easier to do layouts.
.groupings {
display: flex;
}
.groupings label,
.groupings label {
display: block;
}
<fieldset>
<legend>Full name</legend>
<div class="groupings">
<div>
<input type="text" name="fname" id="fn" />
<label for="fname">First Name</label>
</div>
<div>
<input type="text" name="lname" id="ln" />
<label for="lname">Last Name</label>
</div>
</div>
</fieldset>
input {
display: inline-block;
width: 6em;
position: relative;
top: -3em;
}
label {
display: inline-block;
width: 6em;
margin-right: .5em;
padding-top: 1.5em;
}
<label>First Name <input type="text" name="fname" id="fn" /></label>
<label>Last Name <input type="text" name="lname" id="ln" /></label>
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 am creating a form and I would like all of the text boxes to line up along with having the submit button centered. These were the directions I was given:
create a label element selector to float to the left with block
display set text alignment to right assign width of 8em set an
appropriate amount of padding configure and input element and
textarea element selectors with block display of 12em bottom margin
Here is the code I have so far:
form{
float:left;
display:block;
text-align:right;
width:8em;
}
input {
display: block;
margin-bottom:1em;
}
textarea#feedback {
display: block;
margin-bottom:1em;
}
.form{
margin-left: 12em;
}
<form role="form">
<div class="form">
<form>
<label for="Name">Name*</label>
<input type="form" name="Name" id="" value="" required><br>
<label for="Email">Email*</label>
<input type="email" name="email" id= value"" required> <br>
<label for="Experience">Experience*</label>
<textarea rows="2" cols="20">
</textarea><br>
<input type="submit" value="Apply Now">
</div>
</form>
I have no idea what I am doing wrong with my CSS. I tried changing the padding and the margins but nothing worked. What am I missing from my code? Any feedback would be greatly appreciated.
Here's the working fiddle: https://jsfiddle.net/qptgsc1e/
Just some minor changes, Replace this CSS and HTML and you're good to go.
If you want to see the code changes Check it here
form{
float:left;
display:block;
width:50%;
}
input {
display: block;
margin-bottom:1em;
width: 50%;
}
input[type='submit']{
width:30%;
margin:0 auto;
}
textarea#feedback {
display: block;
margin-bottom:1em;
width:50%;
}
.submit-wrap{
width:50%;
}
<form role="form">
<div class="form">
<form>
<label for="Name">Name*</label>
<input type="form" name="Name" id="" value="" required><br>
<label for="Email">Email*</label>
<input type="email" name="email" id= value"" required> <br>
<label for="Experience">Experience*</label><br>
<textarea rows="2" id="feedback">
</textarea><br>
<div class="submit-wrap"><input type="submit" value="Apply Now"></div>
</div>
</form>
First lets start with fixing your HTML.
This line is formatted wrong.
<input type="email" name="email" id= value"" required> <br>
Needs to be
<input type="email" name="email" id="" value="" required> <br>
To be.
<form role="form">
<div class="form">
<form>
<label for="Name">Name*</label><br>
<input type="form" name="Name" id="" value="" required>
<label for="Email">Email*</label><br>
<input type="email" name="email" id="" value="" required>
<label for="Experience">Experience*</label><br>
<textarea rows="2" cols="20"></textarea><br>
<input type="submit" value="Apply Now">
</div>
</form>
Now that we have that fixed as well as some spacing we can start on centering the submit button.
Now we will wrap the submit button with a div
<div class="buttonHolder">
<input type="submit" value="Apply Now">
</div>
Then all that is left is to style the div, you can do this which ever way you find best margin, text-align, etc.
.buttonHolder{ text-align: center; }
Final HTML:
<form role="form">
<div class="form">
<form>
<label for="Name">Name*</label><br>
<input type="form" name="Name" id="" value="" required>
<label for="Email">Email*</label><br>
<input type="email" name="email" id="" value="" required>
<label for="Experience">Experience*</label><br>
<textarea rows="2" cols="20"></textarea><br>
<div class="buttonHolder">
<input type="submit" value="Apply Now">
</div>
</div>
</form>
Check if this style works for you.
form{
float:left;
display:block;
/* text-align:right; */
width:8em;
margin-left: 12em;
min-width: 180px;
}
input, textarea{
min-width: 180px;
}
label{
display: block;
}
input {
display: block;
margin-bottom:1em;
}
input[type="submit"]{
display: block;
margin: auto;
width: auto;
min-width: 50px;
}
textarea#feedback {
display: block;
margin-bottom:1em;
}
<form role="form">
<div class="form">
<form>
<label for="Name">Name*</label>
<input type="form" name="Name" id="" value="" required><br>
<label for="Email">Email*</label>
<input type="email" name="email" id= value"" required> <br>
<label for="Experience">Experience*</label>
<textarea rows="2" cols="20">
</textarea><br>
<input type="submit" value="Apply Now">
</div>
</form>
please include style in the submit button.
<input type="submit" value="Apply Now" style="margin:0 auto;">
refer : center form submit buttons html / css
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.