Vertically Aligning Input Fields Using CSS (No Tables!)

by Justin 5. February 2008 20:35

A common design requirement is for the input fields of a form to be vertically aligned, each to the right of some descriptive text. The look is very clean and easily understood by users because of its widespread use.

Those who want to use this form layout along with a purely CSS-based design should try the following technique. Simply float a label element to the left and set its width. I can verify that this works in Firefox, Internet Explorer 6 and 7, Opera, and Safari. For reference I've included aligning input fields using a table so you may compare the differences.

Aligning Input Fields Using CSS

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Aligning Form Fields Using CSS</title>
        <style type="text/css">
            .form-field
            {
                margin-bottom:5px;
            }
            .form-field label
            {
                float:left;
                width:120px;
            }
        </style>
    </head>
    <body>
        <form action="">
            <div class="form-field">
                <label for="name">Name:</label>
                <input id="name" type="text" name="name" />
            </div>
            <div class="form-field">
                <label for="email">E-mail:</label>
                <input id="email" type="text" name="email" />
            </div>
            <div class="form-field">
                <label for="subject">Subject:</label>
                <input id="subject" type="text" name="subject" />
            </div>
            <p>
                <label for="body">Body:</label><br />
                <textarea id="body" name="body" cols="30" rows="6">
                </textarea><br />
                <input type="submit" />
            </p>
        </form>
    </body>
</html>

 

Aligning Input Fields Using a Table

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Aligning Form Fields Using a Table</title>
        <style type="text/css">
            .input-align
            {
                text-align:right;
            }
        </style>
    </head>
    <body>
        <form action="">
            <table style="width:260px">
                <tr>
                    <td>
                        <label for="name">Name:</label>
                    </td>
                    <td class="input-align">
                        <input id="name" type="text" name="name" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="email">E-mail:</label>
                    </td>
                    <td class="input-align">
                        <input id="email" type="text" name="email" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="subject">Subject:</label>
                    </td>
                    <td class="input-align">
                        <input id="subject" type="text" name="subject" />
                    </td>
                </tr>
            </table>
            <p>
                <label for="body">Body:</label><br />
                <textarea id="body" name="body" cols="30" rows="6"> 
                </textarea><br />
                <input type="submit" />
            </p>
        </form>
    </body>
</html>

Currently rated 2.9 by 14 people

  • Currently 2.857143/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Justin HoltonHi, my  name is Justin. I cut my teeth learning HTML back when Netscape Navigator was still the most popular web browser. Later that inspired me to major in Computer Science at college. Today I'm a professional web developer with experience in everything from social networking application design to Search Engine Optimization (SEO). I believe the Internet is the most important achievement of man since the printing press, and I'm grateful that I was born in time to see it go from obscurity to a ubiquity.

Page List