Images and Alt Text
How the img element works, what alt text is for, when alt should be empty, and what a browser does with a missing or broken image.
Images and Alt Text
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `<img>` needs `src` and `alt`. The `alt` is the text alternative: what a screen reader announces, what shows if the image fails, and what search engines read.
- Write alt text for the image's purpose in context, not a description of pixels. A logo that links home says 'Home', not 'blue circle with a house'.
- A decorative image gets `alt=""`. In a test the accessibility tree dropped it entirely, while an `img` with no `alt` at all was exposed as an unnamed `img`.
- An image inside a link with `alt=""` leaves a link with no name. In a test it came out as a `link` with no text. Put the alt on the image, as `alt="Home"`.
- Set `width` and `height` attributes. In a test, with the file delayed 1.5 seconds, an image with them held a 300 by 225 box while loading, and the same image without them was 0 by 0 until it arrived.
The first element that loads something else
Every element so far came with the page. <img> is different: the HTML contains a reference, src, and the browser goes off and fetches the file. That raises two problems HTML has to answer. What if the file never arrives? And what if the reader can’t see it?
<img src="/img/team.jpg" alt="The five-person support team around a table" width="800" height="600">
alt answers both. It’s the text alternative that stands in for the image when it’s unavailable: a screen reader reads it, a slow connection or broken URL shows it, and search engines index it. img is a void element, so there’s no closing tag. The video covers relative versus absolute paths for src, which work exactly like link URLs.
What alt text is, and isn’t
Think of it as what you’d say if you were reading the page aloud over the phone. Describe the image’s purpose in context, not its pixels:
- A chart showing sales rose each quarter:
alt="Bar chart: sales rose every quarter of 2026". If the surrounding text already says it, shorter is fine. - A logo that links home:
alt="Acme home". The image is acting as the link text. - A product photo: what a shopper needs, such as colour and cut.
Skip “image of” and “picture of”, since the role is already announced. Keep it short, usually under about 125 characters; if a chart needs a paragraph, put the paragraph in the page and keep the alt as a pointer. If the image is text, repeat the text.
Decorative images: empty alt
If an image adds nothing, say so with an empty attribute: alt="". I tested the cases in headless Chromium 159 and read the accessibility tree:
| Markup | In the tree |
|---|---|
alt="A red square" |
img "A red square" |
alt="" |
not exposed at all |
no alt attribute |
an img with no name |
alt="" inside a link |
a link with no name |
alt="Home" inside a link |
link "Home" containing img "Home" |
The difference between the second and third rows is the lesson. An empty alt is a deliberate “ignore this”. A missing alt is a mistake the browser can’t interpret, so the image is exposed and a screen reader may fall back to reading the file name. Always write the attribute. The last two rows show why an image that is the only content of a link must carry the link’s label.
Reserving space with width and height
The browser doesn’t know an image’s shape until it downloads the file’s header. Without hints, text below it jumps when the image arrives. The width and height attributes fix that: the browser uses them to compute an aspect ratio at once. In a test, I delayed the file by 1.5 seconds and measured during the wait: a 400 by 300 image with those attributes in a 300px container (with max-width: 100%; height: auto) already had a 300 by 225 box, while the same image without them measured 0 by 0 until it arrived, then jumped to 300 by 225. The related CSS side is covered in sizing images and replaced elements and aspect ratio and responsive media.
A broken image shows what those attributes buy you. With width=200 height=80 the broken images in the test kept a 200 by 80 box, with or without alt text. With no dimensions it collapsed to a 16 by 16 icon. In both cases the img reported complete as true and naturalWidth as 0, so a script can detect failure.
Formats and next steps
Pick the format by content: JPEG or AVIF/WebP for photographs, PNG for sharp raster graphics, SVG for logos and icons. Serving the right size to each device is responsive images, the next lesson. SVG can also be placed inline, which has its own trade-offs in inline SVG and canvas.
What the video covers
The embed is Kevin Powell’s “HTML & CSS for Absolute Beginners: Images” (January 2025, about 12.5 minutes). Its chapters are introduction to images (0:00), image tag attributes (0:28), relative versus absolute paths (1:16), using alternative text (2:13), SEO best practices (3:54), file paths and structure (4:38), self-closing tags (6:25), adding a logo as SVG (7:35), alt text for decorative images (8:48), adding images to a second page (9:46) and writing descriptive alt text (11:38). That’s an unusually good match: two alt-text chapters and a decorative-image chapter. I could not read a transcript, so I describe it only from the chapters. The accessibility-tree table and the layout measurements are this lesson’s own. The deeper links are a three-minute alt text guide and the W3C’s decision tree.
Where this fits
This opens the images and media group and needs nothing from the earlier groups beyond HTML basics. Next, responsive images with srcset and sizes goes from one image to the right image for each screen.
Check yourself
A photo of a team sits in an article, purely for atmosphere. What alt do you give it?
Empty: `alt=""`. A decorative image adds nothing for someone who can't see it, and an empty alt makes assistive technology skip it. In a test it was removed from the accessibility tree. Leaving `alt` off entirely is different: the image is exposed with no name.
A linked logo has `<a href="/"><img src="logo.svg" alt=""></a>`. What is wrong?
The link has no accessible name. With `alt=""` the image contributes nothing, so a screen reader hears 'link' and nothing else. In a test the link was reported with no text. Use `alt="Home"` or the company name plus 'home'.
Why write `width` and `height` on an image whose size CSS controls anyway?
They give the browser the aspect ratio before the file loads, so it reserves space and the layout does not jump. In a test with a delayed file, the image had a 300 by 225 box while loading, and without the attributes it was 0 by 0. Pair them with `max-width: 100%; height: auto`.
What does a browser show when an image fails to load?
It keeps the box if `width` and `height` are set, and shows the alt text in it. In a test a broken image with both attributes held a 200 by 80 box and without them shrank to a 16 by 16 icon. Good alt text is the fallback content.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.