Sub topic 4
Describing candidates with w descriptors
This chapter assumes you know the img element, src and alt, CSS media queries and viewport units, and the difference between CSS pixels and device pixels. It also assumes you have seen srcset with x descriptors.
What a w descriptor says
An x descriptor says "use this file on a screen with this pixel density." That works when the image is displayed at a fixed size. In a fluid layout, the displayed width changes with the viewport, so density alone is not enough information.
A w descriptor states a fact about the file: its intrinsic width in pixels. The browser later uses that fact to choose a file.
<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">Each candidate is a URL followed by a whitespace and a whole number ending in w. Candidates are separated by commas. Here photo-400.jpg 400w means "this file is 400 pixels wide."
Why the descriptor is not a display width
The w value does not say how wide the image will appear on the page. The browser reads srcset while parsing HTML, often before the CSS has loaded and before layout exists. At that point it cannot measure the image's slot.
The sizes attribute fills that gap. It tells the browser the expected display width in CSS pixels, using media conditions and lengths. The next chapter covers it in detail. For now, remember the division of labor:
srcsetwithwdescribes what the files are.sizesdescribes how wide the slot will be.
If you leave out sizes, the browser assumes 100vw, which is the full viewport width. That is correct for full-bleed images and wasteful for anything smaller, because the browser will download a larger file than the layout needs.
The src attribute still matters
Keep src on the element. Browsers that do not understand srcset use it. In a w setup, it is usually the middle-sized file, a reasonable choice for an unknown device.
Rules and common errors
| Mistake | What happens | Fix |
|---|---|---|
| `w` value differs from the file's real width | The browser does its arithmetic on a wrong number and may choose a file that is too small or too large | Read the real width from the file, and generate variants at exact widths |
| Mixing `x` and `w` in one `srcset` | The whole attribute is invalid and the browser falls back to `src` | Use one descriptor type per `srcset` |
| `w` descriptors with no `sizes` | The browser assumes `100vw` and over-fetches in narrow slots | Add a `sizes` value that matches your layout |
| Decimal or negative values such as `400.5w` | The candidate is invalid | Use positive whole numbers |
| Omitting `src` | Older browsers show nothing, and the markup is invalid for `img` | Always include `src` |
Do not write units other than w here. A value like 400px is not a valid descriptor.
Following the choice
For the example above, suppose the viewport is 800 CSS pixels wide, which is under 60em and over 40em at a 16px font size. The slot is 50vw, which is 400 CSS pixels. On a screen with a pixel density of 2, the browser needs about 800 device pixels, so it compares 800 against the descriptors and can pick photo-800.jpg. On a density-1 screen it needs about 400, so photo-400.jpg is enough. The browser makes the final decision, and it may choose a larger file when the cache already holds one. The selection process gets its own chapter.
Exercise
You have card-480.jpg (480 pixels wide), card-960.jpg (960 pixels wide), and card-1440.jpg (1440 pixels wide). Write the srcset value for an img that uses w descriptors. Then say what the browser assumes if the element has no sizes attribute.
Show answer
srcset="card-480.jpg 480w, card-960.jpg 960w, card-1440.jpg 1440w". Without sizes, the browser assumes the image will be displayed at 100vw, the full viewport width.