Your pages are plain HTML, they load fast on your machine, and then a performance report blames the photos. Or a phone takes seconds to show the first screen, and the text jumps down when a picture finally arrives.
On a static site, the browser downloads exactly the file you link to — nothing resizes or converts it on the way unless your host offers that as a feature. So optimizing images means doing four things yourself: export each image at the sizes it is actually shown, in a modern format with a fallback, tell the browser its dimensions, and lazy-load only the images below the first screen. Each one fixes a different symptom. The first is a one-time export; the other three are attributes on tags you already write. None of them needs a framework — and when you replace an image later, give it a new file name.
The 30-second version
| Problem | What you see | Fix |
|---|---|---|
| Too many pixels | A 4,000-pixel photo in a 700-pixel column | Export a few widths; list them in srcset with sizes |
| Heavy format | JPEG and PNG files where a smaller format would do | AVIF and WebP in <picture>, JPEG as the fallback |
| No reserved space | Text jumps when an image arrives | width and height on every <img> |
| Wrong loading order | The first screen is slow; or images below it load for nobody | loading="lazy" below the first screen only; fetchpriority="high" on the main image |
| Stale copies | You replaced a photo and visitors still see the old one | Give a changed image a new file name |
What is actually happening
Two metrics Google publishes for page speed measure exactly these problems. Largest Contentful Paint reports when the largest image, text block or video in the viewport renders; Google's target is 2.5 seconds or less for at least three out of four page loads (the 75th percentile). The candidates include an <img>, an <image> inside an SVG, a video's poster, and a CSS background-image loaded with url() — so on many pages the largest element is a photo. Cumulative Layout Shift measures content moving after it appears, and Google lists "images without dimensions" among its common causes; a good score is 0.1 or less.
A static host does not change any of that by default. It reads the file from storage and sends it. The work happens before upload, and in the HTML.
Step 1: Export the sizes you actually show
A picture displayed in a 700-pixel column does not need to be 4,000 pixels wide. On a screen with two device pixels per CSS pixel it needs about 1,400. Pick three or so widths that cover your layout, and generate them once.
You do not need a build pipeline for that. An image editor's export does it, and so does a short script. This one uses sharp, a Node.js library:
// resize.mjs — after `npm install sharp`, run once per image: node resize.mjs hero.jpg
import sharp from "sharp";
const input = process.argv[2];
const base = input.replace(/\.[^.]+$/, "");
for (const width of [480, 960, 1600]) {
for (const ext of ["avif", "webp", "jpg"]) {
await sharp(input)
.rotate()
.resize({ width, withoutEnlargement: true })
.toFile(`${base}-${width}.${ext}`);
}
}Three things about the script:
- Orientation. Phones often record a photo's rotation as a note inside the file (EXIF) rather than in the pixels. sharp removes all metadata by default, that note included, so a phone photo could come out sideways with its width and height swapped.
rotate()with no angle applies the note to the pixels first. - Format and quality. sharp infers the output format from the file extension. Its default quality is 50 for AVIF and 80 for WebP and JPEG — a starting point; compare by eye. For an image with transparency, use
pngin place ofjpg. - Too-small sources. Giving only a width keeps the aspect ratio, and
withoutEnlargementrefuses to scale an image up. So if the source is narrower than 1,600 pixels, the file named-1600is not 1,600 pixels wide, and the1600wyou write in the next step is wrong. Start from a source at least as wide as your largest size.
Step 2: Let the browser choose the size
srcset lists the files and how wide each one really is; sizes says how wide the image will be displayed. MDN describes the selection: the browser takes the first media condition (a test such as a maximum screen width) in sizes that is true, reads the slot width next to it — how wide the image will be shown — then loads the candidate that matches that slot, or the first one bigger than it, scaled down. MDN lists screen pixel density among the inputs, so expect a bigger file on a high-density screen than the slot width alone suggests; by the arithmetic in step 1, a 700-pixel slot at two device pixels per CSS pixel wants about 1,400.
<img
src="hero-960.jpg"
srcset="hero-480.jpg 480w, hero-960.jpg 960w, hero-1600.jpg 1600w"
sizes="(max-width: 700px) 100vw, 700px"
width="1600"
height="900"
alt="The harbour at dawn">Three details from the same page catch people out. The w unit is the file's real width in pixels, not px. A slot width cannot be a percentage — use vw or a fixed length. And the browser stops at the first true condition, so order matters; the last entry, with no condition, is the default.
Leave sizes out and the HTML Standard uses 100vw: the browser assumes the image fills the whole viewport, and on a wide screen it picks the largest file for a picture shown in a narrow column.
For a photo shown at one fixed size, such as an avatar, there is a shorter form: srcset="avatar.jpg, avatar-2x.jpg 2x", with no sizes.
Step 3: Serve a modern format, with a fallback
Chrome's own image audit, below, recommends AVIF and WebP. Not every browser decodes every format, and <picture> handles that. MDN on <source type>: if the browser does not support the given type, that <source> is skipped. The <img> inside is the fallback, and it also carries the size and alt for whichever file wins.
<picture>
<source type="image/avif"
srcset="hero-480.avif 480w, hero-960.avif 960w, hero-1600.avif 1600w"
sizes="(max-width: 700px) 100vw, 700px">
<source type="image/webp"
srcset="hero-480.webp 480w, hero-960.webp 960w, hero-1600.webp 1600w"
sizes="(max-width: 700px) 100vw, 700px">
<img src="hero-960.jpg"
srcset="hero-480.jpg 480w, hero-960.jpg 960w, hero-1600.jpg 1600w"
sizes="(max-width: 700px) 100vw, 700px"
width="1600" height="900"
alt="The harbour at dawn">
</picture>Put the format you prefer first. Then check that each .avif address really returns the image — 200 image/avif, never 200 text/html — with the curl command under troubleshooting.
Step 4: Reserve the space
MDN on <img>: including width and height lets the browser calculate the aspect ratio before the image loads and reserve the space, reducing or preventing a layout shift. Use the source's real pixel dimensions, or any pair with the same ratio.
This does not make the image a fixed size. Google's current advice keeps the attributes and lets CSS scale the image to its container, with width: 100% or max-width: 100%. For a whole site, max-width is the safer default: it shrinks a large image to fit without stretching a small one.
img {
max-width: 100%;
height: auto;
}Step 5: Lazy-load below the first screen, never the main image
The HTML Standard gives loading two states: lazy defers the fetch until some conditions are met, and eager — the default when the attribute is missing or invalid — fetches immediately. How close to the viewport a lazy image starts loading is left to the browser.
Lazy-loading helps for images a visitor may never scroll to. It hurts for the image that is on screen when the page opens. Google's analysis of WordPress's built-in lazy loading found that the technique cut image bytes at the cost of a slower LCP, and that lazy-loading only images below the first screen reversed the regression completely.
So: no loading attribute on the hero image, and fetchpriority="high" on it, which asks for a high fetch priority relative to other images. loading="lazy" on everything further down — with width and height, which MDN calls especially important for lazy images, because an unloaded image has a size of zero.
<img src="hero-960.jpg" fetchpriority="high" width="1600" height="900" alt="The harbour at dawn">
<!-- further down the page -->
<img src="gallery-1.jpg" loading="lazy" width="1200" height="800" alt="Fishing boats at the quay">decoding="async" turns up in many snippets. MDN's own note is that it is often difficult to perceive any effect on static images.
Step 6: Rename an image when it changes
A replaced photo under the same name can keep showing the old version from a cache — in the browser, or on the host's servers near the visitor. The dependable fix is a new file name when the content changes — hero-v2.jpg, or a content hash in the name. Each cache layer, and how long each one holds a file, is in why a site still shows the old version after a deploy.
Does your host resize images?
Some hosts transform images on request, so you upload one large file and ask for sizes in the URL. Others serve the file you uploaded.
- Netlify. The Image CDN answers
/.netlify/images?url=/hero.jpg&w=960and takesfmfor the format andqfor quality, which defaults to 75. Withoutfm, it uses WebP if the browser says it accepts it, otherwise AVIF, otherwise the original format. Netlify documents direct use for sites without a framework integration. An invalid parameter returns a 404. - Vercel. Image Optimization runs through a framework's
Imagecomponent — Next.js, Nuxt, Astro and others — or the Build Output API. A plain<img>bypasses it. Transformations are billed, and Vercel lists images under 10 KB, animated GIFs and SVGs as cases where it may not be necessary or beneficial. Redeploying alone does not clear its cache of transformed images: a changed local image gets a new cache key, while a remote one stays cached until its TTL runs out or you purge it. - Cloudflare. On a zone (a domain on your Cloudflare account) with transformations enabled,
/cdn-cgi/image/width=960,format=auto/hero.jpgresizes and picks the most efficient format the browser supports; quality defaults to 85. - Drop2Run. Files are served as uploaded: no resizing or format conversion.
.avifand.webpgo out asimage/avifandimage/webp. On a public site, a file named likehero.3f9a1c2b.jpg— a dot, eight or more hex characters, then the extension — may be cached for a year without the browser checking again;hero-3f9a1c2b.jpgdoes not count. Other images are cached for an hour. This is read from the router's code (apps/router/src/headers.ts, commitd75a02b);curl -sIon one of your images shows the header.
With any of these, width, height and the loading rules from steps 4 and 5 still apply. A resizing service fixes the bytes; it does not know your layout.
Troubleshooting
Open Chrome DevTools, record a page load in the Performance panel, and read the insights.
"Improve image delivery." The insight compares each image's download size with an efficient bytes-per-pixel ratio for its format, and recommends AVIF and WebP. Images with estimated savings under 4 KiB are not flagged. A large animated GIF is flagged too: video delivers animation more efficiently.
The LCP image is lazy-loaded, or not prioritised. Chrome checks three things about the main image: that it can be found in the HTML document (or a preload), that it does not have loading=lazy, and that it has fetchpriority=high. An image the HTML does not mention — one a script adds later, for example — fails the first check unless it is preloaded.
Layout shift from unsized images. Add width and height (step 4).
The largest file loads on every screen. sizes is missing, so the browser assumes 100vw; or a w value does not match the file's real width.
DevTools shows the JPEG, not the AVIF. Check the spelling of type, and that the response for the .avif file is 200 with image/avif, not text/html:
curl -sL -o /dev/null -w "%{http_code} %{content_type}\n" https://example.com/hero-960.avifA single-page-app fallback answering a missing file with your home page looks exactly like this — 200 text/html — and the favicon article walks through the same trap for icons.
Lazy images load immediately in a test. MDN notes that loading is only deferred when JavaScript is enabled, so a browser or tool with JavaScript turned off fetches every image straight away.
A lazy image never loads. The same page says lazy images are never loaded if they do not intersect a visible part of an element, and an unloaded image without width and height has a size of zero. Give it dimensions, and look for a parent that hides it — a collapsed section, or a scrolling container it never scrolls into.
When not to do this
- Logos, icons and diagrams: use SVG, not five raster sizes. Vercel lists vector formats among the cases where its optimization may not be necessary or beneficial.
- A page with two small images: resize them once and add
widthandheight. If Chrome's image delivery insight does not flag them — it skips images whose estimated savings are under 4 KiB — leave them alone. - A one-screen page: if every image is visible when the page opens, there is nothing below the first screen to lazy-load. Skip step 5, apart from
fetchpriorityon the main image. - Images you only link to: a photo offered as a download, not shown in the page, is not part of page speed. Keep the original.
Questions
Should I use WebP or AVIF?
Both, inside <picture>, with a JPEG or PNG <img> as the fallback. The browser skips a <source> whose type it does not support, so listing AVIF first and WebP second costs nothing for a browser that only reads one of them.
Do width and height stop an image from being responsive?
No. They give the browser the aspect ratio. With max-width: 100%; height: auto in CSS, the image scales to its container and the space is still reserved before it loads.
Is loading="lazy" safe to add to every image?
No. It delays the image that is on screen when the page opens, which is usually the page's Largest Contentful Paint. Leave that one eager, give it fetchpriority="high", and lazy-load the rest.
Do I need a build tool or framework to do this?
No. Everything here is HTML attributes plus image files. Generate the files once with an image editor or a short script such as the sharp example above, and upload them with the rest of the site.
Does my host convert images automatically?
Netlify does through its Image CDN URL, Cloudflare does on a zone with transformations enabled, and Vercel does through a framework's Image component or its Build Output API, not a plain <img>. On any other host, look for an image transformation feature in its documentation; without one, the file you upload is the file visitors get.
Why does the old picture still show after I replaced it?
The old file is cached under the same name, in the browser or at the edge. Give the new image a new file name and update the HTML that links to it.