A correct image element is a little longer than most people write it. There is the source, there is the alternative text, there are the dimensions, and there is the loading behaviour.
Each of those prevents a specific problem: a broken layout, a screen reader that announces nothing, a page that jumps while it loads, or a phone that downloads a desktop-sized file.
The previous lesson in this module covered "How Do Links Connect Pages, Anchors and Email?", and the next lesson in this module is about "What Are the Three List Types and When Is Each Right?".
What is the minimum of a correct image?
<img src="images/diagram.svg" alt="A request travelling from browser to server and back" width="640" height="360">There are four attributes, and there are four problems they solve.
The alt attribute is what a screen reader announces, and what appears if the image fails to load.
The width and the height let the browser reserve the space before the file arrives, and that is what stops the page from jumping.
And the src points at the file, with the path relative to the page unless it starts with a slash.
How do you write the alt text?
You write it for somebody who cannot see the image, in the context of the page. That means describing what the image conveys, not what it looks like, and not repeating a caption that is already next to it.
<!-- good: the point of the image, in context -->
<img src="images/cascade-chart.png"
alt="Bar chart showing page load time falling from 3.2s to 0.9s after caching">
<!-- weak: describes the medium, not the content -->
<img src="images/cascade-chart.png" alt="chart">
<!-- decorative: no meaning to convey, so the alt must be empty -->
<img src="images/divider.svg" alt="">An empty alt is meaningful. It tells the assistive technology to skip the image.
It is the correct choice for dividers, background flourishes, and icons that sit next to the text they decorate.
Writing alt="image" or alt="spacer" is worse than nothing, because it makes a screen reader announce noise.
For an image that carries information in a longer form, you put the description in the text near it and you keep the alt short. For a diagram where the detail matters, a figure with a longer description is the right container.
How do you serve the right size?
Phones and desktops want different files.
The srcset attribute offers several candidate images, and the sizes attribute tells the browser how wide the image will be displayed, so it can pick before the CSS has even loaded.
<img src="images/hero-800.jpg"
srcset="images/hero-400.jpg 400w,
images/hero-800.jpg 800w,
images/hero-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 800px"
alt="A workbench with tools laid out in order of use"
width="800" height="450" loading="lazy">The w suffix is a width descriptor. The browser knows the real width of each file, and it combines that with the sizes value and the screen size of the device to pick one.
The sizes value is the width the image will occupy in CSS pixels, and it is what makes the choice accurate.
Get it wrong, and the browser picks an image that is too large or too small, which is worse than not offering the alternatives at all.
Should every image be lazy-loaded?
No.
The loading="lazy" attribute tells the browser to defer an image that sits below the fold, and that saves bandwidth and speeds up the first render.
The image right at the top of the page should load eagerly, because deferring it delays the moment when the page looks complete.
The common rule is this one: lazy-load everything except the first screenful.
<img src="images/hero.jpg" alt="..." width="1200" height="600"> <!-- eager, above the fold -->
<img src="images/section-1.jpg" alt="..." width="800" height="450" loading="lazy">What are the figures and the captions for?
The <figure> groups a piece of content with its caption. The caption is the <figcaption>, and it can come first or last in the group.
<figure>
<img src="images/architecture.svg" alt="Three layers: browser, server, database" width="700" height="420">
<figcaption>How a request moves through the three layers of a typical web application.</figcaption>
</figure>The reason to use a figure rather than an image with a paragraph after it is that the caption becomes programmatically associated with the image.
Screen readers announce them together, and when the page is printed the caption stays with the image.
What about the video and the audio?
Both of them are replaced elements, like an image, and both of them need a fallback and the controls.
The poster attribute gives a video a still frame before it plays, and that matters because many people will never press play.
<video controls poster="images/demo-poster.jpg" width="800" height="450">
<source src="media/demo.webm" type="video/webm">
<source src="media/demo.mp4" type="video/mp4">
Your browser cannot play this video.
<a href="media/demo.mp4">Download it instead.</a>
</video>Putting fallback text inside the element is what a browser shows when it cannot play the media at all, and the download link keeps the content reachable.
You always include the controls. A video that cannot be paused or muted is an accessibility failure, and not a feature.
How do you stop the images from making the page jump?
When an image arrives, the browser does not know how tall it is until it has loaded it. Until then it reserves no space, so the text below jumps down the moment the file arrives.
That movement is called the layout shift, and it is one of the most noticeable performance problems on the web.
The fix is to tell the browser the dimensions up front. The width and the height attributes on the img element do it, and the browser uses them to reserve the right amount of space before the file arrives. The image is then scaled to fit those dimensions, so you still control the layout with the CSS.
<img
src="images/workshop.jpg"
width="960"
height="540"
alt="Two people working at a shared table"
>With those attributes present, the browser computes the aspect ratio immediately, and it can reserve the space before a single byte of the image has downloaded.
The width and the height here describe the real proportions of the file, and then the CSS decides how large it is displayed.
There are three related habits that go with it.
You always write an alt attribute, even when it is empty for a purely decorative image. An empty alt="" tells a screen reader to skip it, while a missing one makes it read the name of the file.
You set loading="lazy" on the images below the fold, so they do not compete with the content the visitor is actually reading.
And you serve an appropriately sized file, because a browser will happily download a four thousand pixel image to display it at four hundred.
Quick reference
- Every image needs the
alt. You describe the meaning, or you use thealt=""for decoration. - You always set the
widthand theheightto stop the page jumping as the images load. - The
srcsetplus thesizeslets the phone download a smaller file. - You lazy-load below the fold, and you load the first screenful eagerly.
- The
<figure>and the<figcaption>bind a caption to its image. - Video and audio need the controls, a poster, the sources as fallbacks, and the text inside the element.
Try it yourself: you add an image with a caption to a page, you add a second version of the file at a different size with the srcset, and you set the width and the height so the layout does not shift. Then you load the page on a phone-sized screen and you check the network tab to see which file was actually downloaded.