Trying to align input field with labels - html

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.

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>

Why doesn't this div move using css?

I have a form inside a div. I want to move the div to the right, and I can do that if I use an inline style like this:
<div class="joinform-page" style="margin-left: 30%;>
I want to move it using margin-left: 30% in the css, not as an inline style because inline styles make media queries more difficult. But it ignores any margin changes I make in the css.
Here's the full html:
<div class="joinform-page">
<div class="form">
<form action="data_in.php" method="post" name='data_in' id='data_in'>
<input type="text" placeholder="Email" name="email_field" maxlength="60">
<input type="text" placeholder="First Name (optional)" name="firstname" maxlength="50">
<input type="text" placeholder="Last Name (optional)" name="lastname" maxlength="50">
<div><input type="hidden" id="password" name="password" value="pwdtemp"></div>
<div><input type="hidden" id="comments" name="comments" value="none"></div>
<button class="btn_class" style="color:rgb(255,255,255); background-color:rgb(25,25,25); text-align:center;" id="btn_submit" onclick="GetDate();">Submit Form</button><br><br><br>
<div style="padding-left:0%;">
<label class="container">
<span class="betajoinpage_cbx">Add me to your list</span>
<input type="hidden" name="custom_checkbox" value="No">
<input type="checkbox" id="ckbx" name="custom_checkbox" checked="checked" value="Yes"><span class="checkmark" style="color:blue;"></span>
</label></div><br>
</form>
</div>
</div>
Here's the relevant css class:
.joinform-page {
width: 80%;
padding: 0% 0 0;
margin-top: -2.5%;
margin-left: 30%; }
Why doesn't this div move when I use margin-left in the css,. not as an inline style.
Thanks for any help.
Actually It was working with the same piece of code.
If it still doesn't work, there might be styling for parent element or another styling for same element.
The CSS you have above works as you would expect. Please ensure your CSS is correctly imported like so:
<!-- Where FILE_NAME is the name of your .CSS file -->
<link rel="stylesheet" type="text/css" href="FILE_NAME.css">
.joinform-page {
width: 80%;
padding: 0% 0 0;
/*margin-top: -2.5%;*/
margin-left: 30%;
}
<div class="joinform-page">
<div class="form">
<form action="data_in.php" method="post" name='data_in' id='data_in'>
<input type="text" placeholder="Email" name="email_field" maxlength="60">
<input type="text" placeholder="First Name (optional)" name="firstname" maxlength="50">
<input type="text" placeholder="Last Name (optional)" name="lastname" maxlength="50">
<div><input type="hidden" id="password" name="password" value="pwdtemp"></div>
<div><input type="hidden" id="comments" name="comments" value="none"></div>
<button class="btn_class" style="color:rgb(255,255,255); background-color:rgb(25,25,25); text-align:center;" id="btn_submit" onclick="GetDate();">Submit Form</button><br><br><br>
<div style="padding-left:0%;">
<label class="container">
<span class="betajoinpage_cbx">Add me to your list</span>
<input type="hidden" name="custom_checkbox" value="No">
<input type="checkbox" id="ckbx" name="custom_checkbox" checked="checked" value="Yes"><span class="checkmark" style="color:blue;"></span>
</label></div><br>
</form>
</div>
</div>

How to align radio buttons with labels in one line? [duplicate]

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>

Put the one box down exactly to the other

