Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make text input fields and their labels line up correctly

Here's an excerpt from a fairly standard Rails form:

  <p>
    <%= f.label :from_station %> <%= f.text_field :from_station %>
  </p>
  <p>
    <%= f.label :to_station %> <%= f.text_field :to_station %>
  </p>

By default, this renders like this:

alt text http://img412.imageshack.us/img412/127/picture6u.png

This doesn't look great since the text fields don't line up. What's the easiest way to make the form look more like this:

alt text http://img193.imageshack.us/img193/746/picture7shk.png

I've tried setting the width style property on the <label>s, but this property doesn't seem to do anything.

like image 438
Tom Lehman Avatar asked Aug 09 '09 22:08

Tom Lehman


People also ask

How do you align inputs and labels?

We specify the margin-bottom of our <div> element. Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.

How do you align a textbox and label on the same line?

Using float and overflow attributes: Make a label and style it with float attribute. Now set the label float(position) left or right according to your requirement. This will align your label accordingly. Overflow property for input is used here to clip the overflow part and show the rest.

How do I align text fields in HTML?

HTML | <input> align Attribute left: It sets the alignment of image to the left. it is a default value. right: It sets the alignment of image to the right. middle: It sets the alignment of image to the middle.


2 Answers

I think this is more of a CSS question;

Labels by default aren't a block level element and so won't accept a width. Try setting this CSS:

label{
  width: 4em;
  float: left;
  text-align: right;
  margin-right: 0.5em;
  display: block
}

Hope that helps!

like image 180
Al. Avatar answered Oct 14 '22 03:10

Al.


You can use a <table> where each label is in column 1 and each textfield is in column 2.

like image 44
sepp2k Avatar answered Oct 14 '22 05:10

sepp2k