Google

Thursday, October 25, 2007

Fun with forms – customized input elements

Fun with forms – customized input elements
By Søren Madsen

Ever wanted to match the look of your HTML forms with the rest of your website? This article demonstrates how to apply customized backgrounds to HTML forms, while preserving stucturally clean markup and accesibility.
Illustration that compares unstyled forms, with CSS styled forms proposed in this article
A few considerations before we begin

Before you throw yourself at this, there are some important considerations you have be aware of.

Usability
Users trust their credit card and personal information with forms on the internet, and that trust is sacred. So changing the appearance of well-known and recognizable browser elements like the basic input elements, may therefore not always be advisable. If you know your audience well enough, and you're confident that changing the appearance of form elements won't confuse or distract said audience from ie. an online purchase, please read on.
Differences in user agents
The techniques we're about to demonstrate are not generic. Form elements are "replaced elements", which means they come directly from the heart of your OS, and not the browser itself. The CSS specifications do therefore not require that form elements should be able to receive styling.
Most popular browsers have however provided us with options to do so, but not all of them. In fact, the following won't work at all in browsers like Safari , since eg. Safaris form elements are tied closely to the Mac OS presentation system, and therefore doesn't allow styling form elements much more than basic height and width. (Update: The following technique does indeed work in the new Safari 1.2 - Argh, I received contradictory reports! No, it doesn't)
So what are we left with? Well — from what yours truly has been able to test, the following works in IE5+, partially in Opera, and gecko-based browsers (Netscape, Mozilla/Firebirdfox).
Logical restrictions
With this technique, we basically hide the given form element, and replace it with graphic backgrounds. Since that doesn't really make sense with all form elements like select, file fields, radio buttons and check boxes due to their behaviour and functions, I've restricted the scope of this article to work with input fields, text areas and submit buttons, and if you're fine with that — by all means, read on.

Some form basics
– the
and elements

The fieldset element functions as a structural container for different sections within a form element. An example would be an order form, which is divided in several structural sections, such as personal information, credit card information and shipping information — but all part of the same form element. To provide meaningful structure between these sections, you would use the fieldset element. The legend element provides a feature to give each of these fieldset groupings a caption/title. Here's what it might look like:



Personal information
input fields here ...


Credit card information
input fields here ...


Shipping information
input fields here ...


submit/reset buttons here ...



Note that the legend element is optional.
– the

No comments: