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! Callingsharp(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:
- Resolution Switching (
srcset+sizes, same aspect ratio): A single composition scaled to480w,960w, and1600w. The browser is free to pick whichever candidate fits current screen width,devicePixelRatio, and network Data-Saver settings. - Art Direction (
<source media="(max-width: 639px)">, different crops!): A wide21:9desktop banner looks tiny and illegible when shrunk onto a375pxportrait phone! Art direction swaps in a tighter4:3or1:1mobile crop zoomed onto the focal subject.
CLS Warning on Art-Directed
<picture>: When your mobile<source>has a4:3aspect ratio (800×600) and your desktop<source>has a16:9aspect ratio (1600×900), puttingwidth="1600" height="900"only on the fallback<img>causes mobile browsers to reserve a16:9box first and then jump to4:3when the mobile source loads! Always declare explicitwidthandheightdirectly 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.