Lesson 6 of 14 · 23m
Image Strategy: Formats, Sizing, and the LCP Image
The LCP image must be discoverable, prioritized, correctly sized, and never lazy-loaded — the three rules that fix most image-driven LCP.
On most content and commerce pages the LCP element is an image — and on most of those pages the image is wrong in at least one of three ways: it's discovered late, it's far bigger than its display size, or it's served in a format from 1992. Fix all three on the hero and you'll often take a full second off LCP without touching application code.
Rule one: the LCP image must be discoverable and prioritized
The preload scanner finds <img> tags in raw HTML almost immediately. It cannot find images set by CSS background-image, injected by JavaScript, or rendered client-side after hydration — those all surface as huge resource load delay.
<img
src="/img/trail-runner-3-1200.avif"
srcset="/img/trail-runner-3-640.avif 640w,
/img/trail-runner-3-960.avif 960w,
/img/trail-runner-3-1200.avif 1200w,
/img/trail-runner-3-1800.avif 1800w"
sizes="(min-width: 1024px) 50vw, 100vw"
width="1200" height="900"
fetchpriority="high"
alt="Trail Runner 3 in slate blue, side view">fetchpriority="high" tells the browser this image outranks the other images it has found — images default to low priority until layout proves they're in view. If the hero truly can't live in the HTML, preload it: <link rel="preload" as="image" imagesrcset="..." imagesizes="..." fetchpriority="high">.
Rule two: never lazy-load the LCP image
loading="lazy" defers the request until layout confirms the image is near the viewport — after CSS has loaded and layout has run. On the hero that's pure added delay, and it is the single most common LCP regression I see — usually a component library defaulting every <Image> to lazy. Lazy everything below the fold; the one image that is the LCP loads eagerly, prioritized.
Rule three: serve the right bytes
- Size:
srcsetwithwdescriptors plus an honestsizeslets the browser pick the smallest file covering the slot at the device's pixel ratio. A wrongsizes— or none, which defaults to100vw— quietly makes phones pull desktop-sized images. - Format: AVIF runs ~30–50% smaller than JPEG at the same perceived quality; WebP ~25–30%. Every current browser supports both. Keep
<picture>+<source type="image/avif">only if you still need a JPEG fallback for ancient clients. - Quality: AVIF at q
50, WebP at q75 look fine for photos. Nobody sees the difference between q85 and q95 on a phone; everyone feels the extra 300 KB.
Generate variants once in a build step rather than trusting editors to export correctly:
// scripts/build-images.mjs — `node scripts/build-images.mjs`
import sharp from "sharp";
const widths = [640, 960, 1200, 1800];
for (const w of widths) {
await sharp("src/img/trail-runner-3.jpg")
.resize({ width: w, withoutEnlargement: true })
.avif({ quality: 50 })
.toFile(`public/img/trail-runner-3-${w}.avif`);
}Videos are not exempt either
A looping hero video is an LCP element wearing a costume — its poster is what LCP records, so give the <video> a poster attribute pointing at a proper AVIF/WebP frame and treat it exactly like the hero image: discoverable in HTML, fetchpriority="high" on the poster via <link rel="preload" as="image">. The video bytes themselves should load lazily (preload="none" or a click-to-play facade); a 6 MB MP4 autoplaying in the hero is a load-duration bomb.
Dimensions prevent layout shift
Always set width and height (or CSS aspect-ratio). The browser uses the ratio to reserve space before a single byte arrives, so content below doesn't jump when the image lands. Missing dimensions remain the most common CLS cause on the web — the CLS lesson covers the rest of that story.
Field notes
- Check the LCP element in field data per device: a carousel's first slide, a text heading, and a video poster each need different fixes.
decoding="async"is fine below the fold; on the LCP image it can occasionally push paint a frame later. Measure before sprinkling.- An image CDN (on-the-fly resize + format negotiation via the
Acceptheader) beats a build step once you accept user uploads — make sure it sendsVary: Accept. - A fade-in on the hero delays the LCP timestamp until the image is actually painted. Designers should know the trade they're making.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.