Skip to main content
100% Client-Side Browser Execution
Image Optimization • •Updated Nov 25, 2026 •11 min read

Responsive HTML5 picture, srcset, sizes & Automated AVIF Pipelines

Stop serving 2400px desktop images to 390px mobile screens: build an automated Sharp AVIF/WebP pipeline with accurate HTML5 srcset and sizes.

GG
GraphicsGrotto Editorial & Engineering Desk

Specification-checked across Blink, WebKit & Gecko

Converting a 2400×1600 JPEG into WebP helps—but if a visitor on a 390px-wide smartphone inside a two-column mobile card grid still downloads a 2400px asset, you are transmitting 6× more pixels than the screen can physically display.

True production image optimization combines multi-codec format negotiation (<picture>), resolution switching (srcset + sizes), and automated build-time generation (sharp). For quick manual conversions during prototyping, use our client-side Bulk Image to WebP Converter.

1. Codec & Quality Equivalency Reference

AVIF (AV1 Image File Format) uses variable block sizes and directional intra-prediction from the royalty-free AV1 video specification, outperforming both JPEG and WebP particularly on smooth sky/studio gradients and high-compression budgets:

Format Recommended sharp Settings Typical 1600×1000 Photo Size Browser Support (Late 2026)
AVIF { quality: 62, effort: 5, chromaSubsampling: '4:2:0' } 78 KB ~95%+ (Chrome, Safari 16.4+, Firefox, Edge)
WebP { quality: 78, effort: 5 } 128 KB ~98%+ global browsers
MozJPEG { quality: 82, mozjpeg: true } 215 KB 100% universal fallback

2. Automated Node.js sharp Multi-Width Build Script

Run a deterministic Node script before static deployment to emit 480w, 960w, and 1600w candidates across AVIF, WebP, and fallback JPEG:

// scripts/build-responsive-images.mjs
import sharp from 'sharp';
import path from 'node:path';
import fs from 'node:fs/promises';

const WIDTHS = [480, 960, 1600];

export async function generateResponsiveSet(inputPath, outDir) {
  await fs.mkdir(outDir, { recursive: true });
  const stem = path.basename(inputPath, path.extname(inputPath));

  for (const w of WIDTHS) {
    const pipeline = sharp(inputPath).rotate().resize({ width: w, withoutEnlargement: true });

    await Promise.all([
      pipeline.clone().avif({ quality: 62, effort: 5 }).toFile(path.join(outDir, `${stem}-${w}w.avif`)),
      pipeline.clone().webp({ quality: 78, effort: 5 }).toFile(path.join(outDir, `${stem}-${w}w.webp`)),
      pipeline.clone().jpeg({ quality: 82, mozjpeg: true }).toFile(path.join(outDir, `${stem}-${w}w.jpg`)),
    ]);
  }
}

Don't Skip .rotate() Before Stripping Metadata! Calling sharp(input).rotate() with zero arguments reads the camera's EXIF Orientation tag and physically rotates the pixel matrix upright before stripping EXIF metadata—preventing sideways portrait photos!

3. Writing Accurate srcset and sizes Markup

The sizes attribute must tell the browser's preload scanner how wide the image renders on screen before external CSS has even finished downloading! For an article body column capped at 768px (max-w-3xl) with 32px horizontal mobile padding:

<picture>
  <source
    type="image/avif"
    srcset="/img/studio-480w.avif 480w, /img/studio-960w.avif 960w, /img/studio-1600w.avif 1600w"
    sizes="(max-width: 832px) calc(100vw - 32px), 768px"
  />
  <source
    type="image/webp"
    srcset="/img/studio-480w.webp 480w, /img/studio-960w.webp 960w, /img/studio-1600w.webp 1600w"
    sizes="(max-width: 832px) calc(100vw - 32px), 768px"
  />
  <img
    src="/img/studio-960w.jpg"
    srcset="/img/studio-480w.jpg 480w, /img/studio-960w.jpg 960w, /img/studio-1600w.jpg 1600w"
    sizes="(max-width: 832px) calc(100vw - 32px), 768px"
    alt="Color calibration workflow diagram"
    width="1600"
    height="1000"
    loading="lazy"
    decoding="async"
    class="w-full h-auto rounded-xl"
  />
