[html] How to align input forms in HTML

I'm new to HTML and I'm trying to learn how to use forms.

The biggest issue I am having so far is aligning the forms. Here is an example of my current HTML file:

<form>
 First Name:<input type="text" name="first"><br />
 Last Name:<input type="text" name="last"><br />
 Email:<input type="text" name="email"><br />
</form>

The problem with this is, the field box after 'Email' is drastically different in terms of spacing compared to first, and last name. What is the 'proper' way to make it so that they 'line-up' essentially?

I am trying to practice good form and syntax...a lot of people might do this with CSS I am not sure, I have only learned the very basics of HTML so far.

This question is related to html forms alignment

The answer is


using css

.containerdiv label {
  float:left;
  width:25%;
  text-align:right;
  margin-right:5px; /* optional */
}
.containerdiv input {
  float:left;
  width:65%;
}

this give you something like:

           label1 |input box             |
    another label |another input box     |

<form>
    <div>
        <label for='username'>UserName</label>
        <input type='text' name='username' id='username' value=''>  
    </div>
</form>

In the CSS you have to declare both label and input as display: inline-block and give width according to your requirements. Hope this will help you. :)


Well for the very basics you can try aligning them in the table. However the use of table is bad for layout since table is meant for contents.

What you can use is CSS floating techniques.

CSS Code

.styleform label{float:left;}
.styleform input{margin-left:200px;} /* this gives space for the label on the left */
.styleform .clear{clear:both;} /* prevent elements from stacking weirdly */

HTML

<div class="styleform">
<form>
<label>First Name:</label><input type="text" name="first" /><div class="clear"></div>
<label>Last Name:</label><input type="text" name="first" /><div class="clear"></div>
<label>Email:</label><input type="text" name="first" /><div class="clear"></div>
</form>
</div>

An elaborated article I wrote can be found answering the question of IE7 float problem: IE7 float right problems


I know this has already been answered, but I found a new way to align them nicely - with an extra benefit - see http://www.gargan.org/en/Web_Development/Form_Layout_with_CSS/

basically you use the label element around the input and align using that:

<label><span>Name</span> <input /></label>
<label><span>E-Mail</span> <input /></label>
<label><span>Comment</span> <textarea></textarea></label>

and then with css you simply align:

label {
    display:block;
    position:relative;
}

label span {
    font-weight:bold;
    position:absolute;
    left: 3px;
}

label input, label textarea, label select {
    margin-left: 120px;    
}
  • you do not need any messy br lying around for linebreaks - meaning you can quickly accomplish a multi-column layout dynamically
  • the whole line is click-able. Especially for checkboxes this is a huge help.
  • Dynamically showing/hiding form lines is easy (you just search for the input and hide its parent -> the label)
  • you can assign classes to the whole label making it show error input much clearer (not only around the input field)

I'm a big fan of using definition lists.

<dl>
<dt>Username:</dt>
<dd><input type="text" name="username" /></dd>
<dt>Password:</dt>
<dd><input type="password" name="password" /></dd>
</dl>

They're easy to style using CSS, and they avoid the stigma of using tables for layout.


It also can be done using CSS and without tables or floats or fixed lengths by changing the content direction to rtl and then back to ltr, but the labels must go after each input.

To avoid this markup reordering, just set the label's text in a data-* attribute and show it using an ::after pseudo-element. I think it becomes much clearer.

Here is an example setting the label's text in a custom attribute called data-text and showing them using the ::after pseudo-element, so we don't mess with markup while changing direction to rtl and ltr :

_x000D_
_x000D_
form
{
  display: inline-block;
  background-color: gold;
  padding: 6px;
}

label{
  display: block;
  direction: rtl; 
}

input{
  direction: ltr; 
}

label::after{
  content: attr(data-text);
}
_x000D_
<form>
  <label data-text="First Name">
    <input type="text" />
  </label>
  <label data-text="Last Name">
    <input type="text" />
  </label>
  <label data-text="E-mail">
    <input type="text" />
  </label>
</form>
_x000D_
_x000D_
_x000D_


A simple solution for you if you're new to HTML, is just to use a table to line everything up.

