Create a stylish accessible form

Written by That Web Guy on 1st July 2009. 14 comments

Create a stylish accessible form
  • Avant Innovations
  • Advertise Here

There are a stack of different ways to lay out a form, but honestly sometimes I’m undecided on which way is better. Some say there’s nothing wrong with using a table to position your form elements, but I reckon that makes it semantically meaningless.

Forms have admittedly always been a field (bad pun, sorry) of HTML that I've not paid as much attention to as I should have, but I recently decided to create a functional template with the intention of re-cycling it when needed.

I had several goals for this exercise. The form needed to:

The finished product can be viewed here, and the complete source and demo is downloadable at the end of this article.

The XHTML bits

As accessibility is a concern, we will surely be putting in fieldsets, legends and labels. These elements also provide us with plenty of styling options, so no complaints so far.

So here's the form:

<form id="myform" method="post" action="">
<fieldset>
<legend>Personal</legend>
<label for="name">Name <span>(Your full name please)</span></label> <input type="text" id="name" />
<label for="web_site">Web Site <span>(No need to include http://)</span></label> <input type="text" value="www." id="web_site" />
<label for="email_address">Email Address <span>(Must be valid)</span></label> <input type="text" id="email_address" />
</fieldset>

<fieldset>
<legend>Professional</legend>
<label for="company">Company <span>(Name of your organisation)</span></label> <input type="text" id="company" />
<label for="position">Position <span>(What is your title?)</span></label> <input type="text" id="position" />
<label for="duration">Duration <span>(How long have you been there?)</span></label> <input type="text" id="duration" />
<label for="location">Location <span>(Seriously, where are you?)</span></label>
<select name="Location" id="location">
<option value="Australia"></option>
<option value="Australia">Australia</option>
<option value="Outside Australia">Outside Australia</option>
</select>
</fieldset>

<fieldset>
<legend>Battlestar Pop Quiz</legend>
<label>Cylon <span>Who is a among the final five?</span></label>
<input type="radio" id="adama" name="pie" class="narrow" /> <em>Adama</em>
<input type="radio" id="anders" name="pie" class="narrow" /> <em>Anders</em>
<input type="radio" id="hilo" name="pie" class="narrow" /> <em>Hilo</em>
</fieldset>

<fieldset>
<legend>Final Stage</legend>
<label for="comments">Comments <span>(Care to leave a comment?)</span></label> <input type="text" id="comments" />
<label>Agreement <span>Tick if you agree to all this</span></label>
<input name="agreement" type="checkbox" value="agreement" class="narrow" />
</fieldset>

<div><input type="submit" name="button" id="submit" value="Submit" class="submit" /></div>
</form>

Nothing out of the ordinary here, except I've placed spans within the labels with their display set to block in the CSS, which makes them fall below the label for the appearance we want.

This is better than dropping the text with a <br /> not only because we get to tweak the position with CSS, but also because it makes more sense to have this:

Name (Your full name please)

...instead of this...

Name
(Your full name please)

This is perfectly valid BTW. Ironically, the latter example is the appearance we are going for, but we'll get there with CSS.

With the label now containing the span (consider them grouped), we can set a fixed width displayed block, floating left and text aligned right so they all appear uniformly under one another. CSS is your friend:

label {
width:180px; /* Must match the exact width specified in the span */
display:block;
float:left;
text-align:right;
}

span {
display:block;
width:180px; /* Must match the exact width specified in the label */
text-align:right;
}

The input fields also have a fixed width and will appear uniformly under each other.

So to summarise at this stage, here's what we're looking at:

label_and_field.gif

The 'block' trick

If you know anything about floats you're probably already wondering how these are going to line up underneath each other without some sort of containing element. In the past I would have done this by placing each in it's own div, which is messy and unnecessary, or each in a p, which is semantically incorrect as these are not paragraphs we're dealing with.

The trick is to have a single containing element around the entire lot, and guess what? We've already got one. Place an ID in the form tag, id="my_form" I've used, and use CSS to set a fixed width only wide enough so that the containing elements wrap.

This will force each onto a new line giving the appearance that they are block level elements. Sneaky huh?

IE issues resolved

As is often the story, we have to make allowances for IE. In this case we've set a background colour on the legend, which when viewed in IE bleeds into the fieldset. This was fixed with a simple absolute positioning 'hack'.

IE also had a nasty habit of showing borders around the radio buttons and check boxes on hover. Again it was easily fixed but still something that didn't happen in any other browser (surprise surprise).

Compatibility

Testing went better than first expected. It works fine in Firefox 1.5, 2.0, 3.0.11, 3.5, IE6, 7 and 8, Safari, Opera and Chrome.

The download contains the complete demo with plenty of comments in the CSS to explain what's happening.

Yours, free to enjoy for any purpose you see fit.

accessible_form.zip (7.5k)

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Not a Member

Pete Harvy

I think to be totally accessible you should have used a higher contrast. But it looks fabulous and that's just a styling issue anyway. Stupendous effort Web Guy.

Wednesday 1st July 2009 | 08:40 PM Reply Comment URL Back to top

Not a Member

Paweł Krefta

There are little problems in Opera 9.62 on Mac, but they are easy to fix :)

