19 Aug 2007

The power of labels

Tags:

I’m impressed by how many developers still haven’t learnt to use labels in their pages. I mean, how hard can it be?

What are labels anyway, I hear you ask. Label is an HTML element, which describes an input field. Let me demonstrate:
<label for="uname">Username:</label> <input id="uname" type="text" />
Here I establish two elements. An input field, which doesn’t say much on its own and a label, which tells us what this input is for. The for attribute of the label matches the id of an input field. One can also put the input directly into the label…
<label>Username: <input type="text" /></label>
Which I prefer, due to the fact it’s shorter. Labels can be assigned to just about any form field. They are “text level” elements and can be styled with css.

So why should you use labels? There are various reasons, which you probably don’t really care about, like semantic coding, browsers being able to visually match the label and the element (although this is your job) and offering help to people using screen readers to help them see. But here’s one that should change your mind - Allowing users to click on the label to focus the input field. As an example, take all those “I agree to …” checkboxes. Isn’t it lovely how you can just click the text in order to check that checkbox? This works in all modern browsers that I know of. Mind you, that I don’t consider Internet Explorer 6 to be a modern browser, though.

To all those who still have no clue what I’m on about, try this demo.

4 Responses to “The power of labels”

  1. Tomaz / Aug 19, 2007 1:56 am

    Thank u for remindimg me about this trick! Great demo btw.

  2. Iztok / Aug 19, 2007 11:31 am

    More of this:
    http://www.sitepoint.com/article/fancy-form-design-css

  3. blog.tomazkovacic.com :: Nekaj svežega na blogroli / Sep 13, 2007 3:18 pm

    […] v angleš?ini, sem prisural po ?istem naklju?ju in me je pritegnil z zelo zanimivem zapisom The power of labels, kjer sem se lahko podu?il o zelo uporabni malenkosti pri gradnji spletnih predlog. Od takrat […]

  4. Dušan Smolnikar ~ blog • Definition lists - do you use them? / Oct 2, 2007 11:11 pm

    […] demonstration purposes I left out label tags, don’t try this at […]

Leave a Reply