Responsive images: srcset, sizes and picture
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Serve an appropriately sized image for the viewport and pixel density.
- Use picture for art direction and modern formats.
Overview
#A single large image wastes bandwidth on small screens. srcset offers the browser a set of candidates and a hint about the rendered width, and the browser picks the best one. sizes tells the browser how wide the image will be at each viewport, which is the missing piece that makes the choice accurate.
<img
src="/hero-800.jpg"
srcset="/hero-400.jpg 400w, /hero-800.jpg 800w, /hero-1600.jpg 1600w"
sizes="(min-width: 60rem) 50vw, 100vw"
width="1600"
height="900"
alt="A team reviewing a design on a large screen"
/>
The w descriptors are intrinsic widths. The sizes attribute describes the layout width as a percentage of the viewport at each breakpoint. Together they let the browser compute which file gives enough pixels for the current device pixel ratio without downloading all of them.
Always include width and height attributes. They let the browser reserve space before the image loads, which prevents layout shift. Modern CSS can make them responsive without distortion:
img { max-inline-size: 100%; height: auto; }
Use the picture element for art direction or format negotiation, where the choice depends on something other than width. It wraps one or more source elements and a fallback img.
<picture>
<source type="image/avif" srcset="/hero.avif" />
<source type="image/webp" srcset="/hero.webp" />
<img src="/hero.jpg" alt="A team reviewing a design" width="1600" height="900" />
</picture>
The browser uses the first supported source, so order matters. The img is mandatory: it is the fallback and it carries the alt text. Lazy-load images that are below the fold with loading="lazy", but never lazy-load the largest above-the-fold image, because that delays the main content.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Write an img that offers 480w, 960w and 1440w candidates, occupies full width below 60rem and half the viewport at and above it, and reserves its aspect ratio.
starter.html<img src="/img-960.jpg" alt="A mountain range at dawn" />Reveal the reference answer
reference.html<img src="/img-960.jpg" srcset="/img-480.jpg 480w, /img-960.jpg 960w, /img-1440.jpg 1440w" sizes="(min-width: 60rem) 50vw, 100vw" width="1440" height="810" alt="A mountain range at dawn" />Authored expected output: The browser can choose among three widths using the layout hint and space is reserved via width and height.
srcset lists candidates, sizes describes layout width, and width plus height prevent layout shift.
Exercise 2Debug
This picture element shows a broken image in browsers without AVIF. Fix it.
starter.html<picture> <source type="image/avif" srcset="/hero.avif" /> </picture>Reveal the reference answer
reference.html<picture> <source type="image/avif" srcset="/hero.avif" /> <img src="/hero.jpg" alt="A team reviewing a design" width="1600" height="900" /> </picture>Authored expected output: A fallback img element provides a source and alt text when no source matches.
picture requires a trailing img as the fallback and the accessible alternative.
Common misconceptions
#- srcset alone is enough; sizes is optional.
- The picture element replaces img.
Follow-up practice
#- html-css-26-intrinsic-layout-patterns
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.