Wednesday 1st July 2009 | 08:49 PM Reply Comment URL Back to top

CSS Babe

CSS Babe

I especially like how you put parts into separate sections. Thank you!

Thursday 2nd July 2009 | 05:55 AM Reply Comment URL Profile Back to top

That Web Guy

That Web Guy

Responding to this comment by Paweł Krefta

You'd think the browsers would work the same regardless of platform. Given the low complexity of the HTML I suspect the fix is probably something as simple as increasing the width of the containing form by a few pixels. I had to do that to make it work in IE6. I will see if I can get my paws on a Mac today at the office to confirm.

Thanks!

Thursday 2nd July 2009 | 05:58 AM Reply Comment URL Profile Back to top

Harun Smrkovic

Harun Smrkovic

Great! I really like the style and usability.

Hope you don't mind it if I use it somewhere :) :)

Thursday 2nd July 2009 | 03:58 PM Reply Comment URL Profile Back to top

Not a Member

Thesorrow

Is this possible to have a fluid container ?
Is this possible to add messagesm under or on the right side of the inputs for displaying error messages...

Thursday 2nd July 2009 | 04:49 PM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Harun Smrkovic

Go for it - "Yours, free to enjoy" I've added "for any purpose you see fit"

Thursday 2nd July 2009 | 05:02 PM Reply Comment URL Profile Back to top

Not a Member

Carerra Davis

I've never been a fan of the fieldset in forms only because aesthetically they don't usually look good. But you somehow make them look so good now I can't imagine not using them from now on. Great work web guy.

Friday 3rd July 2009 | 01:14 PM Reply Comment URL Back to top

Not a Member

Design Demi

That's the best looking form I've ever seen that doesn't have any graphical elements (except for the submit button). Thanks for letting us use it for anything we want.

Thursday 16th July 2009 | 08:40 AM Reply Comment URL Back to top

Not a Member

css freshman

very good work on the form.

Tuesday 29th September 2009 | 05:35 PM Reply Comment URL Back to top

Not a Member

Axcel

Stunning effort web guy. I'm going to use this for an upcoming project. Thanks~!

Tuesday 3rd November 2009 | 03:30 PM Reply Comment URL Back to top

CSS Babe

CSS Babe

I just tried to put this into practice now but the fonts are all massive in Internet Explorer 7. Yours looks fine but I can't see I've done anything different. Any guidance would be great.

Monday 21st December 2009 | 09:41 AM Reply Comment URL Profile Back to top

A Novice

A Novice

Don't worry I figured it out. IE7 is such a POS.

Thursday 31st December 2009 | 01:15 PM Reply Comment URL Profile Back to top

Not a Member

Maddi

Brilliant! Looks great and works perfectly.

Friday 14th May 2010 | 10:48 AM Reply Comment URL Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login