Kevin Marshall's Epic Work Blog for Awesome People

@ksmarshall - i <3 software
in

CSS and Forms aka Forms Without Tables

One thing I've noticed while doing web development is that most people use tables when laying out forms.  In my never-ending quest to eliminate tables in web design for layout, I thought I'd demonstrate how to have a form with aligned columns without a table.  Not that this is innovative or anything, it just seems like many people are unaware of how to do it.  So instead of this:

<table>
<tr>
    
<td>Name:</td><td><input type="text" name="name"></td>
</tr>
<tr>
    
<td>Email:</td><td><input type="text" name="email"></td>
</tr>
<tr>
    
<td>Phone:</td><td><input type="text" name="phone"></td>
</tr>
<tr>
    
<td>Address:</td><td><input type="text" name="address"></td>
</tr>
<tr>
    
<td>City:</td><td><input type="text" name="city"></td>
</tr>
</table>

You can use CSS to style the lablel elements like so:

<style type="text/css">
    fieldset
    
{
        width
: 16em;
    
}
    label
    
{
        font-size
: 1em;
        width
: 5em;
        float
: left;
        margin-right
: 0.5em;
        display
: block;
    
}
    
#rightAligned label
    
{
        text-align
: right;
    
}        
</
style>
<fieldset id="rightAligned">
    
<legend>Right Aligned</legend>
    
<p><label for="name">Name:</label><input type="text" name="name"></p>
    
<p><label for="email">Email:</label><input type="text" name="email"></p>
    
<p><label for="phone">Phone:</label><input type="text" name="phone"></p>
    
<p><label for="address">Address:</label><input type="text" name="address"></p>
    
<p><label for="city">City:</label><input type="text" name="city"></p>
    
<input type="button" class="btn" value="Save">
</fieldset>

Which looks like this:

Posted: Feb 22 2006, 10:18 AM by kmarshall | with 4 comment(s)
Filed under:

Comments

webmojo said:

Great just what Ive been Looking for, Making the transition away from tables and having major headaches

# May 8, 2008 2:07 PM

utube said:

# July 24, 2008 6:57 AM

ketir7 said:

is that australian >?>>

# April 12, 2010 2:40 PM

ClubPenguinCheats said:

Looking for, Making the transition away from tables and having major headaches

# April 13, 2010 8:02 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)