<form>
  <table>
    <tr>
      <td align="right">First Name:</td>
      <td align="left"><input type="text" name="first" /></td>
    </tr>
    <tr>
      <td align="right">Last Name:</td>
      <td align="left"><input type="text" name="last" /></td>
    </tr>
    <tr>
      <td align="right">Email:</td>
      <td align="left"><input type="text" name="email" /></td>
    </tr>
  </table>
</form>

Insert input tags inside an unordered lists.Make the style-type none. Here's an example.

<ul>
Input1     
<li> <input type="text" />
Input2
<li> <input type="text" />
<ul/>

Worked for me !


css I used to solve this problem, similar to Gjaa but styled better

p
{
    text-align:center;
        }
.styleform label
{
    float:left;
    width: 40%;
    text-align:right;
    }
.styleform input
{
    float:left;
    width: 30%;
    }

Here is my HTML, used specifically for a simple registration form with no php code

<form id="registration">
    <h1>Register</h1>
    <div class="styleform">
    <fieldset id="inputs">
        <p><label>Name:</label> 
          <input id="name" type="text" placeholder="Name" autofocus required>
        </p>
        <p><label>Email:</label>   
          <input id="email" type="text" placeholder="Email Address" required>
        </p>
        <p><label>Username:</label>
          <input id="username" type="text" placeholder="Username" autofocus required>
        </p>
        <p>   
          <label>Password:</label>
          <input id="password" type="password" placeholder="Password" required>
        </p>
    </fieldset>
    <fieldset id="actions">

    </fieldset>
    </div>
    <p>
          <input type="submit" id="submit" value="Register">
          </p>

It's very simple, and I'm just beginning, but it worked quite nicely


The accepted answer (setting an explicit width in pixels) makes it hard to make changes, and breaks when your users use a different font size. Using CSS tables, on the other hand, works great:

_x000D_
_x000D_
form  { display: table;      }_x000D_
p     { display: table-row;  }_x000D_
label { display: table-cell; }_x000D_
input { display: table-cell; }
_x000D_
<form>_x000D_
  <p>_x000D_
    <label for="a">Short label:</label>_x000D_
    <input id="a" type="text">_x000D_
  </p>_x000D_
  <p>_x000D_
    <label for="b">Very very very long label:</label>_x000D_
    <input id="b" type="text">_x000D_
  </p>_x000D_
</form>
_x000D_
_x000D_
_x000D_

Here's a JSFiddle: http://jsfiddle.net/DaS39/1/

And if you need the labels right-aligned, just add text-align: right to the labels: http://jsfiddle.net/DaS39/


EDIT: One more quick note: CSS tables also let you play with columns: for example, if you want to make the input fields take as much space as possible, you can add the following in your form

<div style="display: table-column;"></div>
<div style="display: table-column; width:100%;"></div>

you may want to add white-space: nowrap to the labels in that case.


You should use a table. As a matter of logical structure the data is tabular: this is why you want it to align, because you want to show that the labels are not related solely to their input boxes but also to each other, in a two-dimensional structure.

[consider what you would do if you had string or numeric values to display instead of input boxes.]


I find it far easier to change the display of the labels to inline-block and set a width

label {
    display: inline-block;
    width:100px;
    text-align: right;
}

For this, I prefer to keep a correct HTML semantic, and to use a CSS simple as possible.

Something like this would do the job :

label{
  display: block;
  float: left;
  width : 120px;    
}

One drawback however : you might have to pick the right label width for each form, and this is not easy if your labels can be dynamic (I18N labels for instance).


Simply add

<form align="center ></from>

Just put align in opening tag.


Clément's answer is by far the best. Here's a somewhat improved answer, showing different possible alignments, including left-center-right aligned buttons:

