Responsive Images: srcset, sizes, picture and image-set()
Serving the right image for each screen: fluid images in CSS, width and height to prevent layout shifts, srcset with x and w descriptors, sizes and sizes="auto", picture for art direction and formats, loading priority, and image-set() for backgrounds.
kvi5hnuCode Sprout team member8 min readOn this page
Images are usually the heaviest part of a page. A photo sized for a 27-inch screen can be ten times larger than a phone needs, and a phone with a high-density screen needs twice the width in pixels of an old laptop, four times the pixels in all. Responsive images let the browser choose the right file for each device, and keep the layout stable while it loads. Most of this is HTML, but CSS sets how big images are displayed, and image-set() does the same job for CSS backgrounds.
Fluid images in CSS
By default an image is displayed at its natural size, so a large photo overflows a narrow column. A rule most resets include:
img, video, svg {
max-inline-size: 100%; /* never wider than the container */
block-size: auto; /* keep the aspect ratio when the width shrinks */
display: block; /* no gap below (article 45) */
}
block-size: auto matters: with only max-width, an image with a height attribute in HTML would be squashed when its width shrinks.

The second panel is the bug block-size: auto prevents: the height attribute still applies after the width shrinks.
Preventing layout shifts
Until an image loads, the browser doesn't know its size, so it reserves no space, and the content below jumps down when it arrives. That's a layout shift, measured by Core Web Vitals as Cumulative Layout Shift. Always give <img> its width and height attributes, in pixels of the image's ratio:
<img src="harbor-800.jpg" width="1600" height="900" alt="Fishing boats in the harbor at dawn">
Browsers turn the two attributes into a default aspect-ratio: auto 1600 / 900 (article 38). With the CSS above, the image scales to its column, and its box has the right height before a single byte arrives. The attributes don't need to match the displayed size, only the ratio.
For images whose ratio is set by the design instead, such as cards with cropped thumbnails, use aspect-ratio and object-fit: cover in CSS.
srcset and sizes
Density descriptors: x
For an image that's always shown at the same CSS size, such as a 200px logo, offer versions for each pixel density:
<img src="logo-200.png" srcset="logo-200.png 1x, logo-400.png 2x, logo-600.png 3x"
width="200" height="60" alt="Harbor Books">
The browser picks by the screen's device pixel ratio: a typical phone at 3x gets logo-600.png. For logos and icons, SVG is usually better: one file, sharp at every density.
Width descriptors: w and sizes
For images whose displayed size changes with the layout, list each file's real width in pixels with w, and tell the browser how wide the image will be displayed with sizes:
<img
src="harbor-800.jpg"
srcset="harbor-400.jpg 400w, harbor-800.jpg 800w, harbor-1600.jpg 1600w, harbor-2400.jpg 2400w"
sizes="(width >= 60rem) 50vw, 100vw"
width="2400" height="1350"
alt="Fishing boats in the harbor at dawn">
sizes is a list of media conditions with lengths, like a small set of media queries: the first matching condition wins, and the last length, without a condition, is the default. Here: half the viewport on wide screens, the full width otherwise. The browser multiplies the slot width by the pixel density and picks the closest suitable file. On a 390px-wide phone at 3x, the image needs about 1170 device pixels, so it gets harbor-1600.jpg.
Why sizes is needed: the browser chooses images while it parses the HTML, before it has downloaded the CSS or laid out the page, so it can't know how wide the image will be. If you leave sizes out, it's 100vw, so images in narrow columns download far too large.

