Sub topic 1
Why one image file is not enough
A single <img src="photo.jpg"> works everywhere, but it can't fit every screen. A phone on a mobile connection and a 4K monitor on fiber have different needs. Whatever file you choose is wrong for one of them. This chapter explains why, and it sorts the problem into three separate causes. The later chapters each solve one cause with a specific HTML feature.
Prerequisites
This chapter assumes you already know:
- Basic HTML, including the
imgelement with itssrcandaltattributes. - Basic CSS, including media queries and viewport units such as
vw. - The common image formats (JPEG, PNG, WebP) and the fact that they trade file size against quality.
- The difference between CSS pixels and device pixels, which the next section reviews.
You don't need to know srcset, sizes, or picture. This tutorial introduces them in later chapters.
CSS pixels and device pixels
A CSS pixel is the unit that layout uses. If you write width: 400px, the element is 400 CSS pixels wide on every screen. A device pixel is one physical dot on the display.
The ratio between the two is the device pixel ratio (DPR). A DPR of 1 means one device pixel per CSS pixel. A DPR of 2 means a 400-CSS-pixel box is drawn with 800 physical pixels across. Many phones and laptops have a DPR of 2 or 3, and some desktop monitors do too.
For a sharp result, the image file needs about as many pixels as the box has device pixels. The formula is:
needed file width = displayed width in CSS pixels × DPRIf the file has fewer pixels, the browser stretches it and the image looks soft. If it has far more pixels, the browser shrinks it, and you've sent bytes that made no visible difference.
You can check these numbers on any page. Open the browser console after the page has loaded and run this:
window.addEventListener('load', () => {
const img = document.querySelector('img');
const needed = Math.ceil(img.clientWidth * window.devicePixelRatio);
console.log(`Displayed at ${img.clientWidth} CSS px, DPR ${window.devicePixelRatio}`);
console.log(`Sharp rendering needs about ${needed} px; the file is ${img.naturalWidth} px wide`);
});clientWidth reports the width the image is drawn at, and naturalWidth reports the width of the downloaded file.
The single-file problem
Here is the baseline markup that most pages start with:
<img
src="photo-1600.jpg"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">Suppose the CSS makes this image fill its column. The width it needs depends on the device:
| Device | Displayed width (CSS px) | DPR | File width needed (px) |
|---|---|---|---|
| Small phone | 360 | 3 | 1080 |
| Laptop, narrow window | 600 | 1 | 600 |
| Laptop, high-density display | 600 | 2 | 1200 |
| Desktop monitor | 1200 | 1 | 1200 |
| 4K monitor with scaling | 1200 | 2 | 2400 |
The needed width ranges from 600 to 2400 pixels for the same image. No single file fits that whole range. Any choice you make creates one of two failures.
Failure one: wasted bandwidth
Say you pick the 1600-pixel file so large screens look good. The laptop in the narrow window needs only 600 pixels. It still downloads a file with about seven times as many pixels (1600 × 1200 against 600 × 450). File size doesn't scale exactly with pixel count because compression varies, but it grows in the same direction and by a similar order of magnitude.
The cost shows up in three places:
- Visitors on slow or metered connections wait longer and pay more.
- Large images delay the largest contentful paint, which is the moment the main content appears. This hurts both user experience and search ranking signals.
- Phones spend memory and CPU decoding pixels that are discarded when the image is scaled down.
Failure two: a blurry image
Now say you pick the 600-pixel file to keep pages light. The 4K monitor needs 2400 pixels and gets 600. The browser stretches each source pixel across a 4×4 block of device pixels. Text in the photo smears and fine detail disappears.
You can't fix this by making the file "just a bit bigger". Any fixed size is too big for some visitors and too small for others.
Three different problems
The single-file problem is really three problems. They need different tools, and mixing them up is the most common source of confusion with responsive images.
- Resolution switching. The same picture is served at different pixel sizes. A phone gets 800 pixels across and a large monitor gets 2400. You solve this with
srcset, and withsizeswhen the layout is fluid. - Art direction. The picture itself should change. A wide landscape crop works on a desktop. On a narrow phone screen the subject becomes tiny, so a tighter, squarer crop works better. You solve this with
pictureandsource media. - Format switching. The same picture is served in different file formats. AVIF and WebP usually compress better than JPEG, but not every browser supports them. You solve this with
pictureandsource type.
The diagram below summarizes the split.
The first two rows of the table above are about resolution. A crop change is a different kind of decision. You make it for design reasons, so the browser can't make it for you. For that reason, picture with media is a command: the browser must follow it. Resolution switching and format negotiation work differently.
Who decides which file to download
With srcset, you describe the available files and the browser chooses. You don't tell it which one to use. The browser knows things you can't know when you write the HTML:
- The real viewport width and the device pixel ratio.
- Whether the user has enabled a data-saving mode.
- Which files are already in the cache.
- How fast the connection currently is.
Because of this, two browsers can legitimately choose different files for the same page and the same screen. When you test, treat the browser's choice as the result to inspect, not as something to force. Browsers do follow the type and media attributes inside picture strictly, as described above.
This also explains why you must give the browser accurate information. Later chapters cover the attributes that carry it: x and w descriptors for the files, and sizes for the layout. If the information is wrong, the browser makes a poor choice with full confidence.
Common misconceptions
- "CSS can fix this." Setting
max-width: 100%makes an image shrink to fit its container, but it doesn't change which file is downloaded. The full-size file still travels over the network. - "I can just use a huge image and let the browser scale it." This is the bandwidth failure described above. It works visually, but it is expensive.
- "A high-quality image is always better." Past the point where the file has at least one pixel per device pixel, extra pixels add bytes and no visible detail.
- "Responsive images means responsive layout." Layout adapts the box. Responsive images adapt the file inside the box. You need both, and they interact:
sizesexists because the browser has to pick a file before the CSS layout is known. - "
srcsetforces the browser's choice." It doesn't. It offers candidates, and the browser picks one.
What comes next
The following chapters go in this order. First comes the img element as the baseline that every other technique falls back on. Then x descriptors for fixed-size images, w descriptors and sizes for fluid layouts, and how the browser picks a candidate. After that come width and height to prevent layout shift, picture for art direction and format switching, ways to combine the techniques, generating variants, lazy loading, and debugging.
Exercises
Exercise
An image is displayed at 500 CSS pixels wide on a phone with a device pixel ratio of 3. About how many pixels wide should the image file be for a sharp result? What happens if you serve a 500-pixel file?
Show answer
About 1500 pixels (500 × 3). A 500-pixel file has to be stretched across 1500 device pixels, so the browser upscales it and the image looks soft or blurry.
Exercise
A page serves one 2400-pixel-wide JPEG to every visitor, and CSS sets max-width: 100%. A visitor on a 360-CSS-pixel-wide phone with a DPR of 2 loads the page. Does max-width: 100% reduce the bytes this visitor downloads? Which of the three problems from this chapter (resolution switching, art direction, format switching) fits this situation?
Show answer
No. max-width: 100% only scales the displayed size, so the full 2400-pixel file still downloads. The phone needs about 720 pixels (360 × 2), so this is a resolution switching problem: serving a smaller file to a smaller screen with srcset.
Exercise
A designer wants phones to see a tight square crop of a portrait and desktops to see a wide landscape crop. Why can't srcset alone do this, and which element handles it?
Show answer
srcset only offers different sizes of the same picture, and the browser chooses among them. A different crop is a different picture, chosen for design reasons, so you need the picture element with source elements that use the media attribute (art direction).