Category

I love forms. I won’t deny it, they are pretty spectacular. Whether their purpose is to gather account information, make a reservation, or subscribe to a newsletter, every form is special in it’s own way. They are one of the most basic interactions a user will have with a website but it doesn’t mean they have to look that way.

With the advent of Web 2.0 and a focus on the User Experience we have moved past the days of simple black & white html forms to well crafted and visually appealing user interfaces. The first ingredient to a tasty form is valid and semantic markup. It’s the basic elements in a valid form that gives designers the control they want over how it looks. Let’s take a look at our example in it’s most basic structure.

Fig. 01 - Basic Form Structure (view page source).

Pretty ugly. In fact, downright hideous. Their is no consistency or visual appeal to the layout, no indication of focus or required fields, quite unfriendly to the average user. Unfortunately at this point a large portion of designers will immediately turn to tables to format their layout, every time this happens the internet dies a little inside.

It doesn’t have to be this way. We can achieve beautiful and accessible forms without tables, javascript, or an obscene amount of spaces by utilizing it’s important tags; <form> <fieldset> <label> <input> and a little bit of CSS magic. Another important tag that I didn’t use in our example form is <legend> , very useful for organizing large forms by adding captions to fieldsets.

Fig. 02 - Basic Form Style (view page source).

A huge sigh of relief. Adding some basic styling with CSS gives this form some much needed spacing and alignment. I started with giving the <form> a set width, padding, and a border. I wrapped each pair of <label> and <input> elements in a <div> to add some padding, a border, and for some fancy styling later on. What makes the <label> and <input> elements sit next to each other so nicely and not staggered is giving the <label> element a specified width and floating it to the left. The inline elements will fall into place and your left with a clean and clear form.

Fig. 03 - Fancy Form Style (view page source).

Taking your form to the next step in elegance and accessibility is only a few pieces of code away. I gave a background to each div wrapping the <label> and <input> elements. I also added a focus pseudo class for the inputs (input:focus, currently supported by Mozilla).

The simplicity of achieving effective and accessible forms with CSS gives no excuse for the use of tables. There are plenty of great techniques to achieving any layout or effect with your forms, all it takes is a little forethought and ingenuity.

*Known Issues
If your label is longer then a few words it must extend the wrapping div, this pushes the next div below to the right. To fix this you can play around with floating the wrapper div as well as the label.

9 Responses to “A Formula for better Forms”

  1. 1

    Pakcikkantin

    May 15th, 2008 at 7:59 pm

    Thanks man, i really need this . Last time i used tables and td, th
    But now i will use this.

  2. 1

    Chris

    May 15th, 2008 at 8:00 pm

    Great to hear. Now we have to get the rest of the internet away from Tables.

  3. 1

    Francisco Camberos

    May 15th, 2008 at 8:01 pm

    Great post. I love forms too! :P

    Nice site, by the way..

  4. 1

    Chris

    May 15th, 2008 at 8:01 pm

    Francisco Camberos: Glad to see there are more form fanatics out there like me. I appreciate the kind words.
    Cheers.

  5. 1

    michael

    May 16th, 2008 at 7:52 am

    its a form man, i put them in tables because it takes a quarter of the time it does using css. no real reason not to use tables, not like you want good seo on your tables

  6. 1

    Chris Morris

    May 16th, 2008 at 7:53 am

    Tables weren’t meant to be used for layout and structuring. Tables were meant for displaying raw data. And why not optimize your forms for SEO? Any bit helps.

  7. 1

    David

    May 16th, 2008 at 7:54 am

    “Tables weren’t meant to be used for layout and structuring. Tables were meant for displaying raw data.”

    That is my view exactly. However, I think that a simple form IS part of the page “data”.

    But at any case you should add a to the code for more control and fine-tunning that a simple “fieldset/label” combo won’t allow. I just built the whole admin area of my php system (codexplorer.com) this way.

  8. 1

    Chris Morris

    May 16th, 2008 at 7:55 am

    To each their own when it comes to forms. Building their layout with CSS does take a bit more time, but if you generalize all your classes you can reuse it as much as you want.

    David: Thanks for the heads up on code in the comments. I should also implement a specific reply function as well.

  9. 1

    Heartburn Home Remedy

    April 15th, 2009 at 4:25 am

    Not that I’m impressed a lot, but this is a lot more than I expected for when I stumpled upon a link on SU telling that the info is awesome. Thanks.

Leave a Reply



About

Chris Morris is a freelance designer & developer based in Lethbridge Alberta, Canada. He is passionate about creating appealing, accessible websites and print solutions. Chris also has an affinity for guitars, hooded sweatshirts and bendy straws.

Status

RT @awilkinson: Just made a blog post about Mozilla ripping us off. Best part: we sent them an estimate for the work. http://bit.ly/aKxbRT 5 hrs ago

I enjoy watching "How It's Made" half way through a segment and trying to guess what they're making. 6 hrs ago

More updates...