Sub topic 5
Telling the Browser How Wide the Image Will Be with sizes
This chapter assumes you know basic HTML and CSS, including media queries and viewport units, and that you have read the chapter on w descriptors. It covers the sizes attribute, which pairs with srcset when image width changes with the layout.
Why the browser needs sizes
A w descriptor tells the browser how wide each file is. It does not say how wide the image will appear on the page. The browser picks a file while it parses the HTML, before CSS has been applied and before layout runs. Without a hint, it assumes the image fills the whole viewport (100vw).
The sizes attribute supplies that hint. It describes the slot the image will occupy, in CSS pixels, at different viewport widths.
The syntax
sizes is a comma-separated list. Each entry except the last is a media condition followed by a length. The last entry is a default length with no condition.
<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">The browser reads the list from left to right and uses the length from the first condition that matches:
| Viewport width | Matching entry | Slot width |
|---|---|---|
| 60em or wider | `(min-width: 60em) 33vw` | one third of the viewport |
| 40em up to 60em | `(min-width: 40em) 50vw` | half of the viewport |
| Under 40em | `100vw` | the full viewport |
Order matters. If you put (min-width: 40em) first, it also matches wide screens and the 33vw entry is never reached. List min-width conditions from largest to smallest.
Lengths you can use
A length can be vw units, px, em, or a calc() expression. You cannot use percentages, because the layout width is not known yet. Use calc() when the slot has fixed padding:
sizes="(min-width: 50em) calc(50vw - 2rem), calc(100vw - 2rem)"Keep sizes in step with your CSS
The browser trusts sizes and does not check it against the real layout. If your CSS makes the image 400px wide but sizes says 100vw, the browser downloads a larger file than it needs. If sizes claims a smaller slot than the CSS produces, the image looks blurry. Whenever you change the layout breakpoints or column widths, update sizes to match.
A worked example
Suppose the viewport is 1000 CSS pixels wide and the screen has a device pixel ratio of 2. The example above has 60em equal to 960px, so the first condition matches and the slot is 33vw, which is 330px. Multiplying by the pixel ratio gives a target of 660 device pixels. The candidates are 400w, 800w, and 1600w, so the browser picks photo-800.jpg, the smallest file that covers 660 pixels. On a 1x screen the target is 330 pixels, so it picks photo-400.jpg.
Common errors
- Leaving out
sizesand getting oversized downloads on layouts where the image is narrow. - Writing the conditions in the wrong order, so a broad condition hides narrower ones.
- Using
%in a length, which makes the entry invalid. - Changing the CSS layout without updating
sizes.
Exercise
An image sits in a layout that is full width below 50em and fills half the viewport at 50em and above. Write the sizes attribute for it.
Show answer
sizes="(min-width: 50em) 50vw, 100vw". The condition for the wide layout comes first, and the last entry is the default for narrower viewports.
Exercise
A page has srcset="a-400.jpg 400w, a-800.jpg 800w, a-1600.jpg 1600w" and sizes="50vw". The viewport is 1200 CSS pixels wide and the device pixel ratio is 2. Which file does the browser choose?
Show answer
The slot is 600 CSS pixels, so the target is 1200 device pixels. The smallest file at least that wide is a-1600.jpg.