Sub topic 15
Writing alt text
This chapter assumes you know the img element and its attributes, and that you have already chosen and cropped your photos. Here you write the text that stands in for the photo when it can't be seen.
What alt text does
The alt attribute holds a text replacement for an image. Screen readers read it aloud. The browser shows it if the file fails to load. Search engines also use it to understand the image.
Good alt text answers one question: what does a person who can't see this photo need to know to understand the page?
The answer depends on the photo's purpose, which you settled earlier in this tutorial. The same photo can need different alt text on different pages.
Describe the role, not the pixels
Start from why the photo is on the page. Then write the part of the image that serves that purpose.
| Photo | Weak alt | Better alt |
|---|---|---|
| Hero on a bakery page | `photo` | `A baker lifting a loaf from a stone oven` |
| Team page portrait | `IMG_2041.jpg` | `Priya Nair, head of design` |
| Product shot | `Image of a red kettle` | `Red enamel kettle with a wooden handle` |
Screen readers already announce "image", so leave out "image of" and "picture of". Keep the text short. One sentence is usually enough, and about 125 characters is a common upper guide.
Decorative images get empty alt
Some photos add mood but no information. A background texture is one example. A photo that repeats the text beside it is another. Give these an empty alt so screen readers skip them.
<img src="texture.jpg" width="1200" height="400" alt="">Do not omit the attribute. With no alt at all, many screen readers read out the file name instead. An empty alt="" is a deliberate statement that the image is decorative.
Images that carry text or links
If a photo contains text that matters, put that text in the alt. If the image is inside a link, describe where the link goes, not what the picture shows.
<a href="/menu">
<img src="menu-cover.jpg" width="600" height="400" alt="See the full menu">
</a>If a chart or complex photo needs a long explanation, give a short alt and put the full description in nearby visible text.
Alt text and cropping
Your crop changes what the image shows. If the mobile crop in a picture element removes part of the scene, check that the alt still matches what is visible. The alt goes on the img element, and all source crops share it. Write text that is true for every crop.
<picture>
<source media="(min-width: 800px)" srcset="hero-wide.jpg">
<img src="hero-square.jpg" width="800" height="800" alt="A baker lifting a loaf from a stone oven">
</picture>Here the baker and the loaf stay in frame in both crops, so one description works.
Common errors
- Starting with "image of" or "photo of".
- Using file names such as
IMG_2041.jpg. - Stuffing keywords for search.
- Leaving out
altentirely on decorative images. - Writing the same text as the caption next to the photo, so it is read twice.
Exercise
A news article shows a photo of a mayor cutting a ribbon. The caption directly below reads "Mayor Lena Ortiz opens the new library." Write alt text for the photo and explain your choice.
Show answer
Because the caption already names the mayor and the event, the alt should add what the caption leaves out, for example: "A smiling woman in a red coat cutting a blue ribbon in front of a glass building." Repeating the caption would make screen reader users hear the same facts twice. Using alt="" is also defensible if the visual details add nothing to the story.
Exercise
A page uses a wide photo of a wooden table as a purely decorative strip under the header. What should its alt attribute be, and why not remove the attribute?
Show answer
Use alt="". The empty value tells assistive technology to skip the image. If the attribute is missing, some screen readers announce the file name, which adds noise.