Responsive images with srcset and sizes in HTML
38 Let the browser choose the file size
hardOffer the same photograph at three widths and describe how much of the layout it will occupy, so the browser can pick the smallest file that still looks sharp.
Stuck? Three hints, smallest first
- 1.
srcsetlists candidates with awdescriptor: the real pixel width of each file. - 2.
sizesdescribes the rendered width in CSS terms, and the browser needs it before layout, which is why it cannot work this out itself. - 3. With no
sizes, the browser assumes100vwand usually downloads too much.
<img
class="photo"
srcset="bus-480.jpg 480w, bus-960.jpg 960w, bus-1440.jpg 1440w"
sizes="(min-width: 44em) 640px, 100vw"
src="bus-960.jpg"
width="960" height="540"
alt="A coach at bay B with its luggage doors open">
/* No photographs ship with this course, so the file names above resolve to
nothing and the preview shows the styled box with its alt text. The markup is
the subject; the box is here so there is something to look at. */
.photo {
display: block;
width: 100%;
max-width: 280px;
aspect-ratio: 16 / 9;
object-fit: cover;
border: 1px dashed #8e8e98;
border-radius: 8px;
background: #f2f3f5;
color: #5c5c66;
padding: 8px;
box-sizing: border-box;
font-size: 0.78rem;
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// Pull the candidate list apart and check it is coherent.
const candidates = (/srcset="([^"]+)"/.exec(html) || [, ''])[1]
.split(',')
.map((c) => c.trim().split(/\s+/))
.map(([file, descriptor]) => ({ file, width: Number(descriptor.replace('w', '')) }));
const sizes = (/sizes="([^"]+)"/.exec(html) || [, ''])[1];
test('three candidates are offered', candidates.length, 3);
test('their widths are 480, 960 and 1440', candidates.map((c) => c.width), [480, 960, 1440]);
test('the list is in ascending order, which is how it should read', candidates.map((c) => c.width).slice().sort((a, b) => a - b), [480, 960, 1440]);
test('every candidate carries a w descriptor', candidates.every((c) => c.width > 0), true);
test('sizes describes the rendered width, with a fallback last', /100vw$/.test(sizes), true);
test('and a wider case before it', /\(min-width: 44em\) 640px/.test(sizes), true);
test('a plain src is kept for browsers that ignore srcset', /src="bus-960\.jpg"/.test(html), true);
test('intrinsic dimensions and alt text are still present', /width="960" height="540"/.test(html) && /alt="[^"]{15,}"/.test(html), true);
Output
srcset alone is not enough: with no sizes the browser assumes the image will fill the viewport and picks the largest candidate, which is the opposite of the intent. sizes has to be written by hand because the browser chooses the file before it has laid the page out, so it cannot measure the slot — and that also means sizes is a promise you have to keep in step with your CSS. The last entry acts as the fallback, so it goes last with no condition. The file names here resolve to nothing, since this course ships no photographs; the checks are on the markup, which is the part being taught.