Lesson 19 of 51 · Module 3, lesson 4 of 6 · 5 min

Images and alt text

Add images with a size and a description, so that every reader gets the same information.

What you'll learn

  • How to add an image with <img>
  • How to write good alternative text, and when to leave it empty
  • Why you should give every image a width and a height

Why this matters

Images make a page friendly and clear. But not everyone sees them: people who are blind, people on a slow connection, and search engines all rely on a text description. And an image without a size makes the page jump as it loads. This lesson gets both right from the start.

Get the sample image

Download the sample photo into your website:

  1. Open this link: tomato-soup.jpg
  2. Right-click the image and choose Save image as… (Safari: Save Image As…).
  3. Save it in my-website/images/ with the name tomato-soup.jpg.

Your first image

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tomato soup</title>
</head>
<body>
  <h1>Tomato soup</h1>
  <img src="images/tomato-soup.jpg" alt="A white bowl of hot tomato soup" width="300" height="200">
  <p>Ready in thirty minutes.</p>
</body>
</html>

What you'll see: the photo of a bowl of soup, between the heading and the paragraph.

The page "Tomato soup" with a drawing of a white bowl of red soup under the heading, followed by the sentence "Ready in thirty minutes."

<img> is a void element — no closing tag. Its attributes do all the work:

AttributeWhat it does
srcThe path to the image file (the source). A relative path, as in Folders and relative paths.
altThe alternative text: a description read by screen readers, and shown if the image cannot load.
width, heightThe image's size in pixels. The browser reserves the space before the image arrives, so the page does not jump.

Writing good alt text

Three examples of what a screen reader says. With good alt text, alt="A white bowl of hot tomato soup", it says "A white bowl of hot tomato soup, image". With no alt attribute, it often reads the file name: "tomato-soup.jpg, image". For a decorative line with alt="" (empty), it says nothing and skips it.

Ask: "If I could not see this picture, what would I need to know?" Then write that, in one short sentence.

  • ✓ alt="A white bowl of hot tomato soup"
  • ✗ alt="image" or alt="photo" — says nothing useful.
  • ✗ alt="tomato-soup.jpg" — a file name is not a description.
  • ✗ Starting with "Image of…" — screen readers already say "image".

If the image contains important text (for example a sign or a poster), put that text in the alt.

Decorative images: alt=""

Some images are only decoration, such as a dividing line. For those, write alt="" — an empty alt. The screen reader then skips the image. Never leave out alt completely: without it, many screen readers read the file name instead.

html
<img src="images/leaf.png" alt="" width="200" height="30">

Width, height and phones

width and height set the size on the page, in pixels, and tell the browser the image's shape (here 3 wide to 2 high, like the file). The examples use 300 × 200, which fits on a phone. A larger size, such as 600 × 400, would be wider than many phone screens. You will learn to make images shrink to fit with one line of CSS later; until then, choose sizes that fit.

Loading only when needed

For images far down a long page, add loading="lazy". The browser then waits to download them until the reader scrolls near them, which saves data on phones.

html
<img src="images/salad.jpg" alt="A bowl of green salad with tomatoes" width="300" height="200" loading="lazy">

Do not use it for the first image at the top of a page.

Which file type?

TypeBest for
.jpgPhotos
.pngDrawings, screenshots, images with transparent parts
.webpPhotos and drawings, smaller files (all modern browsers)
.svgLogos and icons that must stay sharp at any size
.gifSmall animations

Try it yourself

  1. Select the complete example and choose Try it. (The playground has the sample images, so the picture appears.)
  2. Change the src to images/salad.jpg and write a matching alt.
  3. Break it on purpose: change src to /images/salad.jpg. The image breaks — the leading-slash trap. Now remove the /.
  4. In your own folder, build the page with your downloaded tomato-soup.jpg.

Common mistakes

  • Broken image icon. The path or the file name is wrong. How to spot it: you see a small icon and your alt text. Fix: check the folder, the spelling, the case and the extension (.jpg is not .jpeg).
  • No alt at all. Screen readers read the file name. Fix: describe the image, or use alt="" if it is decoration.
  • Alt text that repeats the caption or the text next to it. The reader hears it twice. Fix: describe what the text does not say.
  • Missing width and height. The page jumps while images load. Fix: always add them.

Check yourself

  1. What does the alt attribute do? Give two uses.
  2. What alt should a decorative border image have?
  3. The image does not show, and you see a small broken icon. What do you check first?

Answers

  1. It describes the image: screen readers read it aloud, and the browser shows it if the image cannot load. (Search engines also use it.)
  2. alt="" — empty.
  3. The path in src: folder, spelling, capital letters and extension.

Next: sometimes an image needs a visible caption, like a photo in a newspaper. The next lesson shows the elements for that.

Chat with us on WhatsApp