Home

CSS Examples Tableless forms using CSS

Date: 02/10/2005 Tableless forms using CSS

Author: CSS Drive

This CSS example transforms a conventional form so it's tableless. A form that doesn't use tables for its layout is much more lightweight and semantically correct.





The CSS:

<style type="text/css">

label{
float: left;
width: 120px;
font-weight: bold;
}

input, textarea{
width: 180px;
margin-bottom: 5px;
}

textarea{
width: 250px;
height: 150px;
}

.boxes{
width: 1em;
}

#submitbutton{
margin-left: 120px;
margin-top: 5px;
width: 90px;
}

br{
clear: left;
}

</style>

The HTML:

<form>

<label for="user">Name</label>
<input type="text" name="user" value="" /><br />

<label for="emailaddress">Email Address:</label>
<input type="text" name="emailaddress" value="" /><br />

<label for="comments">Comments:</label>
<textarea name="comments"></textarea><br />

<label for="terms">Agree to Terms?</label>
<input type="checkbox" name="terms" class="boxes" /><br />

<input type="submit" name="submitbutton" id="submitbutton" value="Submit" />

</form>

By floating the "label" tag to the left, the text description of each form field appears to the left, resulting in a "two column" look for the form. The width of the "label" is controlled, so it will comfortably contain the longest text description in the form. I throw in a <br> tag with "clear: left" after each form field to prevent the floating <label> tag from potentially spilling over to content beneath it. Margins such as "margin-top" and "margin-bottom" are also used to add some nice margins between form fields.

Comments (108)

That's a horrible solution! It uses fixed sizes in pixels. If you use tables, it will intelligently size the columns.

#1: John on 04/29 at 01:38 AM

This is for testing only.

#2: Rohini on 05/07 at 04:48 AM

To John:

You can also use percentages or other measurement types to make these flow as well. Tables are certianly not the ideal solution.

#3: Ryan on 05/10 at 03:20 PM

The float didn't seem seem to work right for me at all. I have IE 6.0 on Windows XP. The columns are directly after the label so it doesn't look good at all. And the label is aligning at the bottom, not the top as in the example.

#4: Denise on 06/08 at 04:59 PM

Actually, it wasn't finding my .css file. I prefer to use .css files and reference them instead of adding the style code to the header. When I put the css code directly in the header it works. There's no logical reason for it not to work using a .css file. My reference is below:

<stylesheet" TYPE="text/css" HREF="tableless_form.css">

There are no typos and the file resides in the same dir as the html file. Any ideas why it doesn't work?

#5: Denise on 06/08 at 05:10 PM

the validation error is fixed by adding cols="" rows=""

< name="comments" cols="" rows="" id="comments"textarea><textarea>

#6: beeouder on 06/08 at 06:34 PM

thanks

#7: Raul on 06/09 at 04:08 PM

>> <stylesheet" TYPE="text/css" HREF="tableless_form.css">
>> Any ideas why it doesn't work?

RTFM!

Use:

<stylesheet" type="text/css" href="tableless_form.css" />

#8: o my god on 06/21 at 10:56 PM

ou crap... forum's php code sucks big time...

#9: o my god on 06/21 at 10:57 PM

This is nice, but you have to hard code the label width. Is there any way of making them expandable according to their content and yet still making all inputs align, like you can do with tables?

#10: defaulk on 07/11 at 01:06 AM
Commenting is not available in this weblog entry.


Partners & ResourcesOur
Partners


CSS Forums News

Dynamic Drive forums | Register

The Latest Comments

All images and content copyright © 2017 CSS Drive. Contact Info | Back to Top
Affiliate Discloser: We receive a commission from purchases through some links on this site