(Functional) Pretty FormsAugust 12th, 2005
Forms have been a rough spot when it comes to style. There’s just only so much you can do; the browsers just don’t allow the kind of styling I’d like. The issue is often discussed, and a nice demonstration of the problem can be found over at Roger Johansson’s site.
There have also been a few solutions attempted. Until I really started working on this, the best solution I was able to find was at badboy media. But there were problems with that solution, so I went about finding my own way.
My end goal was to make my forms look like this:
There were a couple of other requirements I had (that were not necessarily met in other solutions):
- Must respect preselected elements. For example if, when a page loads, a checkbox is automatically selected, it needs to stay selected. This was critical, I plan on using this for a large application
- Must also trigger events! For example, if my select box has an onChange event, that event must get triggered when I choose an item.
- Must allow me to size my form fields however I like. Many other solutions use a single image to replace a form. That doesn’t cut it. I need this to work on all my form fields, regardless of size.
In other words, I should be able to drop this thing on any page in just a few lines, and it should deliver great looking forms without interrupting any existing functionality.
So how’d it go?
It was that 3rd requirement above that proved tricky.
The sample page has a few events on a couple of different elements. When triggered, there should be a bit of text a the bottom saying so. The one caveat here is that the checkboxes ignore the onClick event. It will respond to onChange though. Also, I only bothered with onClick and onChange, but it would be very easy to add other events as well.
So want to use/tinker with this? Here is a zip file that contains everything.
To put the effect on your own page simply include the following in your html:
<link rel="stylesheet" href="[your path here]/prettyForms.css" type="text/css" media="screen" />
Then include this event in your onload:
Depending on how your page is built this might muck up your layout. All of the form elements will now be floated to the left:
I always float my form fields like this so it’s not a problem, but I could see how this might cause a little bit of tweaking on your page.
Also, the layout is affected by the doctype. I generally use strict whenever possible, so put this at the top of your page too:
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">and make sure your html validates and your css validates.
So that’s that. Let me know what you think. I’d especially like to know how it goes on various browsers/platforms. I have tested on IE6 and Firefox on the PC, and Firefox and Safari on the Mac. The borders aren’t quite right on Safari, I’ll get that fixed and post an update.
There are a number of things I’m still working on:
- Keyboard navigation: Using tab works reasonably well, but falls apart a bit on the select element.
- Although it’s a little-used element, I need to put something together for the mulitple select element for completeness.