HTML provides two main ways to add descriptive text to page elements: the
alt attribute, and the
title attribute. While these two attributes may appear to work in a similar way, they have quite different purposes. This article takes a look at these two attributes, explores their similarities and differences, and shows how to use both attributes correctly.
<element alt="text" ... />
alt attribute to supply an alternative description of the element. The text you use for the
alt attribute is shown to a visitor when their browser doesn’t display the element itself. Often it’s shown in the space where an image would normally appear.
Here’s how Opera 9 displays the
alt text of an image when the image itself can’t be shown:
alttext for a missing image
alt attribute can only be used with the following HTML elements:
- Specifies an alternative description of an image to be shown when the image is not displayed.
- Specifies an alternative description of an area of an image map to be shown when the image is not displayed.
- Specifies an alternative description of a form control to be shown when the control is not displayed. Practically speaking, this is nearly always used to describe the image used in an
<input type="image" ... / >form button. However some browsers might use it for other controls – for example, when the browser can’t render a checkbox or a radio button.
- Specifies an alternative description of an applet to be shown when the applet is not displayed.
Of the above elements, the
alt attribute is required for
area, and optional for the other two. In other words, if you don’t include an
alt attribute in an
area tag, it’s invalid HTML.
So when might an image not be displayed in a browser? Here are some common scenarios:
- the image couldn’t be found
- the visitor has turned off image loading in their browser
- they’re using a text-only browser (such as Lynx), or
- they’re using a screen reader for the visually impaired (such as JAWS or Window-Eyes).
alt attribute to provide a short, accurate description of the element. The
alt text should adequately describe the element, and it should make sense to a visitor in the situation where the element isn’t displayed. Don’t use
alt text to provide additional information about the element; this is what the
title attribute (described below) is for.
alt attribute is required for
area, this doesn’t mean that the value of the attribute can’t be empty. In fact, a lot of the time, it should be empty — for example, if the image is purely decorative, and doesn’t need to be described to anyone who can’t see it.
Some examples of good
"Photo of a purple computer mouse"
"'Clouds and rain' icon"
"Buy a WonderWidget today!"
Some examples of bad
"Mouse required to play this game"
"Forecast for Wednesday"
"A blue bullet point"
<element title="text" ... />
title attribute to provide additional information about the element. The text you use for the
title attribute is shown to a visitor as well as the element itself. Most browsers display
title text in a yellow tool tip that appears when you hover your mouse over the element. Some, though, display the
title text in the status bar.
The screenshot below shows how Opera 9 renders
title text for an
titletext for an image
title attribute can be used with all elements except the following:
title attribute is always optional; no HTML element requires that you include a
Some good uses of
title text in any situation where it aids the understanding and usability of your page. Classic examples include:
- Supplying additional info about an image. You can use
titletext to describe an image in more detail. For example, you might mention where a photo was sourced from, the date and place it was taken, and the name of the photographer.
- Adding descriptive text to a link. If the link text (the text between the
</a>tags) is not very descriptive, add a
titleattribute to the
<a>tag to supply more detailed info. Here’s an example:
titlespecification. (Try hovering over the link to see the
- Explaining a form field. If the meaning of a form field is ambiguous, you can clarify its meaning by adding a
titleattribute to the field’s associated
labelelement. For example, a field label that reads “Qty” might have a
titleattribute with the value “Please enter the number of WonderWidgets you want to purchase”.
- Use the HTML
altattribute to provide alternative text for an element, to be shown when the element can’t be displayed.
- Use the
titleattribute to supply additional text to describe the element in more detail when necessary.
If you need more info, try the descriptions of the
title attributes in the official W3C HTML 4 specification. There’s also a great discussion of when to use
title over at the 456 Berea Street blog. Enjoy!