Sub topic 9
Writing useful alt text
This chapter assumes you can write an img element with a src attribute and that you know how attributes work. It also assumes you have met links, since alt text matters most when an image sits inside one. The goal is to decide, for any image, what the alt attribute should say, and when it should say nothing.
What alt text does
The alt attribute gives a text alternative for an image. Browsers and assistive technology use it in several situations:
- A screen reader reads it aloud in place of the image.
- A browser shows it when the image fails to load, for example on a broken URL or a slow connection.
- A search engine uses it to understand what the image shows.
- A text-only browser or a user who has turned off images sees it.
The alt attribute is required on img. An image with no alt attribute at all is invalid HTML, and screen readers often fall back to reading the file name, such as "IMG underscore 4032 dot jpg". That is worse than saying nothing.
The guiding question is: if the image were missing, what text would give the reader the same information or the same function?
Describe purpose, not appearance
The same photo can need different alt text on different pages, because the right text depends on why the image is there.
<!-- On a page about wildlife photography -->
<img src="images/red-fox.jpg" alt="A red fox standing in snow, ears raised, looking at the camera" width="640" height="427">
<!-- On a page about winter animal tracks -->
<img src="images/red-fox.jpg" alt="A red fox in snow, the animal that left the tracks described below" width="640" height="427">Ask what the image contributes to the surrounding content. Then write that. Do not start with "Image of" or "Picture of". Screen readers already announce that the element is an image, so the phrase is repeated noise.
Informative images
An informative image carries content that the text around it does not. Describe the content in one or two short sentences.
<img src="images/chart.png" alt="Bar chart showing sales growing each quarter of 2024" width="800" height="450">Good informative alt text is:
- Specific. "Bar chart showing sales growing each quarter of 2024" beats "chart".
- Concise. Aim for a short sentence. Many guides suggest staying under roughly 125 characters, though no browser enforces this. Some screen readers do not interrupt long text, so brevity helps.
- Free of repetition. If the nearby text already says what the image shows, do not say it twice.
For a complex image such as a detailed chart, put a short summary in alt and the full data nearby. A data table or a paragraph after the image works well.
Decorative images
A decorative image adds no information. Examples include a divider line, a background flourish, or an icon that sits beside text saying the same thing. Give it an empty alt attribute.
<img src="images/divider.png" alt="" width="600" height="4">An empty alt="" tells screen readers to skip the image. This is different from omitting alt, which is an error. Do not write alt=" " with a space, and do not write words like "decorative" or "spacer".
If an image is purely visual styling, CSS backgrounds are often a better fit than img. A CSS background has no alt text, so nothing needs to be skipped.
Functional images
A functional image does something. The usual case is an image inside a link or a button. Here the alt text should describe the action or destination, not the picture.
<a href="/">
<img src="images/logo.svg" alt="Example Co. home" width="120" height="40">
</a>If the logo's alt text were "A blue circle with a white letter E", a screen reader user would hear a link with that name and would not know where it goes. When an image is the only content of a link, its alt text becomes the link text. The same rule applies to a printer icon on a button: use alt="Print this page", not alt="Printer".
If a link contains both an image and visible text that says the same thing, make the image decorative to avoid reading the text twice:
<a href="/cart.html">
<img src="images/cart-icon.svg" alt="" width="24" height="24">
View cart
</a>Images of text
Avoid putting text in images when real text will do. When you must, such as a logo, the alt text should contain the same words that appear in the image. A banner reading "Spring sale: 20% off" needs alt="Spring sale: 20% off".
Alt text and captions
When you use figure and figcaption, the caption and the alt text do different jobs. The caption is visible to everyone and usually adds context. The alt text describes the image for people who cannot see it. Do not copy one into the other.
<figure>
<img src="images/chart.png" alt="Bar chart showing sales growing each quarter of 2024" width="800" height="450">
<figcaption>Quarterly sales, 2024</figcaption>
</figure>If the caption already describes the image fully, the alt text can be shorter, or empty if nothing else is needed. Use judgment: a reader who hears both should not hear the same sentence twice.
Decision guide
| Image type | What to write | Example |
|---|---|---|
| Informative | A short description of the content | `alt="Bar chart showing sales growing each quarter of 2024"` |
| Decorative | Empty alt | `alt=""` |
| Functional (link or button) | The action or destination | `alt="Example Co. home"` |
| Image of text | The same text | `alt="Spring sale: 20% off"` |
| Complex (chart, diagram) | A short summary, with full detail nearby | `alt="Line chart of monthly users, detailed below"` |
Common mistakes
- Omitting
alt. The image fails validation and screen readers may read the file name. Usealt=""if the image is decorative. - Keyword stuffing. Text like
alt="cheap shoes buy shoes best shoes online"harms screen reader users and does not help search ranking. - Starting with "image of". Screen readers already announce the role.
- Using the file name.
alt="IMG_4032.jpg"says nothing useful. - Describing a linked image instead of the link target. Write where the link goes.
- Repeating the caption word for word. Readers hear the same text twice.
- Using
titleas a substitute. Thetitleattribute is not reliably exposed to all users, especially on touch devices. Usealt.
Testing your alt text
You can check alt text without special tools:
- Turn off images in your browser, or temporarily break the
src, and read the page. Does it still make sense? - Open the page with a screen reader such as NVDA on Windows, VoiceOver on macOS, or TalkBack on Android, and listen to the images.
- Run a checker such as the browser's accessibility panel, Lighthouse, or axe. These catch missing
altattributes, but they cannot judge whether the text is good. Only a person can do that.
Exercise
A page has a small magnifying-glass image inside a search button, with no visible text. Another image is a wavy line that separates two sections. What alt text should each image have, and why?
Show answer
The magnifying glass is functional, so its alt text should name the action: alt="Search". The wavy line is decorative, so it should have alt="", which makes screen readers skip it. The first rule is to describe purpose, not the picture, and the second is to use an empty alt rather than omit the attribute.
Exercise
Rewrite this tag so that its alt text is useful: <img src="images/IMG_4032.jpg" alt="image of dog dog photo best dog pictures">. The photo shows a brown dog catching a ball on a beach, and it illustrates an article about training retrievers.
Show answer
<img src="images/IMG_4032.jpg" alt="A brown dog leaping to catch a ball on a beach" width="800" height="533">. The original starts with "image of" and repeats keywords. The new text describes the content briefly and fits the article's purpose. The width and height values are examples, so use the real dimensions of your file.
Exercise
An image of a company logo is the only content of a link to the home page. Which alt text is best: "Blue logo", "Example Co.", or "Example Co. home"? Explain.
Show answer
"Example Co. home" is best. Because the image is the whole link, its alt text becomes the link text, and it should say where the link goes. "Blue logo" describes appearance only. "Example Co." names the company but does not signal the destination.