edubun
English
1 / 15

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 questions

You will learn

  • Write alt text that says what the image shows or means

Before this: Links

1Explain

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.

alt
<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>
Preview

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.

Your code
<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>
Preview

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>.

Checks
  • chart has alt text
  • divider has alt=""
  • chart inside figure
  • figcaption
Your code
<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>
Preview
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.

Checks
  • role="img"
  • title inside svg
Your code
<svg viewBox="0 0 120 80" width="120" height="80">
  <ellipse cx="60" cy="48" rx="52" ry="26" fill="#d9a35b"/>
</svg>
Preview
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

Mistake
<img src="wave.png" alt="Image of a wavy line" width="300" height="10">
Fixed
<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.

Next unitLists
Full text · The whole lesson as one text

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.

The width and height attributes give the size of the image in pixels, without a unit. Set them to the real size of the file. The HTML usually arrives before the pictures. So without these numbers the browser lays out the text first and then pushes it down when each image finishes loading. That is jarring if you have started reading. With them, the browser reserves the right space from the start. To show an image smaller or larger, use CSS rather than changing these numbers.

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. For a chart, give the point the chart makes, such as Sales tripled from January to March, rather than Bar chart.

  • Do not start with Image of or Picture of. Screen readers already announce that it is an image.
  • Decorative images, such as a wavy divider line, get an empty alt="". Then screen readers skip them. Leaving alt out entirely is different: some screen readers read the file name instead.
  • If an image is the only content of a link, its alt text is the link text, so describe where the link goes (Home), not the picture (Bread logo).
  • If the same information is already in the text next to the image, alt="" avoids saying it twice.

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. They should not simply repeat each other. A figure does not have to hold a picture; a code sample, a table or a poem can be a figure too.

Formats, data URLs and inline SVG

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. An SVG can also be written directly in the HTML with an <svg> element. Give it role="img" and a <title> child so screen readers have a name for it. Finally, loading="lazy" on images far down a long page tells the browser to wait until the reader scrolls near them.

One more rule from MDN: only use images you have the right to use. Pictures you find online are protected by copyright unless their licence says otherwise, and pointing your src at someone else's server without permission, called hotlinking, also makes them pay for your traffic.

Key points
  • 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.

Practice questions from Images and alt text on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (9)
  • WHATWG, HTML Living Standard (html.spec.whatwg.org, checked 2026-10-17)
  • MDN Web Docs, Learn web development: Structuring content with HTML, and the HTML element reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content and developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements, checked 2026-10-17)
  • MDN, HTML images (alt text guidance, checked 2026-10-17)
  • W3C WAI, Images tutorial and alt decision tree (w3.org/WAI/tutorials/images/decision-tree, checked 2026-10-17)
  • CERN, A short history of the Web (home.cern/science/computing/the-birth-of-the-web/short-history-web/, checked 2026-10-17)
  • W3C and WHATWG memorandum of understanding, 28 May 2019 (w3.org/2019/04/WHATWG-W3C-MOU.html, checked 2026-10-17)
  • W3C, Using ARIA, first rule of ARIA use (w3.org/TR/using-aria/, checked 2026-10-17)
  • W3C Markup Validation Service (validator.w3.org, checked 2026-10-17)
  • MDN, Viewport meta element (checked 2026-10-17).

Checked: 2026-10-17