Sub topic 6
How the browser picks a file
This chapter assumes you know basic HTML (img, src, alt), CSS media queries and viewport units, and the difference between CSS pixels and device pixels. You have already seen srcset with w descriptors and the sizes attribute. Here you will see how the browser combines them into one decision.
The inputs to the decision
The browser does not read the page top to bottom and pick a file at that moment. It needs four facts, and it gets them at different times:
- The list of candidates from
srcset, each with a width descriptor such as800w. - The
sizesvalue, which it evaluates against the current viewport to get a slot width in CSS pixels. - The device pixel ratio (DPR), the number of device pixels per CSS pixel.
- Its own extras, such as cached files, network conditions, or a data-saving mode.
The browser makes this choice before layout, often before the CSS has even loaded. That is why sizes exists: it is your promise about the final width, written where the browser can read it early.
The selection steps
Take this markup:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 60em) 33vw, (min-width: 40em) 50vw, 100vw"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">Suppose the viewport is 1000 CSS pixels wide and the DPR is 2. The default em is 16px, so 40em is 640px and 60em is 960px.
- The browser checks
sizesfrom left to right and uses the first condition that is true. At 1000px,(min-width: 60em)matches, so the slot is33vw, which is 330 CSS pixels. - It multiplies by the DPR: 330 × 2 = 660 device pixels.
- It looks for the smallest candidate at least 660 pixels wide.
photo-800.jpgqualifies.
On a 500px-wide phone with DPR 3, no media condition matches, so the slot is 100vw, or 500 CSS pixels. The target is 1500 pixels, and photo-1600.jpg is the smallest file that covers it.
What the browser may change
The specification says the browser chooses "the most appropriate" candidate. The steps above are the usual behavior, not a guarantee. A browser may:
- Pick a larger file than needed on a fast connection, or a smaller one in data-saving mode.
- Reuse a file already in its cache instead of downloading a better fit.
- Skip a download when a larger variant is already available.
Because of this, two browsers can pick different files for the same page, and both are correct. Do not write code that assumes a specific file.
Resizing after load
If a user widens the window or rotates the device, the browser may fetch a larger candidate. It does not usually swap back to a smaller one, since the larger file is already downloaded. If you test by resizing, reload the page between runs to see a clean result.
Common errors
- A missing or wrong
sizes. Withwdescriptors and nosizes, the browser assumes100vw. A half-width image then downloads a file far larger than it needs. - Descriptors that do not match the files. If
photo-800.jpg 800wpoints to a file that is really 600 pixels wide, the browser's math is wrong, and the image looks soft. - Mixing
xandwin onesrcset. This is invalid. Use one kind per attribute. - Using
sizeswithxdescriptors.sizesonly matters withwdescriptors, and it is ignored otherwise. - Expecting CSS to inform the choice. A CSS rule such as
img { width: 50% }does not reach the browser at selection time. Mirror your layout insizes.
Exercise
The viewport is 800 CSS pixels wide, the DPR is 2, and the image has sizes="(min-width: 60em) 33vw, (min-width: 40em) 50vw, 100vw" with candidates of 400w, 800w, and 1600w. Which file is the usual choice, and why?
Show answer
800 CSS pixels is at least 40em (640px) but below 60em (960px), so the second condition matches and the slot is 50vw, or 400 CSS pixels. With a DPR of 2, the target is 800 device pixels. The smallest candidate at least that wide is the 800w file.