In html I have this form:
<form action="index.php" method="GET">
Name <input type="text" name="FirstName" size="10" /><br />
<br />
Surname: <input type="text" name="LastName" size="10" /><br />
<br />
</form>
It is a for with the first and last name. Is it possible to make the one box to be exactly down to the other. Because the first box is closer to name because it has less letters than the surname and respectively it takes the space?
Without using CSS simplest way to do this, is using Table of HTML.
Consider this Snippet:
<form action="index.php" method="GET">
<table>
<tr>
<td>Name </td>
<td>
<input type="text" name="FirstName" size="10" />
<br />
</td>
</tr>
<tr>
<td>Surname:</td>
<td>
<input type="text" name="LastName" size="10" />
</td>
</tr>
</table>
</form>
here's one way you could do it.
input{position:absolute;left:80px;}
<form action="index.php" method="GET">
<label>Name</label> <input type="text" name="FirstName" size="10" />
<br><br>
<label>Surname:</label> <input type="text" name="LastName" size="10" />
</form>
HTML:
<form action="index.php" method="GET">
<label>Name:</label><input type="text" name="FirstName" size="10" /><br />
<br />
<label>Surname:</label> <input type="text" name="LastName" size="10" /><br />
<br />
</form>
CSS:
label{
display: inline-block;
float: left;
clear: left;
width: 150px;
text-align: right;
}
input {
display: inline-block;
float: left;
}
Fiddle:http://jsfiddle.net/4ncwzLpx/
First, you should improve your HTML. <br /> are ugly and you should associate the text with the inputs using labels:
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>
Now, you can try displaying the form as a table:
form {
display: table;
}
label{
display: table-row;
}
form {
display: table;
}
label{
display: table-row;
}
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>
And now, to align the inputs,
You can align the content to the right:
label {
text-align: right;
}
form {
display: table;
}
label{
display: table-row;
text-align: right;
}
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>
Or, if you want the text aligned to the left, you can float the inputs to the right:
input {
float: right;
}
form {
display: table;
}
label{
display: table-row;
}
input {
float: right;
}
<form action="index.php" method="GET">
<label>
Name: <input type="text" name="FirstName" size="10" />
</label>
<label>
Surname: <input type="text" name="LastName" size="10" />
</label>
</form>

How do you align input texts?

I was trying to make a form with a username, password and an email. But for some reason the input text or the box for email isn't aligned with the boxes for the username and the password. I was wondering if there's a way to make them all align each other.
<form>
<label for="username">Username</label>
<input type="text" id="username" name="username" maxlength="30"><br><br>
<label for="password">Password</label>
<input type="password" id="password" name="password"><br><br>
<label for="email">Email</label>
<input type="email" id="email" name="email" maxlength="30">
<br>
<input type="submit" value="Register">
</form>
It's just for the sake of making everything look nice and pretty.
Oh man... Tables?? HTML from '90s incoming!
<style>
label {
width: 80px;
display: inline-block;
}
</style>
<form>
<label for="username">Username</label>
<input type="text" id="username" name="username" maxlength="30"><br><br>
<label for="password">Password</label>
<input type="password" id="password" name="password"><br><br>
<label for="email">Email</label>
<input type="email" id="email" name="email" maxlength="30">
<br>
<input type="submit" value="Register">
</form>
I went for a different approach than a table, since if your are going to table up your form, I suggest you use a solid css framework, which is simply better.
This is the approach of CSS only A Cool Fiddle
form {
width: 80%;
margin: 0 auto;
}
label, input {
/* in order to define widths */
display: inline-block;
}
label {
width: 30%;
/* positions the label text beside the input */
text-align: right;
}
label + input {
width: 30%;
/* large margin-right to force the next element to the new-line
and margin-left to create a gutter between the label and input */
margin: 0 30% 0 4%;
}
/* only the submit button is matched by this selector,
but to be sure you could use an id or class for that button */
input + input {
float: right;
}
input[type="submit"]{
margin: 4% 40%;
}
With all that said, I also suggest you change the old way of forms being written with label values to placeholder.
for more reference Placeholders are cool!
<form>
<table>
<tr> <td> <label for="username">Username</label> </td> <td> <input type="text" id="username" name="username" maxlength="30"> </td> </tr>
<tr> <td> <label for="password">Password</label> </td> <td> <input type="password" id="password" name="password"></td> </tr>
<tr> <td> <label for="email">Email</label> </td> <td><input type="email" id="email" name="email" maxlength="30"></td> </tr>
<tr> <td></td> <td> <input type="submit" value="Register"> </td> </tr>
</table>
</form>
should work, just surroundet it with a table.
<table>
<form>
<tr>
<td> <label for="username">Username</label></td>
<td> <input type="text" id="username" name="username" maxlength="30"> </td>
</tr>
<tr>
<td><label for="password">Password</label></td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><label for="email">Email</label></td>
<td><input type="email" id="email" name="email" maxlength="30"></td>
</tr>
<tr>
<td><input type="submit" value="Register"></td>
</tr>
</form>
</table>
It is always a good practice to make any form in either a Table or in a Div.