Sub topic 12
Serve images that fit every screen
This chapter assumes you can write a basic HTML page, link a stylesheet, and use media queries to adapt a layout. It adds one more piece: images that load at a sensible size on any screen.
Why one image file is not enough
A single 2000-pixel-wide photo works on a large monitor. On a phone it wastes data and slows the page. A tiny file looks blurry on a high-density display. Responsive images let you offer several files and let the browser choose.
The browser knows things you don't: the screen width, the pixel density, and sometimes the network speed. Your job is to describe the available files and how wide the image will appear. The browser makes the choice.
The img element with srcset and sizes
Here is the image from the portfolio page.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w"
sizes="(min-width: 48rem) 400px, 100vw"
width="800"
height="800"
loading="lazy"
alt="Portrait of Jane Doe at her desk">Each attribute has a separate job.
srcis the fallback. Browsers that ignoresrcsetuse it.srcsetlists candidate files. Thewnumber is the file's real width in pixels, not the display width.sizestells the browser how wide the image will be on the page. The browser reads it from left to right and uses the first matching condition. The last entry is the default.widthandheightgive the image's natural dimensions.loading="lazy"delays loading until the image is near the viewport.altis the text alternative for screen readers and for when the image fails to load.
In this example, the image is 400 CSS pixels wide when the viewport is at least 48rem. Below that, it fills the full viewport width (100vw). On a 400-pixel-wide phone with a 2x display, the browser needs about 800 pixels of image data and picks photo-800.jpg. On a wide desktop with a 1x display, it needs only 400 pixels and picks photo-400.jpg.
Prevent layout shift
Without dimensions, the browser doesn't know how much space an image needs until the file arrives. The text below it then jumps down. This jump is called layout shift.
The width and height attributes fix this. Browsers use them to compute an aspect ratio and reserve the space before loading. To keep the image flexible, add a small rule to your stylesheet.
img {
max-width: 100%;
height: auto;
}max-width: 100% stops the image from overflowing its container. height: auto keeps the proportions while the width changes, and the reserved aspect ratio still applies.
Lazy loading and its limit
loading="lazy" saves bandwidth for images far down the page. Don't use it on the image at the top of the page, such as a header photo. Lazy loading that image delays what the visitor sees first. Leave the attribute off for anything visible when the page opens.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Using `srcset` with `w` values but no `sizes` | The browser assumes the image is `100vw` wide and may download too much | Add a `sizes` attribute |
| Giving `w` values that don't match the real file widths | The browser picks the wrong file | Check each file's pixel width |
| Leaving out `width` and `height` | Content shifts when the image loads | Add both attributes |
| Lazy loading the top image | The first view appears late | Remove `loading="lazy"` there |
| Empty or missing `alt` on a meaningful image | Screen reader users get no information | Describe what the image shows |
How the browser decides
Try it
Create two or three resized copies of one photo. Add them to srcset, then open your browser's developer tools. In the Network panel, resize the window and reload. Watch which file is requested.
Exercise
An image has srcset="a-400.jpg 400w, a-800.jpg 800w" and sizes="100vw". A phone has a 400 CSS pixel wide viewport and a 2x pixel density. Which file does the browser most likely choose, and why?
Show answer
a-800.jpg. The image displays at 400 CSS pixels, and 2x density means it needs about 800 physical pixels. Only a-800.jpg is wide enough to look sharp.
Exercise
Why should you add width and height attributes even though the CSS sets max-width: 100% and height: auto?
Show answer
The attributes let the browser reserve the correct space, using the aspect ratio, before the file loads. This prevents layout shift.