Sub topic 15
Writing alt text
This chapter assumes you know the img element and its attributes, basic CSS, and how responsive layouts work. By the end you should be able to decide whether an image needs alt text, write a useful description, and avoid the common mistakes.
What alt text does
The alt attribute holds a text replacement for an image. Three groups of people rely on it:
- Screen reader users hear the alt text read aloud in place of the image.
- Users on slow or broken connections see the alt text when the file fails to load.
- Search engines use it to understand what the image shows.
Alt text is not a caption. A caption is visible to everyone and adds context. Alt text is read only when the image is not seen, and it should convey what the image contributes to the page.
The core question: what is this photo for?
Earlier in this tutorial you decided what each photo is for. That decision drives the alt text. The same photo can need different text on different pages.
Take a photo of a baker lifting a loaf from a stone oven:
- On a bakery's "About us" page, the photo shows the craft. Alt text:
A baker lifting a loaf from a stone oven. - On a page about oven maintenance, the photo may only illustrate the topic. Alt text:
Stone oven in use, or empty if the nearby text already says everything. - On a product page where the photo is a link to the bread recipe, the alt text should describe the link's destination, such as
Sourdough recipe.
Ask what a visitor would miss if the image vanished. Write that.
Four kinds of images
Most images fall into one of these groups, and each has a different rule.
| Kind | Purpose | Alt text approach |
|---|---|---|
| Informative | Adds content the text does not give | Describe the relevant content briefly |
| Decorative | Adds mood only | Use an empty `alt=""` |
| Functional | Sits inside a link or button | Describe the action or destination |
| Complex | Chart, diagram, infographic | Short alt text plus a longer description nearby |
Informative images
Describe what matters in the context of the page. Include details the surrounding text relies on. Skip details that do not matter.
<img
src="team-800.jpg"
srcset="team-400.jpg 400w, team-800.jpg 800w, team-1600.jpg 1600w"
sizes="(min-width: 960px) 50vw, 100vw"
width="800" height="600"
alt="Four colleagues around a whiteboard">This describes who and what, in a short phrase. If the page says the team is planning a launch, the alt text does not need to repeat that.
Decorative images
Some photos add atmosphere and carry no information. Give them an empty alt attribute. Screen readers skip an image with alt="".
<img src="texture-flour.jpg" width="1200" height="400" alt="">Do not leave the attribute out. When alt is missing, many screen readers read the file name instead, such as "texture dash flour dot jpg". An empty alt="" is a deliberate signal. A missing attribute is an error.
If a decoration is applied through CSS background-image, it has no alt text at all, which is correct for decoration. This is one reason to use CSS backgrounds for purely visual images and img for content.
Functional images
When an image is the only content of a link or button, the alt text must say what happens when it is activated, not what it looks like.
<a href="/recipes/sourdough">
<img src="loaf-400.jpg" width="400" height="300" alt="Sourdough recipe">
</a>A description such as Round loaf with a deep crust would leave a screen reader user guessing where the link goes.
Complex images
A chart or infographic cannot be summarized in a sentence. Give the image a short alt that names it, and place the full description in the page text.
<figure>
<img src="sales-chart.png" width="800" height="450"
alt="Bar chart of monthly sales, described below">
<figcaption>
Sales rose each month from January (120 loaves a day) to June (310 loaves a day).
</figcaption>
</figure>How to write a good description
Follow these habits.
- Lead with the most important information. Screen readers read from the start, and some users skip ahead.
- Be specific.
A baker lifting a loaf from a stone ovenbeatsPerson with bread. - Keep it short. One sentence or phrase is usually enough. A common guideline is under about 125 characters, though that is a rule of thumb, not a limit in the specification.
- Describe what is visible and relevant. Do not guess at feelings or identities unless the page states them.
- Use normal punctuation. A final period helps screen readers pause.
- Write for the page's language and tone.
Phrases to drop
- Do not start with "Image of", "Picture of", or "Photo of". Screen readers already announce that an image is present. Mention the medium only when it matters, for example
Oil painting of a harbor. - Do not stuff keywords.
bread bakery best bread buy bread onlinehelps no one and can be treated as spam by search engines. - Do not repeat the caption or the paragraph next to the image word for word. Repetition makes screen reader users hear the same content twice.
Text inside images
If a photo contains text that matters, such as a sign or a banner, include that text in the alt text. Better still, put important text in HTML instead of baking it into the image, so it can be resized, translated, and read by software.
Alt text and cropping
The crops you choose affect what the alt text should say. If you use object-fit: cover or <picture> to show a different crop at different screen sizes, check that the alt text is still true for each 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>The alt attribute lives on the img element, so it applies to every source. Suppose the wide crop shows the whole bakery and the square crop shows only the baker's hands. A single sentence must then work for both. Write alt text for the subject you protected with your focal point, because that is present in every crop. Do not describe background details that disappear in one version.
If two crops differ so much that no single description is accurate, the images may be serving different purposes, and it is better to use separate img elements shown or hidden with CSS. Note that hidden content must be hidden in a way that also removes it from the accessibility tree, such as display: none.
Overlaid text
When you place text over a photo, the visible text is real HTML and is read separately. Do not repeat it in the alt text. If the photo is decorative behind a headline, use alt="", or use a CSS background so the photo is not exposed at all.
Common errors
- A missing
altattribute, which most validators flag and screen readers handle poorly. - File names as alt text, such as
IMG_4032.jpg. - The same alt text on every image in a gallery.
- Describing the look of a functional image instead of its action.
- Writing alt text once and never checking it after the image or the page's purpose changes.
- Treating the
titleattribute as a substitute. Support fortitleis inconsistent, and touch and keyboard users often cannot reach it.
Checking your work
You can test alt text without special tools.
- Turn images off in your browser, or block them, and read the page. It should still make sense.
- Use a screen reader such as NVDA, VoiceOver, or Orca on a page with a few images and listen to the result.
- Run an automated checker such as Lighthouse or axe. These find missing attributes but cannot judge quality, so you still need a human check.
The flow below summarizes the decision.
Where this fits in the review checklist
The final chapter collects the review steps. For alt text, the checks are: every img has an alt attribute, decorative images use alt="", functional images describe the action, and no description starts with "image of" or repeats nearby text.
Exercise
A product card shows a photo of a ceramic mug. The whole card, including the photo, is wrapped in one link to /shop/blue-mug. The card's heading already reads "Blue stoneware mug". What alt text would you give the photo, and why?
Show answer
Use alt="". The link already has an accessible name from the heading, so a description of the photo would make a screen reader read the same information twice. If the photo were the only content of the link, you would instead use text describing the destination, such as "Blue stoneware mug".
Exercise
Your hero image uses a <picture> element. The wide crop shows a baker at a counter with shelves of loaves behind, and the square crop shows only the baker lifting a loaf. Write one alt text that is accurate for both crops.
Show answer
A baker lifting a loaf from a stone oven. It describes the subject that the focal point protects, which is visible in both crops. It leaves out the shelves, which appear only in the wide crop.
Exercise
Name two problems with this markup and fix it: <img src="IMG_4032.jpg" alt="Photo of bread bakery best bread buy bread">
Show answer
The alt text begins with "Photo of", which is redundant, and it is stuffed with keywords instead of describing the image. The fixed version is <img src="IMG_4032.jpg" width="800" height="600" alt="Sliced sourdough loaf on a wooden board">. Adding width and height also reserves space and prevents layout shift.