pictuary

← Glossary

web performance and seo

srcset attribute

The srcset attribute enables responsive images by letting browsers choose optimal image sizes for each device automatically.

What is srcset attribute?

The srcset attribute is an HTML attribute that provides browsers with multiple image candidates at different widths or pixel densities, allowing automatic selection of the most appropriate version for each device. Without srcset, every device downloads the same image file regardless of screen size, wasting bandwidth when phones download desktop-sized images. The browser uses width descriptors (like 480w, 800w, 1200w) paired with the sizes attribute to calculate which image version matches the device's display requirements.

Importance of srcset attribute

For Pictuary users optimizing images for web and social media, the srcset attribute prevents bandwidth waste and improves loading performance across devices. Without responsive images, a compressed 1920px wide image still wastes data when viewed on a 390px mobile screen. Implementing srcset with properly resized image versions ensures each device downloads only the pixels it can actually display, reducing page weight and improving user experience.

srcset attribute in Practice

A photography blog displays hero images that render 800px wide on desktop but full-width on mobile devices. The srcset attribute lists three versions: hero-480w.jpg (15KB), hero-800w.jpg (45KB), and hero-1200w.jpg (80KB). On a standard mobile device, the browser selects the 480px version, downloading 15KB instead of 80KB. On a retina display rendering the same 400px slot, the browser chooses the 800px version to provide crisp quality at 2× pixel density.

srcset attribute Best Practices

  • → Include width descriptors for at least three image sizes covering mobile, tablet, and desktop breakpoints.
  • → Pair srcset with the sizes attribute to tell browsers how wide the image renders at different viewport sizes.
  • → Generate image candidates that increase by 25-50% increments rather than doubling to provide granular options.
  • → Test srcset implementation with browser developer tools to verify correct image selection across device types.

Example of srcset attribute

An e-commerce product image uses srcset="product-400w.jpg 400w, product-600w.jpg 600w, product-900w.jpg 900w" with sizes="(max-width: 600px) 100vw, 50vw". On a 375px mobile screen, the browser calculates it needs a 375px image and selects product-400w.jpg. On a 1920px desktop where the image renders 960px wide, the browser selects product-900w.jpg to avoid upscaling artifacts.

Related Terms

Picture elementRetina display / HiDPIPixel dimensionsUpscalingLCP (Largest Contentful Paint)Page weightResize

Frequently Asked Questions

What is the srcset attribute in HTML?

The srcset attribute is an HTML attribute that lists multiple versions of the same image at different widths or pixel densities, enabling browsers to automatically select the most appropriate version for each device. It prevents bandwidth waste by ensuring mobile devices don't download unnecessarily large desktop images. The browser uses width descriptors and the device's screen characteristics to make the selection without requiring JavaScript.

What is the difference between srcset and picture element?

The srcset attribute provides image suggestions that browsers can override based on network conditions, while the picture element gives explicit commands about which source to use. Use srcset for resolution switching (serving different sizes of the same image) and picture for format switching (AVIF to WebP to JPEG fallbacks) or art direction (different crops for different screens). Both can be combined for maximum flexibility.

How does the browser choose which srcset image to download?

The browser multiplies the image's rendered width (from the sizes attribute) by the device's pixel density ratio to calculate how many physical pixels it needs, then selects the smallest srcset candidate that covers that requirement. On a 2× retina display rendering a 400px wide image, the browser needs 800 physical pixels and chooses the smallest srcset option that's at least 800px wide. The browser can override this selection based on network speed or data-saving preferences.