views:

222

answers:

3

How do I line up textfields so they look more neat? At the moment I've got:

<div id="content">
    <h2>Change password</h2>
    <% form_tag({:action => "change_password"}, :method => "post") do %>
    <%= @error %>

    <div class="form_row">
     <label for="current_password">Current password:</label>
     <%= password_field_tag 'current_password', "", :size => 15 %>
    </div>

    <div class="form_row">
     <label for="new_password">New password:</label>
     <%= password_field_tag 'new_password', "", :size => 15 %>
    </div>

    <div class="form_row">
     <label for="repeat_new_password">Repeat new password:</label>
     <%= password_field_tag 'repeat_new_password', "", :size => 15 %>
    </div>

    <%= submit_tag "Set new password", :class => "submit" %>
    <% end %>
</div>
+4  A: 

You put a width in the label element.

<form action="">
    <label style='float: left; display: block; width: 100px;'>Hello</label><input type="text" size="3"><br />
    <label style='float: left; display: block; width: 100px;'>Long World</label><input type="text" size="3"><br />
    <label style='float: left; display: block; width: 100px;'>How are you?</label><input type="text" size="3"><br />
</form>
Ólafur Waage
be sure to migrate your styles to an external stylesheet and use a class...
nickmorss
@nickmorss Yes.
Ólafur Waage
oh and try to use a relative width... like 10em so it scales.i tend to put my forms into a fieldset with a legend. i then use an orderd list and put each 'row' in as a list item... makes clkearing the floats a little easier.
nickmorss
+2  A: 

In A List Apart are a beautiful article on how to do it, with all kinds of safeguards for older browers

http://www.alistapart.com/articles/prettyaccessibleforms

Eduardo Molteni
A: 

For your question: Set a width to the label element. On a separate note, I'd do the styling in an external stylesheet and use the following syntax for form, just a matter of taste. Sure it is a bit more verbose, but a bit easier to style imo.

<div id="#content">
    <h2>Change Password</h2>
    <form>
        <dl>
            <dt>
                <label for="elm">Test:</label>
            </dt>
            <dd>
                <input type="text" id="elm" />
            </dd>
        </dl>
    </form>
</div>
Dmitri Farkov