srcset

Responsive images: provide multiple image sources for different screen sizes and pixel densities. Works with sizes for resolution switching and picture for art direction.

Overview

The srcset attribute lets you provide multiple image files at different sizes or resolutions so the browser can pick the best one for the current viewport and device pixel ratio. It replaces the old pattern of serving a single oversized image to every device.

There are two approaches: width descriptors (with the sizes attribute) for resolution switching, and density descriptors for fixed-size images at different pixel ratios. For entirely different crops at different breakpoints, use the <picture> element for art direction.

Applies to: <img>, <source> (inside <picture>)

Values

DescriptorSyntaxUse Case
Width (w)photo-800.jpg 800wBrowser picks based on viewport width + sizes
Density (x)logo@2x.png 2xFixed-size images at different pixel ratios (icons, logos)

You cannot mix w and x descriptors in the same srcset.

Width Descriptors + sizes

For content images that scale with the layout, use width descriptors. The sizes attribute tells the browser how wide the image will render at each breakpoint, so it can calculate which file to download before CSS is parsed.

<!-- Width descriptors: browser picks the best file based on viewport --> <img src="photo-400.jpg" srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w" sizes="(min-width: 768px) 50vw, 100vw" alt="Landscape photo" />

The sizes value (min-width: 768px) 50vw, 100vw means: above 768px the image occupies 50% of the viewport; below that, 100%. The browser multiplies this by the device pixel ratio to pick a file.

Common Mistake

Using srcset with width descriptors but omitting sizes causes the browser to assume sizes="100vw". On a page where the image is only 50% wide, the browser downloads a file twice as large as needed.

Density Descriptors

For fixed-size images (logos, icons, avatars), density descriptors are simpler. The browser matches the descriptor to the device pixel ratio.

<!-- Density descriptors: match device pixel ratio --> <img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x, logo@3x.png 3x" alt="Company logo" />

Art Direction with picture

When you need different crops or aspect ratios at different breakpoints (not just different resolutions of the same image), use <picture> with <source media="..."> elements. The browser picks the first <source> whose media query matches.

<!-- Art direction: different crops for different breakpoints --> <picture> <source media="(min-width: 1024px)" srcset="hero-wide-800.jpg 800w, hero-wide-1600.jpg 1600w" sizes="100vw" /> <source media="(min-width: 640px)" srcset="hero-medium-600.jpg 600w, hero-medium-1200.jpg 1200w" sizes="100vw" /> <img src="hero-narrow-400.jpg" srcset="hero-narrow-400.jpg 400w, hero-narrow-800.jpg 800w" sizes="100vw" alt="Mountain landscape" /> </picture>

Format Negotiation

Combine <picture> with type to serve modern formats (AVIF, WebP) with JPEG fallback. The browser picks the first format it supports.

<!-- Modern formats with fallback --> <picture> <source type="image/avif" srcset="photo-400.avif 400w, photo-800.avif 800w" /> <source type="image/webp" srcset="photo-400.webp 400w, photo-800.webp 800w" /> <img src="photo-400.jpg" srcset="photo-400.jpg 400w, photo-800.jpg 800w" sizes="(min-width: 768px) 50vw, 100vw" alt="Vacation photo" /> </picture>

Accessibility

  • The alt attribute is still required on the <img> element. It applies regardless of which source the browser selects.
  • Screen readers announce the same alt text no matter which resolution loads, so ensure it describes the content, not the file size.
  • Art direction changes (e.g., cropping out context at mobile sizes) may lose information relevant to the alt text. Review alt descriptions for all crops.

Limitations

  • The browser's source selection is a hint, not a guarantee. It may cache a higher-resolution image from a previous visit and reuse it.
  • You cannot control which image the browser selects via JavaScript; the selection algorithm is internal.
  • srcset does not work on CSS background-image. Use image-set() in CSS for similar resolution switching.
  • Width descriptors require accurate sizes values. If your layout changes via CSS later, the sizes may become stale.
  • Density descriptors are limited to fixed-size images. For fluid layouts, width descriptors are the correct choice.

See Also

  • loading — lazy loading and fetch priority for images
  • <img> element reference
  • <picture> element reference
  • poster — placeholder images for video elements