Responsive Images in HTML & CSS: The Complete & srcset Guide
Responsive images optimize page load speed and Core Web Vitals (LCP) by serving device-tailored image assets. Use srcset with width descriptors (hero-800.webp 800w, hero-1600.webp 1600w) and the sizes attribute (sizes="(max-width: 768px) 100vw, 50vw") for resolution switching. Use the HTML5 <picture> element when you need art direction (serving different aspect-ratio crops on mobile vs desktop) or next-gen format negotiation (AVIF → WebP → JPEG).
Step 1: Resolution Switching with srcset and width descriptors (w)
Never serve a 2400px wide image to a 400px mobile screen. Define available file resolutions in srcset paired with the width in pixels (e.g. 800w), allowing the browser engine to pick the optimal file based on screen density (DPR) and network conditions.
<img src="photo-800.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1600.jpg 1600w"
sizes="(max-width: 640px) 100vw,
(max-width: 1024px) 50vw,
33vw"
alt="Architectural bridge suspension"
loading="lazy"
decoding="async">
Step 2: De-mystifying the sizes Attribute
The sizes attribute tells the browser how wide the image will display *before* CSS styles are downloaded. Without sizes, the browser assumes the image is 100vw (full width) and downloads unnecessarily large files on desktop grids.
<!-- If screen <= 768px, image takes full viewport width (100vw) -->
<!-- Otherwise, image takes 1200px max container width -->
sizes="(max-width: 768px) 100vw, 1200px"
Step 3: Format Negotiation: AVIF -> WebP -> JPEG with
Wrap multiple <source> elements inside a <picture> tag. Modern browsers parse top-to-bottom and download the most advanced format supported, dropping payload size by up to 75%.
<picture>
<!-- Next-Gen AVIF: Superior compression -->
<source type="image/avif" srcset="hero.avif">
<!-- Modern WebP: Universal modern support -->
<source type="image/webp" srcset="hero.webp">
<!-- Legacy JPEG Fallback -->
<img src="hero.jpg" alt="Modern city skyline" width="1200" height="675">
</picture>
Step 4: Eliminating Layout Shift (CLS) with aspect-ratio
Always specify native width and height HTML attributes, or use CSS aspect-ratio: 16 / 9; width: 100%; height: auto; so the browser reserves exact layout space before the image streams in.
img, picture {
display: block;
max-width: 100%;
height: auto;
aspect-ratio: 16 / 9;
object-fit: cover;
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Whenever I design responsive widgets or fluid typography, I rely heavily on clamp() and container queries (@container). Testing down to 320px viewport widths in Chrome DevTools will instantly reveal edge cases before your users see them.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the difference between resolution switching and art direction?
Resolution switching serves the same image content at different file resolutions (sizes/srcset) to match device pixel density. Art direction serves different crops or completely different compositions (via `
Why should I always specify width and height attributes on the
tag?
Modern browsers compute the intrinsic aspect ratio from the HTML `width` and `height` attributes before downloading the image, reserving layout space and preventing Cumulative Layout Shift (CLS).
What happens if a browser does not support AVIF or WebP in a element?
The browser ignores `` fallback tag, ensuring 100% backward compatibility for all browsers.
Why is loading="lazy" not recommended for hero banner images?
Hero images are visible in the above-the-fold viewport upon page load. Adding `loading="lazy"` delays their download until layout calculation completes, hurting your Largest Contentful Paint (LCP) score. Only lazy load below-the-fold images.
How does fetchpriority="high" improve LCP for hero images?
Declaring `fetchpriority="high"` on your primary hero image signals the browser to schedule its HTTP download ahead of non-critical stylesheets, fonts, and scripts.