I want to display an HTML form containing labelled text fields, like this:
First Name: [_____________] Last Name: [_____________] Date of Birth: [________]
My obvious approach is to use a <TABLE>
and simply place the labels and text fields in its cells, but is there a better way, e.g. a CSS-based approach?
EDIT:
- I'm looking for a way that reduces the verbosity in the HTML file.
- And yes, I'm looking for auto-sizing of the labels. See a related question about wrapping labels
In terms of usability and if vertical space is not a limiting factor, a list of fields with the label above each field is the quickest to read and fill out, and can be done aesthetically. See many of the usability studies on the web for more info, eg. http://www.lukew.com/resources/articles/web_forms.html
I think something like this is what i do but also won't autosize to the length of the text but it's cleaner in my opinion
Typically, I have found that there are at least some issues when not using Tables for forms. The general things I have not been able to solve:
I might be missing a few things, but the most flexible mark-up if you are using CSS is as below:
You have issues if you want multiple fields per label section, so you have to introduce another div to hold the inputs (to allow the label to still float left etc):
With the above mark-up you can achieve very flexible forms, I won't post the CSS as it's very similar to a 2 Column-Layout.
I still need to sit down and try and figure out if pure CSS forms are viable for all occasions but tbh it's very unlikely!
Forms are the worst thing to style using CSS. The only major Cross Browser problems i've had are when styling the FieldSet and Legend elements. No real hacks, but they take some work to look good.
Use a CSS framework like Blueprint and use that to style the forms.
Another trick would be to create virual "columns" with css and float them next to each other. Labels in one column and inputs in another. Put that (both columns) in a div with a large enough width and float the columns the opposite way you want to align them.
Here's some code (because I am creating a form) that will work for basic forms. The only constraint is the large right margin on inputs.
And layout like so
On top of this small, narrowly written code, there is an entire article related to creating tableless forms in CSS.
The article is a bit old, but I've always found a list apart's advice to be solid: (if you do want to get rid of your tables)
http://www.alistapart.com/articles/prettyaccessibleforms/
CSS Table Display
From IE8+ you can use CSS Table Display for this: