Align HTML input fields by :

Sankar picture Sankar · Jun 3, 2012 · Viewed 257.3k times · Source

I have a HTML form like:

 <html>
  Name:<input type="text"/></br>
  Email Address:<input type="text"/></br>
  Description of the input value:<input type="text"/></br>
 </html>

Now the labels all begin in the same column but the text boxes are beginning in different positions as per the label's text length.

Is there a way to align the input fields such that, all the ":" and the text boxes, will begin in the same position, and the preceding text will be right aligned until the ":" ?

I am okay with using CSS if that can help achieving this.

Answer

DaneSoul picture DaneSoul · Jun 3, 2012

Working JS Fiddle

HTML:

  <div>
      <label>Name:</label><input type="text">
      <label>Email Address:</label><input type = "text">
      <label>Description of the input value:</label><input type="text">
  </div>

CSS:

label{
    display: inline-block;
    float: left;
    clear: left;
    width: 250px;
    text-align: right;
}
input {
  display: inline-block;
  float: left;
}