Definition and Usage
The alt attribute of the <img> tag, which represents an image, specifies alternative text for the image. It is optional.
It is very useful for web accessibility because it allows screen reader users with visual impairments to understand content through alternative text in place of images.
- The value of this attribute is not displayed directly on the screen.
- However, some browsers (such as Chrome) display the value of this attribute as text when an image cannot be displayed for any reason, such as a network error or blocked content.
Basic Example
<img src="earth.jpg" alt="A round, blue Earth glowing in space">
Correct Usage
Learn how to use the alt attribute correctly to provide text as an alternative to images for web accessibility.
The HTML specification sets out various requirements for using the alt attribute of images.
These requirements are intended to ensure the accessibility of web pages and effectively convey image content to a wide range of users, especially users of assistive technologies, including people with visual impairments. Here, we will explain the requirements for the alt attribute in detail based on the specification.
Please note that most of the explanations and related examples are based on the HTML specification.
General Guidelines
Providing alternative text for images is a key factor in improving web accessibility. It is necessary to write appropriate and clear alternative text so that users can understand images and fully access the page's content. The value of the alt attribute should focus on describing the image in a way that helps users understand its purpose and meaning.
Writing Alternative Text
- Replacing all images with the text of their
altattributes must not change the meaning of the page. - Alternative text must never include text that could be considered a caption, title, or legend for an image. It should provide alternative text that users can use in place of the image, not text intended to supplement the image. The
titleattribute can be used to provide supplementary information about an image.
A good way to write alternative text is to think about how you would read the page to someone over the phone, without mentioning that an image is present. Whatever you say instead of the image is typically a good starting point for writing alternative text.
Providing Alternative Text for Links or Buttons Containing Only Images
Example 1
For example, when users choose their preferred color, each image provides the color name as alternative text so that users who do not see the images can still understand their purpose. In this color selection example, the name of each color is provided in the alt attribute of its image to help users select a color.
<h1>Choose a Color</h1>
<ul>
<li><a href="green.html"><img src="green.jpeg" alt="Green"></a></li>
<li><a href="blue.html"><img src="blue.jpeg" alt="Blue"></a></li>
<li><a href="red.html"><img src="red.jpeg" alt="Red"></a></li>
</ul>
Example 2
In the button examples, the images represent RGB or CMYK color profiles. Since each image represents one part of the text, the buttons can be written as follows:
<button name="rgb">
<img src="red" alt="R">
<img src="green" alt="G">
<img src="blue" alt="B">
</button>
<button name="cmyk">
<img src="cyan" alt="C">
<img src="magenta" alt="M">
<img src="yellow" alt="Y">
<img src="black" alt="K">
</button>
Of course, if the images in each example are combined into a single image, the alternative text can be provided as follows:
<button name="rgb">
<img src="rgb" alt="RGB">
</button>
<button name="cmyk">
<img src="cmyk" alt="CMYK">
</button>
Providing Alternative Text for Graphs, Diagrams, Charts, Maps, and Other Visuals
- Some things can be expressed more clearly in graphical form, such as flowcharts, diagrams, graphs, and simple maps showing directions. In such cases, an image can be provided using the
<img>tag, but the same message must also be conveyed in text for users who cannot view the image. - To do this, text must be provided in the
altattribute, and this text must convey the same message as the image specified in thesrcattribute.
Example
In this example, a flowchart is included as an image, and the alt attribute provides text describing the flowchart so that users who cannot view the image can still understand the message.
<p>
In the common case,
the data handled by the tokenization stage comes from the network,
but it can also come from script.
</p>
<p>
<img
src="images/parsing-model-overview.svg"
alt="The Network passes data to the Input Stream Preprocessor,
which passes it to the Tokenizer, which passes it to the
Tree Construction stage. From there, data goes to both
the DOM and to Script Execution. Script Execution is
linked to the DOM, and, using document.write(), passes
data to the Tokenizer.">
</p>
Correct and Incorrect Approaches
Here are examples showing good and bad ways to provide alternative text for an image included in a description.
<!-- Correct example -->
<p>
You are standing in an open field west of a house.
<img src="house.jpeg" alt="The house is white, with a boarded front door.">
There is a small mailbox here.
</p>
<!-- Incorrect example -->
<p>
You are standing in an open field west of a house.
<img src="house.jpeg" alt="house.jpeg">
There is a small mailbox here.
</p>
In the incorrect example, alternative text is provided to describe the image, but when the image is not displayed, the text does not flow naturally.
Text that replaces a graphical element should help users who cannot see the image understand the content.
The alt attribute serves as a replacement for the image and should focus on conveying its message, not describing the image itself.
Providing Alternative Text for Icons and Logos
A web page can contain information in iconic form. Icons are intended to help users of visual browsers recognize features at a glance.
When Icons Are Used Alongside Text That Clearly Conveys Their Meaning
When an icon has no text label, it must be self-explanatory. In this case, a text label that can serve as an alternative to the icon must be provided in the alt attribute. However, in some cases, an icon is used alongside text that clearly conveys its meaning. In such cases, the alt attribute must be present but must be empty.
alt attributes because they appear next to text that conveys the same meaning.
<nav>
<p>
<a href="/help/"><img src="/icons/help.png" alt=""> Help</a>
</p>
<p>
<a href="/configure/"><img src="/icons/configuration.png" alt=""> Configuration Tools</a>
</p>
</nav>
When a Logo Represents an Entity, Such as in a Page Heading
When a logo is used to represent an entity, such as in a page heading, the alt attribute must contain the name of that entity. It must not include words such as "logo".
<h1><img src="XYZ.gif" alt="The XYZ Company Logo"></h1>
<h1><img src="XYZ.gif" alt="The XYZ Company"></h1>
When a Logo Is Used Decoratively or Is the Subject of Discussion
When a logo is used as a decorative element or is the subject of discussion, alternative text must be provided accordingly.
<p>Consider their logo:</p>
<p><img src="/images/logo" alt="It consists of a green circle with a green question mark centered inside it."></p>
Icons, logos, and similar elements are intended to help users of visual browsers recognize features at a glance. Therefore, the information conveyed by these images must be provided in text through the alt attribute so that users who cannot access the visual content can understand it as well.
When Text Is Rendered as an Image for Visual Effect
Sometimes an image consists entirely of text, and the text is rendered as an image to achieve a visual effect. In such cases, the alt attribute must be present, but its value must contain the same text as the image itself.
Example
Suppose you want to use the text "Earth Day" as a heading. Instead of using actual text, you create a graphic that displays the words "Earth Day" in a decorative style, with flowers and plants adorning the letters. If you use this image as a heading in place of text, the alt attribute must contain only the text "Earth Day".
<h1><img src="earthdayheading.png" alt="Earth Day"></h1>
When text is rendered as an image for visual effect, the alternative text must convey the text itself, not the visual effects. This is important to ensure that users who cannot access the visual content can understand it as well.
When an Image Supplements the Surrounding Text
If an image serves only to supplement the surrounding text rather than being the main content, the alt attribute must be present, but its value must be an empty string (""). Removing such an image does not make the page any less useful, but including it can greatly help users who can view images visually understand the concept.
Example 1
The following example shows a flowchart that repeats the textual content of the preceding paragraph in graphical form. In this case, the alt attribute is set to an empty string ("").
<p>
The Network passes data to the Input Stream Preprocessor,
which passes it to the Tokenizer, which passes it to the Tree Construction
stage. From there, data goes to both the DOM and to Script Execution.
Script Execution is linked to the DOM, and, using document.write(),
passes data to the Tokenizer.
</p>
<p><img src="images/parsing-model-overview.svg" alt=""></p>
Example 2 (Including a Caption)
The alt attribute must not contain the image's caption. To include a caption, you can use the title attribute or the <figure> and <figcaption> tags.
- You can use the
titleattribute to provide a caption. - When using the
<figure>and<figcaption>tags, thealtattribute must contain text that replaces the image, not its caption.
The following examples show correct and incorrect approaches.
<!-- Using the title attribute -->
<p>
The Network passes data to the Input Stream Preprocessor,
which passes it to the Tokenizer, which passes it to the Tree Construction
stage. From there, data goes to both the DOM and to Script Execution.
Script Execution is linked to the DOM, and, using document.write(),
passes data to the Tokenizer.
</p>
<!-- If an image serves only to supplement the surrounding text rather than being the main content,
the alt attribute must be present, but its value must be an empty string (""). -->
<p>
<img
src="images/parsing-model-overview.svg"
alt=""
title="Flowchart representation of the parsing model.">
</p>
<!-- Using the figure and figcaption tags -->
<p>
The Network passes data to the Input Stream Preprocessor,
which passes it to the Tokenizer, which passes it to the Tree Construction
stage. From there, data goes to both the DOM and to Script Execution.
Script Execution is linked to the DOM, and, using document.write(),
passes data to the Tokenizer.
</p>
<figure>
<img
src="images/parsing-model-overview.svg"
alt="The Network leads to
the Input Stream Preprocessor, which leads to the Tokenizer, which
leads to the Tree Construction stage. The Tree Construction stage
leads to two items. The first is Script Execution, which leads via
document.write() back to the Tokenizer. The second item from which
Tree Construction leads is the DOM. The DOM is related to the Script
Execution.">
<figcaption>Flowchart representation of the parsing model.</figcaption>
</figure>
alt Attribute
<!-- Using the title attribute -->
<p>
The Network passes data to the Input Stream Preprocessor,
which passes it to the Tokenizer, which passes it to the Tree Construction
stage. From there, data goes to both the DOM and to Script Execution.
Script Execution is linked to the DOM, and, using document.write(),
passes data to the Tokenizer.
</p>
<p>
<img
src="images/parsing-model-overview.svg"
alt="Flowchart representation of the parsing model.">
</p>
Ancillary Images
Ancillary images are not essential to the content, but they are neither purely decorative nor entirely redundant with the text. In these cases, the alt attribute must be present, and its value must be either an empty string or a textual representation of the information conveyed by the image. If an ancillary image has a caption, the alt attribute must not have an empty value. This is because users with visual impairments who use screen readers may miss contextual information conveyed by the image.
Image Shown Without Context
When an image is shown without context, alternative text does not need to be provided.
<p>
<img src="president.jpeg" alt="">
Ahead of today's referendum,
the President wrote an open letter to all registered voters.
She acknowledged that the country was divided.
</p>
In this case, if the photo shows only a face, details such as hair color, skin color, and the number of eyes may not be important to the reader.
Describing the Mood Conveyed by an Image
If an image conveys a politician's emotions, alternative text can be useful in setting the tone of the article.
<p>
<img src="president.jpeg" alt="The President looks sad.">
Ahead of today's referendum,
the President wrote an open letter to all registered voters.
She acknowledged that the country was divided.
</p>
<p>
<img src="president.jpeg" alt="The President looks happy!">
Ahead of today's referendum,
the President wrote an open letter to all registered voters.
She acknowledged that the country was divided.
</p>
Here, whether the President "looks sad" or "looks happy" affects how readers interpret the text.
When an Image Has a Caption
When an image has a caption, alternative text must be included so that users who cannot see the image can understand what the caption refers to.
<p>
Ahead of today's referendum,
the President wrote an open letter to all registered voters.
She acknowledged that the country was divided.
</p>
<figure>
<img
src="president.jpeg"
alt="A high forehead, a cheerful expression,
and dark hair complete the President's face.">
<figcaption>The President of Ruritania. Photo © 2014 PolitiPhoto.</figcaption>
</figure>
When an ancillary image is not essential to the content but conveys information, the alt attribute must be used. If the image conveys important information on its own, alternative text must be provided. When an image has a caption, alternative text must be included to avoid confusion.
A Purely Decorative Image
A purely decorative image is an image that does not serve an informational purpose and is not directly related to specific content on the page. Such images should preferably be specified in CSS as part of the site's overall design.
However, a decorative image that is still meaningful as part of the content can be included in the page using the <img> tag. In these cases, the alt attribute must be present, and its value must be an empty string ("").
Example
The following examples show how decorative but relevant images can be used. For example, a blog post about an urban event might include a photo of the cityscape, or a page reciting a poem might include an illustration inspired by the poem.
The following example illustrates the latter case, with the first verse of the poem "The Lady of Shalott" and a related decorative image.
<h1>The Lady of Shalott</h1>
<p><img src="shalott.jpeg" alt=""></p>
<p>
On either side the river lie
<br>
Long fields of barley and of rye,
<br>
That clothe the wold and meet the sky;
<br>
And through the field the road run by
<br>
To many-tower'd Camelot;
<br>
And up and down the people go,
<br>
Gazing where the lilies blow
<br>
Round an island there below,
<br>
The island of Shalott.
</p>
A purely decorative image should be specified in CSS when it does not serve an informational purpose and is not directly related to specific content on the page. However, a decorative image that has some relevance to the content can be embedded using the <img> tag. In this case, the alt attribute must be set to an empty string ("").
When an Image Is a Key Part of the Content
In some cases, an image is a key part of the content. For example, on a photo gallery page, the images themselves are the main content of the page. When an image is a key part of the content, how to provide alternative text for it depends on the image's source.
When detailed alternative text can be provided, such as for a screenshot in a magazine review, part of a comic, or a photograph included in a photo blog, text that can replace the image must be provided in the alt attribute.
Example 1
Screenshot and Alternative Text in a Screenshot Gallery for a New Operating System
<figure>
<img
src="KDE%20Light%20desktop.png"
alt="The desktop is blue, with icons along the left hand side in
two columns, reading System, Home, K-Mail, etc. A window is
open showing that menus wrap to a second line if they
cannot fit in the window. The window has a list of icons
along the top, with an address bar below it, a list of
icons for tabs along the left edge, a status bar on the
bottom, and two panes in the middle. The desktop has a bar
at the bottom of the screen with a few buttons, a pager, a
list of open applications, and a clock.">
<figcaption>Screenshot of a KDE desktop.</figcaption>
</figure>
Example 2
A Graph in a Financial Report
<img src="sales.gif"
title="Sales graph"
alt="From 1998 to 2005, annual sales growth was as follows:
624%, 75%, 138%, 40%, 35%, 9%, 21%">
In this case, "Sales graph" is not an appropriate alternative text. Alternative text should provide a detailed description that can replace the image.
For an image that is a key part of the content, appropriate alternative text should be provided based on the image's source. Detailed alternative text is provided in the alt attribute and should contain enough information to replace the image.
Images That Defy a Complete Description
In some cases, providing detailed alternative text for an image is impractical. Examples include indistinct images, complex fractals (geometric patterns in which some small parts resemble the whole), and detailed topographical maps. In these cases, the alt attribute must still contain suitable alternative text, but it may be somewhat brief.
Example
Sometimes there is simply no text that can adequately describe an image. For example, there is little that can be said to usefully describe a Rorschach test inkblot. However, even a brief description is better than nothing.
<figure>
<img
src="/commons/a/a7/Rorschach1.jpg"
alt="A shape with left-right symmetry and indistinct edges,
with a small gap in the center and two larger gaps
slightly offset from the center. The top half is wider
than the bottom half, the sides extend higher than the
center, and the center extends below the sides.">
<figcaption>A black outline of the first card in the Rorschach inkblot test.</figcaption>
</figure>
The following is a very bad example of alternative text.
<figure>
<img
src="/commons/a/a7/Rorschach1.jpg"
alt="A black outline of the first card in the Rorschach inkblot test.">
<figcaption>A black outline of the first card in the Rorschach inkblot test.</figcaption>
</figure>
Including the caption in the alternative text like this is not useful because it effectively makes users who rely on screen readers hear the caption twice. Instead, it is more useful to provide the visual information conveyed by the image directly.
For images that defy a complete description, provide as much detail as possible in the alternative text, but it may be brief. The important thing is to provide useful information by avoiding duplication and describing the visual information conveyed by the image directly.
Images Whose Contents Are Not Known
In some cases, it may be impossible to provide any alternative text for an image. This may happen because the image is collected automatically (e.g., from a webcam), the page is generated using user-provided images that have no suitable alternative text, or the author does not know what the image represents (e.g., a blind person sharing a photo on their blog). In these cases, the alt attribute may be omitted, but one of the following conditions must be met.
- The
<img>tag is inside a<figure>tag that contains a<figcaption>tag with content other than whitespace, and the<figure>tag contains no other content apart from whitespace, excluding the<figcaption>tag and its descendants. - The
titleattribute is present and has a non-empty value.
Relying on the title attribute is currently discouraged because many browsers do not expose it in an accessible way (e.g., displaying a tooltip only when using a pointing device such as a mouse, which is inaccessible to keyboard and touchscreen users).
Such cases should be kept to an absolute minimum. If there is even the slightest possibility that the author can provide alternative text, omitting the alt attribute is not acceptable.
Example
Photo-Sharing Site
<figure>
<img src="1100670787_6a7c664aef.jpg">
<figcaption>Bubbles traveled everywhere with us.</figcaption>
</figure>
A Blind Person's Photo Blog
<article>
<h1>I Took a Photo</h1>
<p>I went out and took a photo today!</p>
<figure>
<img src="photo2.jpeg">
<figcaption>This is a photo taken in the front yard.</figcaption>
</figure>
</article>
CAPTCHA Image
<p>
<label>What does this image say?
<img src="captcha.cgi?id=8934" title="CAPTCHA">
<input type="text" name="captcha">
</label>
(If you cannot see the image, you can use an <a href="?audio">audio</a> test instead.)
</p>
The alt attribute may be omitted only when alternative text cannot be provided. The author's lack of effort is not a valid reason to omit the alt attribute.
Images Not Intended for the User
Generally, authors should avoid using the <img> tag for purposes other than displaying images.
If the <img> tag is used for purposes other than displaying an image (e.g., as part of a service that counts page views), the alt attribute must be an empty string (""). In such cases, both the width and height attributes should be set to 0.
Example (An <img> Tag Used to Count Page Views)
<img src="tracker.png" alt="" width="0" height="0">
The example above uses an image element to track page views. Its dimensions are set to 0 so that users cannot see it, and its alternative text is set to an empty string ("").
Images in Emails or Private Documents Intended for a Specific Person Who Is Known to Be Able to View Images
This section does not apply to publicly accessible documents or documents intended for an audience whose members are not necessarily personally known to the author, such as documents on a website, emails sent to public mailing lists, or software documentation.
When an image is included in a private communication (such as an HTML email) intended for a specific person who is known to be able to view images, the alt attribute may be omitted. However, even in such cases, authors are strongly encouraged to include appropriate alternative text based on the type of image, in case the user uses a mail client that does not support images or the document is forwarded to other users who may not be able to see images easily.
Example (A Private Email Sent to a Specific Person Who Can View Images)
<p>Check out this photo!</p>
<img src="vacation_photo.jpg">
The example above shows an email sent to a specific recipient who is known to be able to view images, in which the alt attribute is omitted. However, including alternative text is recommended for better accessibility.
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
alt
|
1 | 12 | 1 | 1 |