Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Label elements

Peter Van Djick asks why does hardly anyone use LABEL tags? It’s a very good question—in my opinion label tags, like title attributes on links, are a complete no-brainer. They’re well supported by all modern browsers, completely backwards compatible (in that there are no ill effects for older browsers), great for accessibility and easy to implement. They’re much more than just an accessibility issue—the usability of a form is dramatically increased by the addition of label tags, especially for radio and check boxes where they greatly increase the “target area” for the user to click on.

In case you haven’t come across them before, here’s how they work:


<label for="myFormElement">Your name:</label>
<input name="yourName" type="text" id="myFormElement" />

The for attribute on the label links it to the form element with that ID. When the user clicks on the label, their cursor will jump to the form field. If the targetted form element is a check box or radio box clicking the label text will toggle it. Labels can by styled using CSS just like any other element—I often use display: block to stack them above their corresponding form elements to save me from having to mess around with the layout of a form, as can be seen on my Contact Page.

If you aren’t using label elements, now’s the time to start.

This is Label elements by Simon Willison, posted on 11th February 2003.

View blog reactions

Next: Indexing hypertext

Previous: Validity would be nice

9 comments

  1. Quite agree... they have been used in stand-alone software for years, and as you say, they are well supported in browsers now... it makes life so much easier to be able to click the whole item, rather than having to go directly to the often pretty small element.

    Andrew - 12th February 2003 00:22 - #

  2. http://diveintoaccessibility.org/day_28_labeling_f orm_elements.html

    Mark - 12th February 2003 02:43 - #

  3. Why doesn't anybody use them? Well there's a nasty bug in Netscape 7.0 that hides them completely.

    Jim - 12th February 2003 03:31 - #

  4. I thought the Netscape 7 bug only hid them if they were floated?

    Simon Willison - 12th February 2003 10:06 - #

  5. Going along with the previous post, youngpup created a neat Labels-related inobtrusive DHTML script:

    http://youngpup.net/?request=/articles/labels.xml

    "It first hides all label elements, and uses their for attribute to populate the related textboxes with labels. Then, it attaches event listeners to the fields to control the hiding and showing of text. It even handles password input boxes."

    Anyway, it's just one of the many possible things you can do with labels, and also with DHTML.

    Stanley Sy - 12th February 2003 18:26 - #

  6. It amazes me too. The number of times that I've said to people about the <label> tag and got blank stares in return is shocking.

    I tried to make it a little easier for people by putting together a web-based tool to generate forms that automatically include the label tag, which you can find here:

    http://www.accessify.com/tools-and-wizards/accessi ble-form-builder.asp

    It's still a bit basic - expect a better version soon though ...

    Ian - 13th February 2003 17:09 - #

  7. Hiya, I have just noticed the link to the article by Peter Van Djic mentioned above is broken; the actual link is now located here: http://poorbuthappy.com/ease/archives/002226.html

    Nat - 13th April 2004 13:14 - #

  8. dfs

    dfs - 14th July 2004 21:18 - #

  9. Couldn't agree more, such a no-brainer has such huge benefits, but the problem is, until everyone uses it, noone will use it... I still find myself clicking checkbox titles in the HOPE the designer has used them...

    Tom Smith - 5th January 2005 20:51 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/02/11/labelElements

A django site