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)

Failed XHTML 1.0 Strict validation, has anybody succed in validating both css and XHTML, if so which version of the above codes worked

#1: Horus on 10/22 at 08:09 PM

What if I need a third and fourth column? Should I repeat the process made in the first and second columns?

I´m not sure if that would be the best practice.

#2: Moardib on 10/26 at 01:15 PM

whats new here? you can even make border=0

#3: tobto on 11/07 at 02:23 PM

Does th eform check for errors/syntax? ie Correct email format,empty fields etc..

If not - could you show us the code that would?

#4: pinkpanther on 11/13 at 09:40 AM

never thought about solution like that. your light weight table is working fine for me.
thanks!

#5: Private Krankenversicherung on 11/20 at 03:08 PM

I want to use the tableless form, can you tell me where do I specify the recipient email address in the code.

#6: pclagon on 12/01 at 02:04 AM

fdsfsfacfx23

#7: jose on 12/15 at 02:47 AM

qikxiyiz mjnifwog osxqzjot http://jchxcrav.com flnjivya owqahwel

#8: kuykvdlz on 01/01 at 12:31 PM

medpilxv lkaxxjmv bsnsreoq http://zdkmsvnq.com unrxuinw epalgypo

#9: byffgrvw on 01/01 at 12:35 PM

deneme

#10: Hüseyin Usanmaz on 01/19 at 07:39 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