Sub topic 9
Writing useful alt text
This chapter assumes you can write a basic img element, understand src paths, and know what an attribute is. Here you learn what to put in the alt attribute and what to leave out.
What alt text does
The alt attribute holds a text replacement for an image. Screen readers read it aloud. Browsers show it when the file fails to load. Search engines use it to understand the image.
Good alt text gives the reader the same information the image gives a sighted visitor. It does not describe the picture for its own sake. The right text depends on why the image is on the page.
<img src="images/red-fox.jpg" alt="A red fox standing in snow" width="640" height="427">Decide by purpose
Ask what the image is for, then write text that serves that purpose.
| Image role | What to write | Example alt |
|---|---|---|
| Informative photo | Describe the relevant content | `A red fox standing in snow` |
| Chart or graph | State the main finding | `Bar chart showing sales growing each quarter of 2024` |
| Image inside a link | Describe where the link goes | `Example Co. home` |
| Decorative | Use an empty value | `alt=""` |
Decorative images
A decorative image adds no information, such as a divider line. Give it an empty alt attribute so screen readers skip it.
<img src="images/divider.png" alt="" width="600" height="4">Do not leave the attribute out. Without alt, some screen readers read the file name aloud, such as "divider dot png". An empty alt="" is a deliberate statement that the image can be ignored.
Writing good text
- Be specific and brief. One short sentence or phrase is usually enough.
- Skip phrases like "image of" or "picture of". Screen readers already announce that it is an image.
- Include text that appears in the image. A banner reading "Sale ends Friday" needs that text in the alt.
- Describe what matters in context. A fox photo in a wildlife article might need "A red fox hunting in deep snow". In a page about winter fur colors, "A red fox with a thick winter coat" fits better.
- Do not repeat nearby text. If a caption already says everything, keep the alt short or empty so the content is not read twice.
Common errors
- Missing
alt. The image has no text alternative, and validators flag it. - File names as alt.
alt="IMG_4032.jpg"tells the reader nothing. - Keyword stuffing.
alt="fox red fox buy fox photos"harms accessibility and gains nothing. - Describing the image in a link. For a logo link,
alt="Blue logo with a circle"fails. Usealt="Example Co. home".
Exercise
A page shows a thin decorative wave between two sections, and a product photo of a green ceramic mug. Write the img elements' alt attributes for both.
Show answer
The wave uses an empty value, alt="", because it adds no information. The mug needs a short description of what matters, for example alt="Green ceramic mug with a wide handle".
Exercise
Why is alt="Image of a company logo" a poor choice for a logo that links to the home page?
Show answer
It begins with "Image of", which screen readers already announce, and it describes the picture instead of the link's destination. Better: alt="Example Co. home".