5 / 10 · Intro
Images and alt text
An img element needs two attributes to be useful: src, which says where the picture is, and alt, which says what it shows when it cannot be seen.
Jump to the 10 practice questions1Explain
The img element
Pictures are not stored inside the HTML file. The page points to them, and the browser fetches each one as a separate file. The <img> element does the pointing. It is a void element, so it has no closing tag, and its src attribute holds the address of the image, absolute or relative, just like the href of a link. Images are replaced elements: the browser replaces the element with the picture, and the picture decides how big it is unless you say otherwise.
<img src="images/sourdough.jpg"
alt="A round sourdough loaf with a dark, cracked crust"
width="400" height="300">src, alt, and the real width and height of the file in pixels.
2Explore
When the file fails to load
The picture file is missing on purpose. What a visitor gets is the alt text, so it has to carry the point of the image.
Tap a choice: the code and the preview change together.
<img src="missing.png" alt="Loaves sold tripled from January to March" width="200" height="60">
<p>The file is missing, so the browser shows the alt text.</p>Nothing is drawn: there is no visible content.
3Problem
A decorative line
What alt text suits a decorative wavy line?
Hint
A decorative image should be skipped by a screen reader.
Solution
Answer: An empty alt=""
Empty alt tells screen readers to skip it. Leaving alt out may make them read the file name.
4Problem
Alt text for a chart
Which is the best alt text for a chart showing sales tripling from January to March?
Hint
Say what the chart shows, not that it is a chart.
Solution
Answer: Sales tripled from January to March
Give what would be lost without the picture: the point it makes.
5Problem
A logo that is a link
<a href="/">
<img src="logo.png" alt="?"
width="120" height="40">
</a>The logo is the only thing inside a link to the home page. What should replace the question mark?
Hint
Here the alt text becomes the name of the link.
Solution
Answer: Corner Bakery home page
When an image is the only content of a link, its alt says where the link goes. An empty alt would leave the link with no name at all.
6Explain
Writing alt text
The alt attribute holds a text alternative. A screen reader reads it aloud, a browser shows it when the file fails to load, and search engines read it to understand the picture. MDN suggests a simple test: what would be lost if the image did not show up? Write that, briefly. For a photo of a product, describe what someone would need to know to choose it.
7Problem
Why width and height
Why set width and height on an img?
Hint
Think about the page while the image is still loading.
Solution
Answer: So the browser reserves space and text does not jump
Use the real size; resize with CSS if needed.
8Problem
What a screen reader announces
<img src="logo.png"
alt="Corner Bakery"
width="120" height="40">
<img src="dots.png" alt=""
width="300" height="8">
<img src="rye.jpg"
alt="A rye loaf cut in half"
width="300" height="200">How many of these images does a screen reader announce?
Hint
An empty alt tells it to skip the image.
Solution
Answer: 2
The logo and the rye loaf have alt text, so they are announced. The dotted line has alt="" and is skipped: 2.
9Problem
Words in a picture
A banner image shows only the words Open daily from 7 am. What is the best alt text?
Hint
A visitor who cannot see the image should get the same words.
Solution
Answer: Open daily from 7 am
When an image shows text, the alt text repeats that text. Banner or Image with text says nothing about the opening hours.
10Explain
Figures and captions
When an image comes with a caption, wrap both in <figure> and put the caption in <figcaption>. This ties the caption to the image for assistive technology, which a plain paragraph under the image does not do. A caption and alt text have different jobs: the caption is read by everyone, while alt replaces the image when it is missing.
<figure>
<svg viewBox="0 0 120 60" width="120" height="60" role="img">
<title>Three bars growing</title>
<rect x="10" y="40" width="25" height="20" fill="#d9a35b"/>
<rect x="48" y="25" width="25" height="35" fill="#d9a35b"/>
<rect x="86" y="5" width="25" height="55" fill="#d9a35b"/>
</svg>
<figcaption>Loaves sold, January to March</figcaption>
</figure>11Problem
Sharp at any size
Photographs are usually JPEG or WebP, flat graphics with sharp edges are PNG, and drawings made of shapes are often SVG, a text format that stays sharp at any size. A small image can even be written straight into the src as a data URL that starts with data:image/svg+xml,; the exercises below use this so nothing loads from another site.
Which format stays sharp at any size?
Hint
It is a text format made of shapes.
Solution
Answer: SVG
SVG describes shapes, so it is redrawn at every size.
12Practice
Alt text and a caption
The long src values are the pictures themselves. Give the chart alt text that states its point (sales grew from January to March). Give the decorative divider an empty alt. Then wrap the chart in a <figure> with a <figcaption>.
- chart has alt text
- divider has alt=""
- chart inside figure
- figcaption
<h2>Loaves sold</h2>
<img class="chart" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 100'%3E%3Crect width='160' height='100' fill='%23fff'/%3E%3Crect x='20' y='60' width='24' height='30' fill='%23c58a3a'/%3E%3Crect x='60' y='40' width='24' height='50' fill='%23c58a3a'/%3E%3Crect x='100' y='15' width='24' height='75' fill='%23c58a3a'/%3E%3Cline x1='10' y1='90' x2='150' y2='90' stroke='%23333' stroke-width='2'/%3E%3C/svg%3E" width="160" height="100">
<img class="divider" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 10'%3E%3Cpath d='M0 5 Q 15 0 30 5 T 60 5 T 90 5 T 120 5 T 150 5 T 180 5 T 210 5 T 240 5 T 270 5 T 300 5' fill='none' stroke='%23c58a3a' stroke-width='2'/%3E%3C/svg%3E" width="300" height="10">
<p>Thank you for a busy spring.</p>Hint
Three changes: alt on the chart, alt="" on the divider, and figure with figcaption around the chart.
A solution
<h2>Loaves sold</h2>
<figure>
<img class="chart" alt="Bar chart: loaves sold grew from January to March" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 100'%3E%3Crect width='160' height='100' fill='%23fff'/%3E%3Crect x='20' y='60' width='24' height='30' fill='%23c58a3a'/%3E%3Crect x='60' y='40' width='24' height='50' fill='%23c58a3a'/%3E%3Crect x='100' y='15' width='24' height='75' fill='%23c58a3a'/%3E%3Cline x1='10' y1='90' x2='150' y2='90' stroke='%23333' stroke-width='2'/%3E%3C/svg%3E" width="160" height="100">
<figcaption>Loaves sold, January to March</figcaption>
</figure>
<img class="divider" alt="" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 300 10'%3E%3Cpath d='M0 5 Q 15 0 30 5 T 60 5 T 90 5 T 120 5 T 150 5 T 180 5 T 210 5 T 240 5 T 270 5 T 300 5' fill='none' stroke='%23c58a3a' stroke-width='2'/%3E%3C/svg%3E" width="300" height="10">
<p>Thank you for a busy spring.</p>13Practice
An accessible SVG
Make this inline SVG accessible: add role="img" to the <svg> and a <title> inside it that says Loaf of bread.
- role="img"
- title inside svg
<svg viewBox="0 0 120 80" width="120" height="80">
<ellipse cx="60" cy="48" rx="52" ry="26" fill="#d9a35b"/>
</svg>Hint
role="img" goes in the svg start tag; title is the first element inside.
A solution
<svg viewBox="0 0 120 80" width="120" height="80" role="img">
<title>Loaf of bread</title>
<ellipse cx="60" cy="48" rx="52" ry="26" fill="#d9a35b"/>
</svg>14Explain
Common mistakes
<img src="wave.png" alt="Image of a wavy line" width="300" height="10"><img src="wave.png" alt="" width="300" height="10">A purely decorative line gets an empty alt, so screen readers skip it. Image of … is never needed: the reader already says it is an image.
15
Summary
- img + src + alt; set width and height to the real size.
- alt says what would be lost without the picture; decorative images get alt="".
- Use figure and figcaption for captioned images; give inline SVG role="img" and a title.