The browser does this arithmetic before any CSS is loaded, which is why sizes has to describe the layout in advance.
sizeslengths can bevw,px,remorcalc(), such ascalc(100vw - 2rem), but not percentages.- The browser may also choose a smaller file to save data, or reuse a larger one already in its cache.
srcsetgives it options; it doesn't force a choice. srcis the fallback for browsers withoutsrcset, and counts as the 1x candidate whensrcsetusesxdescriptors and lists no 1x file.
sizes="auto"
For images with loading="lazy", the browser has done the layout before it loads the image, so it can measure the slot itself. sizes="auto" tells it to:
<img loading="lazy" sizes="auto, (width >= 60rem) 50vw, 100vw" srcset="..." width="2400" height="1350" alt="...">
It works in Chrome 126 and Firefox 150, and partly in Safari 27; the fallback list after auto covers other browsers. The image needs a CSS width or width and height attributes, since its slot must have a size before the image loads.
Checking which file was chosen
In DevTools, select the image and read currentSrc in the console ($0.currentSrc), or check the Network panel. Test with the cache disabled and with device emulation at different pixel ratios: a browser that already has a larger file cached may reuse it, which hides mistakes in sizes.
<picture>: art direction and formats
<picture> wraps an <img> with <source> elements. The browser uses the first source that matches, and the <img> is both the fallback and the element that's actually displayed and styled.
Art direction, different crops for different layouts, uses media:
<picture>
<source media="(width >= 50rem)" srcset="harbor-wide.jpg" width="2400" height="1000">
<source media="(width >= 30rem)" srcset="harbor-medium.jpg" width="1600" height="1000">
<img src="harbor-tall.jpg" width="900" height="1200" alt="Fishing boats in the harbor at dawn">
</picture>
Each source can have its own width and height, so the reserved ratio changes with the crop.
Formats use type: the browser takes the first format it supports.
<picture>
<source type="image/avif" srcset="harbor-800.avif 800w, harbor-1600.avif 1600w" sizes="100vw">
<source type="image/webp" srcset="harbor-800.webp 800w, harbor-1600.webp 1600w" sizes="100vw">
<img src="harbor-800.jpg" srcset="harbor-800.jpg 800w, harbor-1600.jpg 1600w" sizes="100vw"
width="1600" height="900" alt="Fishing boats in the harbor at dawn">
</picture>
AVIF and WebP work in every current browser, so a JPEG fallback mostly matters for very old ones. AVIF is usually the smallest for photos.
Style the <img>, not the <picture>: <picture> is an inline wrapper with no special rendering.
Loading priority
| Attribute | Effect |
|---|---|
loading="lazy" | delays loading until the image is near the viewport; use it for images below the first screen |
loading="eager" (default) | loads right away |
fetchpriority="high" | loads it before other images; use it on the main above-the-fold image, often the Largest Contentful Paint |
decoding="async" | lets the browser show other content without waiting for this image to decode |
Don't lazy-load the main hero image: it delays the most important image on the page.
image-set() for CSS backgrounds
srcset is for <img>. For background images, image-set() offers alternatives by resolution or format:
.hero {
background-image: image-set(
"harbor.avif" 1x type("image/avif"),
"harbor-2x.avif" 2x type("image/avif"),
"harbor.jpg" 1x,
"harbor-2x.jpg" 2x
);
background-size: cover;
}
- Resolutions use
x(ordppx);type()lets the browser skip formats it can't decode. image-set()has no width descriptors. To switch background sizes by viewport width, use media queries:
.hero { background-image: url("harbor-800.jpg"); }
@media (width >= 50rem) {
.hero { background-image: url("harbor-1600.jpg"); }
}
Unprefixed image-set() works in all browsers since Safari 17 (2023), and is Baseline widely available since March 2026. Remember that background images have no alternative text (article 48): images with meaning should be <img>.
What's current
srcset,sizesand<picture>work everywhere.sizes="auto"for lazy-loaded images: Chrome 126, Firefox 150, partly Safari 27.fetchpriority: in all browsers since 2024.image-set()unprefixed, withtype(), is Baseline widely available since March 2026.- AVIF and WebP are supported in every current browser.
Common questions
Why does the browser download a larger image than I expected?
Usually sizes is missing, so it assumes 100vw, or the screen has a high pixel density, which multiplies the needed width. Browsers may also reuse a larger cached file.
Do I still need width and height attributes if CSS sets the size?
Yes. They give the browser the aspect ratio before the image loads, which prevents layout shifts. CSS then decides the displayed size.
When should I use <picture> instead of srcset?
Use srcset and sizes for the same image at different resolutions. Use <picture> when you need different crops (art direction) or different formats.
Should I lazy-load every image?
No. Lazy-load images below the first screen. Load the main visible image eagerly, with fetchpriority="high" if it's the largest element.
How do I do responsive background images?
image-set() for resolution and format, and media queries for different sizes.
Why is my image blurry on my phone but sharp on my laptop?
The phone has more device pixels per CSS pixel, often 2 or 3, so a file exactly as wide as the displayed size is stretched. Offer larger files with srcset (width or x descriptors) and a sizes value that matches the displayed width, so the browser can pick one with enough pixels.
Summary
max-inline-size: 100%; block-size: automakes images fluid.widthandheightattributes reserve their space and prevent layout shifts.srcsetwithxdescriptors serves pixel densities; withwdescriptors andsizes, it serves layout widths.sizesdescribes the image's display width, because the browser picks before layout.sizes="auto"measures it for lazy images.<picture>withmediagives art direction, and withtypeoffers formats like AVIF.loading="lazy"for images further down,fetchpriority="high"for the main image.image-set()does resolution and format switching for CSS backgrounds.
Practice questions
An image fills the full width on phones and a third of the viewport from 64rem up. Files are 480, 960 and 1920 pixels wide. Write the srcset and sizes.
Answer
<img src="photo-960.jpg"
srcset="photo-480.jpg 480w, photo-960.jpg 960w, photo-1920.jpg 1920w"
sizes="(width >= 64rem) 33vw, 100vw"
width="1920" height="1280" alt="…">
A card grid reserves no space for images, and the text jumps as they load. What's the fix?
Answer
Add width and height attributes with the images' ratio, and keep block-size: auto in CSS. For a fixed design ratio, set aspect-ratio on the images, with object-fit: cover.
On a 400px-wide phone with a pixel ratio of 2, which file does srcset="a-400.jpg 400w, a-800.jpg 800w, a-1600.jpg 1600w" sizes="100vw" most likely load?
Answer
a-800.jpg. The slot is 400 CSS pixels × 2 = 800 device pixels, so the 800w file fits.
Write a background that uses hero.avif where it's supported and hero.jpg otherwise.
Answer
.hero { background-image: image-set("hero.avif" type("image/avif"), "hero.jpg" type("image/jpeg")); }
Review quiz
Test yourself on Part 7 with the CSS Part 7 review quiz.
Previous: Shadows: box-shadow, text-shadow and Designing Depth

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.