Frontend System Design · Lesson 45 of 86 · 4 min
Responsive Images: srcset, sizes and picture
In an interview, this topic separates developers who just write markup from those who engineer performant user experiences.
How to handle resolution switching with srcset
The simplest way to provide responsive images is with the srcset attribute on an <img> tag. It lets the browser choose the best image from a list you provide, based on the user's screen density and the image's display size.
You define sources using either a pixel density descriptor (x) or a width descriptor (w).
2xdescriptor:srcset="image-1x.jpg 1x, image-2x.jpg 2x". Use this for fixed-size images, like icons or logos, that need to be sharp on high-DPI (Retina) displays. It's simple but inflexible for fluid layouts.wdescriptor:srcset="image-400w.jpg 400w, image-800w.jpg 800w". This is what you'll use for most images in a responsive layout. You provide the intrinsic width of each image file in pixels.
The w descriptor is more powerful because it decouples the image choice from the device's pixel ratio. Instead, the browser can make a smarter decision based on the actual space the image will occupy.
Why sizes is the critical part
When you use w descriptors in srcset, you must also provide the sizes attribute. It's not optional for getting this right.
The sizes attribute tells the browser how wide the image will be at different viewport widths. You write it as a list of media conditions paired with slot widths.
<img
src="images/product-default.jpg"
alt="A product shot"
srcset="
images/product-400.jpg 400w,
images/product-800.jpg 800w,
images/product-1200.jpg 1200w,
images/product-1600.jpg 1600w
"
sizes="(max-width: 600px) 100vw, 50vw"
/>
Here’s how the browser reads that sizes attribute:
- Is the viewport 600px wide or less? If yes, the image will occupy
100vw(100% of the viewport width). - Otherwise (for any viewport wider than 600px), the image will occupy
50vw.
The browser then uses this information to pick the best image from srcset. For example, on a 1200px wide screen, the image slot is 50vw (600px). On a 2x DPI screen, it needs a 1200px source image, so it will likely download product-1200.jpg.
The trap: Omitting sizes when using w descriptors. If you forget it, the browser defaults to sizes="100vw". If your image is in a 50% width column on a large screen, the browser will still assume it's full-width and download a much larger image than necessary, wasting bandwidth.
When to reach for <picture>
Use the <picture> element when srcset alone isn't enough. It gives you more explicit control. It has two primary use cases: art direction and format switching.
Art direction is when you want to serve a different image, not just a smaller version of the same one. For example, a wide landscape shot on desktop might be a tight, portrait crop on mobile to keep the subject visible.
Format switching is for serving modern image formats like AVIF or WebP to browsers that support them, with a fallback to JPEG for older browsers. This is a huge performance win, as modern formats offer much better compression.
The <picture> tag wraps multiple <source> elements and a final <img> fallback. The browser evaluates each <source> in order and uses the first one that matches.
In an interview, frame this as a trade-off. The
<picture>element is more verbose and complex than a simple<img>tag. You use it when the performance gains from format switching or the UX benefits of art direction justify the extra markup.
<picture>
<!--
Serve AVIF to supporting browsers. This is the most modern format.
The 'type' attribute lets the browser quickly check for support.
-->
<source type="image/avif" srcset="photo.avif" />
<!-- If not, try WebP. -->
<source type="image/webp" srcset="photo.webp" />
<!--
The 'img' tag is the mandatory fallback. It's also what's used by
browsers that don't support 'picture' at all.
-->
<img
src="photo.jpg"
alt="A colorful abstract image"
loading="lazy"
decoding="async"
/>
</picture>
How the browser decides which image to load
Understanding the browser's logic is key to debugging and explaining your design choices.
For an <img> with srcset and sizes, the process is an algorithm:
- Check the viewport width.
- Evaluate the
sizesattribute to find the first matching media condition. - Use the corresponding slot width (e.g.,
50vw,300px) to calculate the image's display width in CSS pixels. - Multiply this by the device pixel ratio to get the desired image resolution in physical pixels.
- Look at the
wdescriptors insrcsetand download the image that is closest to, but not smaller than, this desired resolution.
This allows the browser to download an image that is just right—not wastefully large, but sharp enough for the screen.
For <picture>, the logic is simpler: it's a switch statement. The browser goes through the <source> elements from top to bottom. The very first one whose media or type attribute matches the current environment is chosen, and its srcset is used. All subsequent <source> elements are ignored. If none match, the fallback <img> is used.
Try it
Take a simple HTML page with a few static <img> tags. Convert one of them to be responsive. Create three versions of the image (e.g., 400px, 800px, 1200px wide). Write the srcset and a sizes attribute that assumes the image is full-width on mobile but in a two-column layout on desktop. Open your browser's dev tools, go to the network tab, and resize the viewport to see which image gets downloaded at different sizes.
Next: Learn about the file formats themselves in "Modern Image Formats: WebP, AVIF, JPEG XL".
Lock it in — apply this lesson under interview conditions.