_x000D_
_x000D_
label_x000D_
{ padding-right:8px;_x000D_
}_x000D_
_x000D_
.FAligned,.FAlignIn_x000D_
{ display:table;_x000D_
}_x000D_
_x000D_
.FAlignIn_x000D_
{ width:100%;_x000D_
}_x000D_
_x000D_
.FRLeft,.FRRight,.FRCenter_x000D_
{ display:table-row;_x000D_
 white-space:nowrap;_x000D_
}_x000D_
_x000D_
.FCLeft,.FCRight,.FCCenter_x000D_
{ display:table-cell;_x000D_
}_x000D_
_x000D_
.FRLeft,.FCLeft,.FILeft_x000D_
{ text-align:left;_x000D_
}_x000D_
_x000D_
.FRRight,.FCRight,.FIRight_x000D_
{ text-align:right;_x000D_
}_x000D_
_x000D_
.FRCenter,.FCCenter,.FICenter_x000D_
{ text-align:center;_x000D_
}
_x000D_
<form class="FAligned">_x000D_
 <div class="FRLeft">_x000D_
  <p class="FRLeft">_x000D_
   <label for="Input0" class="FCLeft">Left:</label>_x000D_
   <input id="Input0" type="text" size="30"  placeholder="Left Left Left" class="FILeft"/>_x000D_
  </p>_x000D_
  <p class="FRLeft">_x000D_
   <label for="Input1" class="FCRight">Left Right Left:</label>_x000D_
   <input id="Input1" type="text" size="30"  placeholder="Left Right Left" class="FILeft"/>_x000D_
  </p>_x000D_
  <p class="FRRight">_x000D_
   <label for="Input2" class="FCLeft">Right Left Left:</label>_x000D_
   <input id="Input2" type="text" size="30"  placeholder="Right Left Left" class="FILeft"/>_x000D_
  </p>_x000D_
  <p class="FRRight">_x000D_
   <label for="Input3" class="FCRight">Right Right Left:</label>_x000D_
   <input id="Input3" type="text" size="30"  placeholder="Right Right Left" class="FILeft"/>_x000D_
  </p>_x000D_
  <p class="FRLeft">_x000D_
   <label for="Input4" class="FCLeft">Left Left Right:</label>_x000D_
   <input id="Input4" type="text" size="30"  placeholder="Left Left Right" class="FIRight"/>_x000D_
  </p>_x000D_
  <p class="FRLeft">_x000D_
   <label for="Input5" class="FCRight">Left Right Right:</label>_x000D_
   <input id="Input5" type="text" size="30"  placeholder="Left Right Right" class="FIRight"/>_x000D_
  </p>_x000D_
  <p class="FRRight">_x000D_
   <label for="Input6" class="FCLeft">Right Left Right:</label>_x000D_
   <input id="Input6" type="text" size="30"  placeholder="Right Left Right" class="FIRight"/>_x000D_
  </p>_x000D_
  <p class="FRRight">_x000D_
   <label for="Input7" class="FCRight">Right:</label>_x000D_
   <input id="Input7" type="text" size="30"  placeholder="Right Right Right" class="FIRight"/>_x000D_
  </p>_x000D_
  <p class="FRCenter">_x000D_
   <label for="Input8" class="FCCenter">And centralised is also possible:</label>_x000D_
   <input id="Input8" type="text" size="60"  placeholder="Center in the centre" class="FICenter"/>_x000D_
  </p>_x000D_
 </div>_x000D_
 <div class="FAlignIn">_x000D_
  <div class="FRCenter">_x000D_
   <div class="FCLeft"><button type="button">Button on the Left</button></div>_x000D_
   <div class="FCCenter"><button type="button">Button on the Centre</button></div>_x000D_
   <div class="FCRight"><button type="button">Button on the Right</button></div>_x000D_
  </div>_x000D_
 </div>_x000D_
</form>
_x000D_
_x000D_
_x000D_

I added some padding on the right of all labels (padding-right:8px) just to make the example slight less horrible looking, but that should be done more carefully in a real project (adding padding to all other elements would also be a good idea).


The traditional method is to use a table.

Example:

<table>
  <tbody>
     <tr>
        <td>
           First Name:
        </td>
        <td>
           <input type="text" name="first">
        </td>
     </tr>
     <tr>
        <td>
           Last Name:
        </td>
        <td>
           <input type="text" name="last">
        </td>
     </tr>
  </tbody>
</table>

However, many would argue that tables are restricting and prefer CSS. The benefit of using CSS is that you could use various elements. From divs, ordered and un-ordered list, you could accomplish the same layout.

In the end, you'll want to use what you're most comfortable with.

Hint: Tables are easy to get started with.


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to alignment

How do I center text vertically and horizontally in Flutter? CSS align one item right with flexbox What's the difference between align-content and align-items? align images side by side in html How to align iframe always in the center How to make popup look at the centre of the screen? Responsive image align center bootstrap 3 How to align title at center of ActionBar in default theme(Theme.Holo.Light) Center align a column in twitter bootstrap How do I position an image at the bottom of div?