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:
- Open this link: tomato-soup.jpg
- Right-click the image and choose Save image as… (Safari: Save Image As…).
- Save it in
my-website/images/with the nametomato-soup.jpg.
Your first image
<!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.
<img> is a void element — no closing tag. Its attributes do all the work:
| Attribute | What it does |
|---|---|
src | The path to the image file (the source). A relative path, as in Folders and relative paths. |
alt | The alternative text: a description read by screen readers, and shown if the image cannot load. |
width, height | The image's size in pixels. The browser reserves the space before the image arrives, so the page does not jump. |
Writing good alt text
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"oralt="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.
<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.
<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?
| Type | Best for |
|---|---|
.jpg | Photos |
.png | Drawings, screenshots, images with transparent parts |
.webp | Photos and drawings, smaller files (all modern browsers) |
.svg | Logos and icons that must stay sharp at any size |
.gif | Small animations |
Try it yourself
- Select the complete example and choose Try it. (The playground has the sample images, so the picture appears.)
- Change the
srctoimages/salad.jpgand write a matchingalt. - Break it on purpose: change
srcto/images/salad.jpg. The image breaks — the leading-slash trap. Now remove the/. - 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 (
.jpgis not.jpeg). - No
altat all. Screen readers read the file name. Fix: describe the image, or usealt=""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
widthandheight. The page jumps while images load. Fix: always add them.
Check yourself
- What does the
altattribute do? Give two uses. - What
altshould a decorative border image have? - The image does not show, and you see a small broken icon. What do you check first?
Answers
- It describes the image: screen readers read it aloud, and the browser shows it if the image cannot load. (Search engines also use it.)
alt=""— empty.- 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.


