HTML
HTML introduction
HTML basics
HTML text format 1
HTML text format 2
HTML text format 3
HTML links
HTML images
HTML image maps
HTML forms
Fieldset/legend tags
Email links
HTML labels
HTML tables
HTML frames
HTML backgrounds
HTML colors
HTML color shades
HTML color usage
HTML marquees
HTML fonts
HTML entities
HTML stylesheets
HTML layout
HTML div/span
HTML audio
HTML video
HTML objects
HTML d'load media
HTML meta tags
HTML scripts
HTML declarations
HTML head section
HTML document types
HTML tag rules
HTML things to avoid
URL formatting
Encoding and decoding
HTML use/access
HTML publish work
History of HTML
HTML summary

Programming

Programming intro
Java

Markup

First webpage guide
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

HTML <label> tag

Accessibility is a big thing in the world wide web. Fortunately, HTML is designed a certain way (and continues to be as it changes over time) that provides for a high level of accessibility.

This lesson focuses on:

The <label> tag

The <label> tag is a special tag that sets the text of form elements.

The <label> tag should sorround the text that will appear for a form element. The form element for which the text is specified should be set using the for attribute which takes the same value as the value set for the id attribute of that particular form element.

Example:

<form> <label for="fname">First Name:</label> <input type="text" name="fname" id="fname" /> </form>

Output:

In the above example, we set the for attribute of the <label> tag with the value "fname". This is the id of the form element for which the text in the <label> tag is specified. The textbox with the id "fname" will be labeled with the text "First Name:"

Another way to achieve the same effect is like this:

Choose a browser: <form> <label for="ff"> <input type="radio" name="browser" id="ff" /> Firefox </label> <label for="ie"> <input type="radio" name="browser" id="ie" /> Internet Explorer </label> </form>

Output:

Choose a browser:

The difference in this example from the first one is that in this example we actually include the form element within the <label> tag.

What is the purpose of the <label> tag?

So then what is the purpose of the <label> tag? Can't we just include some text for a form element next to it?

Technically, we can. But using the <label> tag puts things into a clearer context and is a good usability practice which has some benefits to it.

Benefits of using the <label> tag:

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments