Sub topic 12
Serving the right size with srcset and sizes
This chapter assumes you know the img element, basic CSS widths, and how breakpoints work in a responsive layout. It covers how to give the browser several versions of one photo and let it choose the smallest file that still looks sharp.
The problem with one file
A single src forces every visitor to download the same file. A 1600px-wide photo wastes data on a phone that shows it at 400 CSS pixels. A 400px file looks blurry on a large screen. Serving several sizes fixes both problems.
This is not art direction. Every file here shows the same crop at a different resolution. Changing the crop is a job for the picture element, covered in the previous chapter.
Describing the candidates with srcset
The srcset attribute lists candidate files. Each one has a width descriptor, a number followed by w. That number is the file's real pixel width, not the width it will be shown at.
<img
src="team-800.jpg"
srcset="team-400.jpg 400w, team-800.jpg 800w, team-1600.jpg 1600w"
sizes="(min-width: 960px) 50vw, 100vw"
width="800" height="600"
alt="Four colleagues around a whiteboard">The src value is the fallback for browsers that do not understand srcset.
Telling the browser the display width with sizes
The browser picks a file before it loads the CSS and lays out the page, so it cannot know how wide the image will be. The sizes attribute supplies that answer.
Read sizes from left to right. The browser uses the first media condition that matches. The last entry has no condition and is the default.
(min-width: 960px) 50vwmeans that on viewports at least 960px wide, the image takes half the viewport width.100vwmeans that in all other cases, it takes the full viewport width.
The browser multiplies the slot width by the screen's pixel density. A 400px-wide slot on a 2x display needs about 800 pixels of image data, so the browser would choose team-800.jpg.
A worked example
A visitor opens the page above on a 390px-wide phone with a 3x display.
- No condition matches, so the slot is
100vw, which is 390 CSS pixels. - At 3x density, the browser needs about 1170 pixels.
- The smallest file that covers this is
team-1600.jpg.
On a 1280px-wide laptop with a 1x display:
(min-width: 960px)matches, so the slot is50vw, which is 640 CSS pixels.- At 1x density, the browser needs 640 pixels.
- The smallest file that covers this is
team-800.jpg.
Common errors
- A
sizesvalue that does not match the layout. If the CSS makes the image 33% wide butsizessays100vw, the browser downloads files that are too large. Updatesizeswhen the layout changes. - Omitting
sizeswhen usingwdescriptors. The browser then assumes100vw, which is often wrong. - Using pixel values that disagree with the CSS.
sizesdescribes the layout. It does not set the layout. - Expecting a precise choice. The browser may choose a larger file than your calculation suggests, for example on a slow connection or when it has a cached version. Treat the result as a decision the browser makes, not one you control.
- Generating too few sizes. A set with one huge jump, such as 400px to 2400px, leaves the browser no good middle option.
Exercise
An image is shown at 50% of the viewport width on screens at least 1000px wide, and at full width on smaller screens. Write the sizes value. Then say which file a browser on a 1200px-wide, 1x screen should pick from 500w, 700w, and 1200w candidates.
Show answer
sizes="(min-width: 1000px) 50vw, 100vw". At 1200px the slot is 50vw, which is 600 CSS pixels. At 1x density the browser needs 600 pixels, so the smallest file that is wide enough is the 700w one.