Sub topic 1
Why one image file is not enough
Prerequisites
This chapter assumes you know:
- Basic HTML, including the
imgelement with itssrcandaltattributes. - Basic CSS, including media queries and viewport units such as
vw. - Common image formats: JPEG, PNG, and WebP.
- The difference between CSS pixels and device pixels.
The problem with a single file
A plain img element points to one file:
<img src="photo-1600.jpg" width="1600" height="1200" alt="A lighthouse on a rocky coast at sunset">Every visitor downloads this same 1600-pixel-wide file. That works for some visitors and fails for others, in two opposite ways.
- Too large. A phone may show the image at 360 CSS pixels wide. It still downloads all 1600 pixels of width. The extra data costs the user time and mobile data, and it slows the page.
- Too small. Suppose you choose a 400-pixel file to save bandwidth. A laptop with a high-density screen displays the image at 400 CSS pixels, but that area covers 800 device pixels. The browser stretches the file, and the image looks soft.
No single file width fits every screen. You need to offer several files and let the browser pick one.
Two different problems
"Responsive images" covers two separate jobs. Mixing them up is a common source of confusion.
| Job | Question it answers | Main tools |
|---|---|---|
| Resolution switching | Which size of the same picture should I download? | `srcset` with `x` or `w` descriptors, and `sizes` |
| Art direction | Should a different crop or composition be shown here? | `picture` with `source` and `media` |
A third, related job is format switching. It serves AVIF or WebP to browsers that support them and JPEG to the rest. It uses picture with source and type.
In resolution switching, every file shows the same picture at a different size. In art direction, the files show different crops. A wide landscape crop on a desktop and a tight square crop on a phone are an example.
Who decides?
With srcset and sizes, you give the browser a menu and some hints. The browser chooses, based on things you cannot see from the HTML: screen density, window size, and sometimes network conditions or user settings. The choice is a hint-based decision, not a command.
With picture, you control the choice more directly. The browser walks through the source elements in order and uses the first one that matches.
What the rest of the tutorial covers
The remaining chapters build up in this order:
- The
imgelement as the baseline and fallback. srcsetwithxdescriptors, then withwdescriptors andsizes.- How the browser picks a candidate.
widthandheightto prevent layout shift.- The
pictureelement, format switching, and art direction. - Combining the techniques, generating variants, lazy loading, and checking which file was chosen.
You will see the plain img stay central throughout. Even inside picture, the img element is what renders the image and provides the fallback.
Exercise
Exercise
A page shows a 600 CSS pixel wide photo. You serve one 600-pixel-wide file. A visitor opens the page on a 2x density screen. Is the image too large or too small for that visitor, and what kind of solution addresses it?
Show answer
Too small. The image covers 1200 device pixels, so the 600-pixel file is stretched and looks soft. This is a resolution switching problem. You solve it by offering a larger candidate, such as a 1200-pixel file, through srcset.