Subscribe to RSS

How to create a good looking form without tables, using CSS

There are a lot of different attempts that we have all made over the years to master the quirky art of creating an aesthetically pleasing form. The trouble is being able to control the form and have your labels and inputs match up nicely without going to too much work. We’ve all made a form in tables, regrettably. For some this has been the only option due to restraints from CSS or frustration with cross-browser compatibility.

There is more than just having a label and an input when it comes to accessible form creation. It is important to utilize the “for” attribute within the label tag to ensure that screen readers are able to associate the label with the input object. It is also a huge usability addition to be able to click the label and set focus to the input object, especially with radio boxes.

A proper and simple form should look something like this:

<dl class="newsletter">
<dt><label for="name">Name:</label></dt>
<dd><input type="text" id="name" value="Enter your name" /></dd>
<dt><label for="email">Email:</label></dt>
<dd><input type="text" id="email" value="Enter your email" /></dd>
</dl>

As I’m sure you have noticed I used a definition list in this example. This is the basic HTML markup for styling a form with CSS. Using tables to do this is no longer necessary and this method will actually prove to be significantly less work. Especially if you have multiple forms throughout your site, with the beauty of CSS.

The CSS to ensure that this form works correctly across all browsers is:

<style type="text/css">
.newsletter {width: 50%;}
.newsletter dt {float: left;}
.newsletter dd {margin: 0 5em 1em 5em;}
</style>

With these simple lines of CSS and following the plain example, you can simply add on to it with additional markup to build on the visual appeal of the form.

Popularity: 7%

Whats Next?

  • Save to Delicious! Save the page

9 Responses to “How to create a good looking form without tables, using CSS”

  1. Matthew Griffin Says:

    Great article, Dustin. Forms are shaky ground a lot of the time. Also, I love the new design.

  2. Jennifer Kyrnin Says:

    Great post! Using a definition list for forms is an excellent way to get nice looking forms. The only caveat I would leave is that you have to be very careful about the width of your form elements - if they are too wide, they can wreak havoc with the design. And because the labels are floated, you can end up with form fields in unexpected locations. But this is an excellent use of the definition list.

  3. Dustin Brewer Says:

    @Matthew I’m glad you like the new design. :)

    @Jennifer Thank you. I agree, it is important to tweak the form beyond the general width percentages I have set to match your needed placement of your form. I thought about adding a fully marked up version to show what can be done. May save that for another article, something like “Beautifully designed web forms using css.”

  4. grafic7 Says:

    Hi dustin,
    It’s been a long time i’ve visited ur blog. And this post of yours on creating forms using CSS made me peek into your brand new looking blog. I must say it made my eyes pop. This design is simply beautiful. I like it.
    Also, can we expect something more on creating web forms and making it work? may be a tutorial…Wat’s your say?

  5. Dustin Brewer Says:

    @grafic7 Absolutely, I think I’ll have another post coming up about styling web forms that may be a little more in depth about how to manage them and may even add some tutorials using JavaScript to improve them.

    I’ve gotten a few email questions about forms since posting this also that I’m going to be answering soon.

    I’m glad you like the design, I’ll be tweaking it a little adding some stuff in the coming days to make it look even better— I’ll also be updating some older posts to make sure they all adhere to the current design.

  6. E. I. Says:

    It would have been nice to have added a picture of what the form looked like.

    I’ve been trying to use this dl/dt model for some lists at our site.

    Works pretty well.

  7. Raphael Essoo-Snowdon Says:

    Thanks Dustin, what a quick and easy insight into creating forms without using tables. Thanks again. Bookmarked!

  8. Cyril Thomas Says:

    Hi
    So long I was using the ul,li tags to construct a form. This one is new for me and I am really going to give it a try in my next form page.

    Regards Cyril

  9. Stephen Says:

    Is there a way to right align the DT so that its flush right, jagged left?

    Tried “text-align: right”, to no avail…

Leave a Reply


Recent comments

I’ve been on a vacation, normality will happen next week

On August 28, 2008, holiday travel wrote:

well this is useful… (at least for me) very thanks ————&# 8212;———R 12;—–...

Top 10 search engine optimization techniques

On August 26, 2008, ибп wrote:

А что, неплохо!

Styling the first post of your Wordpress blog

On August 21, 2008, cheap website design wrote:

good point you have in there! thanks for sharing with us

CSS Hack:Getting Safari to behave

On August 21, 2008, Glass wrote:

You can also target Safari with the following: body:first-of-type .class {} That is: just prepend css the rule with body:first-of-type. Simple.

Styling the first post of your Wordpress blog

On August 20, 2008, jeeremie wrote:

Hi, This solution didn’t work for me. Instead, I use this one »