Optimize Responsive Images with srcset and Modern Formats
Overview
Reduce image bytes and layout shifts by selecting image dimensions for the rendered slot and declaring intrinsic width and height.
Match the source to the rendered slot
A large desktop hero image should not be downloaded by a phone when a smaller candidate is sufficient. Use srcset with width descriptors and sizes to describe the expected rendered width. The browser can then choose a suitable resource based on viewport and pixel density.
<img
src="/images/guide-800.webp"
srcset="/images/guide-480.webp 480w,
/images/guide-800.webp 800w,
/images/guide-1400.webp 1400w"
sizes="(max-width: 600px) 100vw, 800px"
width="1400" height="900"
alt="A developer reviewing a responsive layout">
Offer formats with fallbacks
Use picture and source when offering AVIF or WebP with a fallback format. Keep a useful img element as the final source and provide meaningful alternative text. Decorative images should use an empty alt value so they do not add noise to assistive technology.
Control loading priority
Lazy-load below-the-fold images. Do not lazy-load the likely Largest Contentful Paint image; consider fetchpriority="high" when measurement supports it. Always provide width and height attributes or a stable aspect ratio to reserve layout space.
Measure real outcomes
Inspect transferred bytes, decoded dimensions, layout shifts, and LCP on mobile-sized viewports. Avoid converting every image to the same dimensions or quality. A responsive image strategy is a content pipeline decision as much as an HTML decision.
Implementation checklist
- Define the expected behavior and supported environments.
- Keep security, accessibility, and failure states in the design.
- Measure the result with representative user journeys.
- Document operational assumptions and review them after changes.