</picture>

Next, make sure your above-the-fold hero image never uses loading="lazy"—see Fixing Hero Image LCP & CLS with fetchpriority.

4. Art Direction vs. Resolution Switching (media vs. sizes)

Developers frequently confuse two distinct capabilities of <picture> and srcset:

  1. Resolution Switching (srcset + sizes, same aspect ratio): A single composition scaled to 480w, 960w, and 1600w. The browser is free to pick whichever candidate fits current screen width, devicePixelRatio, and network Data-Saver settings.
  2. Art Direction (<source media="(max-width: 639px)">, different crops!): A wide 21:9 desktop banner looks tiny and illegible when shrunk onto a 375px portrait phone! Art direction swaps in a tighter 4:3 or 1:1 mobile crop zoomed onto the focal subject.

CLS Warning on Art-Directed <picture>: When your mobile <source> has a 4:3 aspect ratio (800×600) and your desktop <source> has a 16:9 aspect ratio (1600×900), putting width="1600" height="900" only on the fallback <img> causes mobile browsers to reserve a 16:9 box first and then jump to 4:3 when the mobile source loads! Always declare explicit width and height directly on every <source> tag too—supported across all modern HTML5 engines specifically to eliminate art-direction CLS:

<picture>
  <!-- Mobile 4:3 tight crop with its own intrinsic width/height! -->
  <source
    media="(max-width: 639px)"
    type="image/avif"
    srcset="/img/diagram-mobile-480.avif 480w, /img/diagram-mobile-800.avif 800w"
    sizes="calc(100vw - 32px)"
    width="800"
    height="600"
  />
  <!-- Desktop 16:9 wide diagram -->
  <source
    type="image/avif"
    srcset="/img/diagram-desk-960.avif 960w, /img/diagram-desk-1600.avif 1600w"
    sizes="(max-width: 1280px) 92vw, 1180px"
    width="1600"
    height="900"
  />
  <img
    src="/img/diagram-desk-960.webp"
    alt="Browser rendering pipeline stages"
    width="1600"
    height="900"
    loading="lazy"
    decoding="async"
    class="w-full h-auto rounded-xl"
  />
</picture>

5. Verifying Which Candidate Browser Actually Downloaded

Don't inspect img.src in DevTools—on <picture> elements, img.src always prints the fallback JPEG/WebP attribute string even when the browser actually rendered AVIF! Instead, inspect $0.currentSrc in the Console or check the Type column in the Network tab:

// Inspect exact negotiated codec & width variant across every image on page
document.querySelectorAll('img').forEach((img) => {
  console.table({
    renderedBox: `${img.clientWidth}x${img.clientHeight}`,
    intrinsic: `${img.naturalWidth}x${img.naturalHeight}`,
    chosenAsset: img.currentSrc.split('/').pop(),
  });
});

If naturalWidth is more than 2.2× larger than clientWidth * window.devicePixelRatio, tighten your sizes attribute so mobile devices select smaller candidates! Pair responsive sizing with priority hints in our Hero Image LCP & CLS Optimization Guide.

Frequently Asked Technical Questions

Why did Chrome download my 1600w image on a 390px iPhone even though I wrote srcset?

Two reasons: first, iPhones have a 3x devicePixelRatio (390 x 3 = 1170 physical pixels); second, if you omit the sizes attribute, HTML defaults sizes to 100vw! Always declare accurate sizes matching your CSS grid layout.

Should <source type="image/avif"> come before or after <source type="image/webp">?

Always place <source type="image/avif"> FIRST! Browsers select the very first <source> element whose MIME type and media query they support, ignoring subsequent sources.

What Sharp quality settings match JPEG q82 visual fidelity across WebP and AVIF?

As a reliable starting baseline for photographic assets: JPEG quality 82 roughly corresponds to WebP quality 78–80 and AVIF quality 60–65 (with effort 4–6).

Editorial Verification

Maintained by the GraphicsGrotto Editorial & Engineering Desk

Code examples and metrics follow W3C SVG/CSS specifications and browser rendering tests. Spotted an engine change or errata?

More in Image Optimization

View Full Silo →