Responsive Images with srcset and sizes: Complete Guide
Serve the right image size to every device. srcset, sizes, picture element, and art direction for optimal performance.
- Serve the right image size to every device.
- Why Responsive Images Matter.
- Using srcset and sizes.
- The picture Element.
- Lazy Loading and Performance.
Why Responsive Images Matter
A single image canβt serve every device well. A hero image for a 2560px Retina display is 4x larger than what a 375px phone needs. Without responsive images, mobile users download massive files they canβt benefit from. Responsive images let the browser choose the most appropriate source from candidates. The average web page is over 50% images by weight β optimizing them is the single biggest performance lever for most sites.
Using srcset and sizes
The srcset attribute provides image sources at different widths: srcset="hero-400.webp 400w, hero-800.webp 800w, hero-1200.webp 1200w". The sizes attribute tells the browser how wide the image displays at each viewport: sizes="(max-width: 768px) 100vw, 50vw". The browser picks the smallest image that covers the display at current viewport and pixel density. Use the Aspect Ratio Calculator to determine proper dimensions.
The picture Element
The picture element provides art direction β different crops for different screens, not just different resolutions. Itβs also used for format negotiation: serve AVIF to supporting browsers, WebP to the rest, JPEG as universal fallback. Combine both uses for maximum optimization β different formats AND different crops based on viewport width.
Lazy Loading and Performance
Always include loading="lazy" on images below the fold. Donβt lazy-load your LCP hero image β it needs to load immediately. Add fetchpriority="high" to your hero. Always set width and height attributes to prevent CLS. The browser reserves correct space before the image loads, eliminating the layout jump when it appears.
Frequently Asked Questions
Do I need to generate all image sizes myself?
What sizes should I generate?
Does srcset affect SEO?
Use the Aspect Ratio Calculator β free, no signup required.
β‘ Open Aspect Ratio Calculator