5.4. Image Attributes

The <img> tag supports several attributes that can be used to help adjust how an image is rendered in a user agent. The basic XHTML attributes are described in the following sections.

Earlier versions of HTML supported additional attributes such as align, border, hspace, and vspace to help position the image. However, those attributes have been deprecated; to adjust the factors controlled by those attributes, you must now use their CSS equivalents.

5.4.1. Specifying Text for Nongraphical Browsers

As previously mentioned in this chapter, the alt attribute is used to provide text for nongraphical browsers. This text is generally displayed in place of the graphic in text-only browsers or in browsers that have images disabled. Alt-text is also used by nonvisual browsers—for example, audio browsers will speak the value of the alt attribute as it renders the page. Lastly, some user agents will use the alt tag's value as a tooltip or other textual hint, as shown in Figure 5-8.

Because of the utility of the attribute, you should endeavor to always include the alt tag with a descriptive value.

Resist the urge to embed extra information in an alt attribute value. Doing so will obscure the information from browsers that display the image and otherwise don't use the alt-text. Additionally, it will not give alternative user agents (nongraphical) the information they need to understand the purpose of the graphic that they cannot see.

If you have a lot of information ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.