How does the srcset attribute work?
The Short Answer
The srcset attribute on <img> elements lets you provide multiple image sources at different resolutions or widths, and the browser automatically picks the most appropriate one based on the device's screen size and pixel density. Instead of serving a single image to all devices (wasting bandwidth on phones or looking blurry on retina displays), srcset enables responsive image loading — small images for small screens, high-resolution images for retina displays, all without any JavaScript.
Density Descriptors (x)
Density descriptors tell the browser which image to use based on the device pixel ratio (DPR). A 2x descriptor means that image has twice the pixels of the displayed size — perfect for retina screens. This approach works best for fixed-size images like logos, icons, and avatars where the CSS dimensions don't change across viewports.
<!-- Fixed-size logo: always displayed at 150×50 CSS pixels -->
<img
src="/logo-150.png"
srcset="
/logo-150.png 1x,
/logo-300.png 2x,
/logo-450.png 3x
"
width="150"
height="50"
alt="Company logo"
/>
<!-- How the browser decides:
DPR 1 (standard display) → downloads logo-150.png (150×50 actual pixels)
DPR 2 (retina MacBook) → downloads logo-300.png (300×100 actual pixels)
DPR 3 (iPhone Pro) → downloads logo-450.png (450×150 actual pixels)
The src attribute is the fallback for browsers that don't support srcset.
-->
Width Descriptors (w) + sizes
Width descriptors are more powerful — you tell the browser the actual pixel width of each source image, and the sizes attribute tells it how wide the image will be displayed at different viewport widths. The browser combines this information with the device's DPR to calculate exactly which source to download. This is the approach for responsive images that change size based on the layout.
<!-- Responsive hero image that changes size with viewport -->
<img
src="/hero-800.jpg"
srcset="
/hero-400.jpg 400w,
/hero-800.jpg 800w,
/hero-1200.jpg 1200w,
/hero-1600.jpg 1600w
"
sizes="
(max-width: 640px) 100vw,
(max-width: 1024px) 50vw,
800px
"
alt="Hero banner"
/>
<!-- Reading the sizes attribute:
- Viewport ≤ 640px → image displays at 100% viewport width
- Viewport ≤ 1024px → image displays at 50% viewport width
- Otherwise → image displays at 800px
Browser calculation example (phone, 375px wide, 2x DPR):
Display width = 375px (100vw)
Pixels needed = 375 × 2 = 750
Picks: hero-800.jpg (closest source ≥ 750w)
Browser calculation example (laptop, 1440px wide, 2x DPR):
Display width = 800px (fixed from sizes)
Pixels needed = 800 × 2 = 1600
Picks: hero-1600.jpg
-->
How the Browser Chooses
The browser's selection algorithm considers the viewport width, the device pixel ratio, the sizes attribute, and potentially network conditions. Importantly, the browser has full discretion — it might choose a smaller image on a slow connection even if a larger one would be ideal. This is by design: srcset is a hint, not a command.
- Browser reads the `sizes` attribute to determine the display width of the image
- Multiplies display width by the device pixel ratio to get the ideal source width
- Scans the `srcset` candidates and picks the closest match (usually the smallest source that's ≥ the ideal width)
- May factor in network speed, cached images, or data-saver mode
- Downloads only the chosen source — not all of them
You cannot mix density descriptors (x) and width descriptors (w) in the same srcset. Use density descriptors for fixed-size images and width descriptors for responsive images. If you use width descriptors, the sizes attribute is required — without it, the browser assumes the image is 100vw.
srcset vs the <picture> Element
| Feature | srcset on <img> | <picture> element |
|---|---|---|
| Use case | Same image at different resolutions | Different images for different conditions |
| Who decides | Browser picks the best source | Developer controls which source via media queries |
| Art direction | No — same image, different sizes | Yes — can show different crops/compositions |
| Format switching | No | Yes — serve AVIF/WebP with JPEG fallback |
| Complexity | Simple — one attribute | More markup — source elements + fallback img |
Why Interviewers Ask This
This question tests your understanding of responsive images and performance optimization. Interviewers want to see that you know the difference between density and width descriptors, understand how sizes works with width descriptors, can explain why the browser (not the developer) makes the final choice, and know when to use srcset vs the picture element.
Quick Revision Cheat Sheet
Density (x): Fixed-size images — provide 1x, 2x, 3x versions for different DPRs
Width (w): Responsive images — provide actual pixel widths, browser calculates best fit
sizes attribute: Required with w descriptors — tells browser the display width at each breakpoint
Browser discretion: srcset is a hint — browser may pick smaller image on slow networks
Can't mix: Don't combine x and w descriptors in the same srcset
Fallback: src attribute is used by browsers that don't support srcset