b++ Label element secret powers
In web forms, label element plays an important role. It describes the corresponding form field, whether it’s a text field, radio button, checkbox or a drop-down. The label is an essential interface element for challenged people.
If labels are properly set, user agents attach a behavior: when a label is clicked with a mouse — in most browsers — the related field gains focus. The behavior is quite useful, since it gives you possibilities to increase hit area for a field.
Gaining field focus works much like tabbing with a keyboard, but there are exceptions. When a form button (for example Submit) gains focus via keyboard navigation, naturally it is selected and ready to be stroked by Enter. But, if there’s a label assigned to it, hitting the label will also submit the form automatically.
There is many possibilities. One of them is hiding form button with CSS and style label more freely, since it is a plain inline element. Design/CSS-wise, think of it like a span but with an extra behavior built in.
<label for="send" style="background-image: url(send-button.gif);"> <input type="submit" id="send" value="Send" style="position: absolute; left: -9999px;" /> </label>
You can nest fields inside labels and increase hit area, which many people do by default with checkboxes and radio buttons.
<label for="sure"> <input type="checkbox" value="1" id="sure" name="sure" /> Are you sure? </label>
Another interesting application is adding multiple labels for the same field — for instance, one right next to checkbox, and another one in a supporting paragraph where you can contextually anchor keywords directly to that checkbox.
<p>If you are absolutely sure, then <label for="sure">confirm it</label> in the form.</p> <label for="sure"> <input type="checkbox" value="1" id="sure" name="sure" /> Are you sure? </label>
Last, but not least, labels provide valuable connection between two elements, which gives you opportunity for a unique interface designs.
001—2008.09.15.00:45
Nice post — I didn’t know about the submit button trick. Now it makes sense when I think about it.
How do you feel about a nested INPUT inside a label, but when a label doesn’t have the “for” attribute? According to the spec, it should still associate the label with the input, but I’ve heard some user agents fail to comply.
002—2008.09.15.11:43
Hey, you should note that in some screen readers the input-element becomes invisible if nested within a label-element. Mike Cherim also mention some other problem:
http://green-beast.com/blog/?p=254
003—2008.09.15.13:12
Hah! Today I was checking my RSS feed in reversed order and the last one is from CSS Globe and there I saw a headline
” Label element secret powers “. I went to see what’s there and Bang! I landed here. Of course I have Maratz in my feed too, but somewhere in the middle so I checked his blog and saw the same headline. Now I know where it comes from, thanks for the article! I’ve learned something again :) .
004—2008.09.15.16:14
Nils, thank you very much for the useful link! Good to know.
Mislav, Safari 2 doesn’t support label to field assotiation, so one have to reach out for the secrets of JavaScript if this behavior is required.
Luckily, Tiger OS X users are able to upgrade to Safari 3, so only users w/ Safari 1.3 are left behind. Pretty tiny pie-chart slice.
005—2008.09.16.02:58
Thanks for the post - how well supported is the label submits form behaviour?