Make a website’s pictures load faster

Websites, code and drawingsUpdated อ่านภาษาไทย

Run your pictures through Optimize for web. It makes a WebP at the width you need, a half-size copy for phones, and the HTML with srcset, width and height so the page does not jump. Nothing is uploaded, so client work can go through it.

The resultThe file beforeBefore JPG, 309 KBAfter 2 × WEBP, 289 KB
What the tool really gave for our sample file. Drag the line to compare.

What actually slows a page

Three mistakes, the most costly first:

  • Too many pixels. A 4000 px photo in an 800 px column sends 25 times the pixels that anyone sees.
  • The wrong kind. A photo as PNG is several times its JPG.
  • No size in the HTML. The text jumps when the picture arrives, and readers tap the wrong link.

Fix the first and the rest is polish.

Do it

  1. Find the widest the picture is ever shown, in CSS pixels, and double it for sharp screens, up to about 1600.
  2. Open Optimize for web, press Choose files, and put that number in Widest (px).
  3. Leave Quality at 78 and Also a half-size copy for phones on.
  4. Look at the result, then press Download. File names become lower case with dashes.
  5. Copy the HTML, fill in the alt, and set sizes to your layout.

For our 309 KB sample the tool gave:

<img src="photo.webp" srcset="photo-small.webp 640w, photo.webp 1280w" sizes="(max-width: 1280px) 100vw, 1280px" width="1280" height="960" loading="lazy" alt="">

One photo, six ways

The same 1280 × 960 photo:

Kind Size
JPG, as it came 309 KB
PNG 2.3 MB
WebP at 90 368 KB
WebP at 78 224 KB
JPEG XL at 80 192 KB
AVIF at 60 194 KB
WebP, 640 px wide, for phones 65 KB

Two lessons. WebP at 90 is larger than the JPG it came from: quality numbers of different kinds are not comparable, and saving a JPG again at high quality wastes bytes. And the phone copy saves more than any format does.

Things to set by hand

  • The first picture on the page should not be lazy. Take loading="lazy" off it and add fetchpriority="high".
  • sizes must say how wide the picture is in your layout, or the browser picks the large one.
  • Logos and icons should be SVG. If a place needs PNG, use SVG to PNG. Tab icons: favicon generator.
  • AVIF with a WebP fallback goes in a <picture> element. Make the AVIF with Convert to AVIF.
  • JPEG XL is the smallest here for some photos but not every browser shows it, so keep it for archives.

What goes wrong

Safari cannot write WebP in the converter page. Use Chrome, Edge or Firefox to make it; Safari shows WebP without trouble.

Text in the picture is fuzzy. Screenshots want PNG or a high quality. Better, write the text as HTML.

Colours shifted. The original had a wide-gamut profile. Export sRGB from your editor first.

The page still jumps. Your CSS sets width: 100% without height: auto.

Check

Open the page, press F12, Network, filter Img, and reload as a phone. The total of pictures should fall well under a megabyte for an ordinary page, and no picture should be more than twice its shown size. Then look at it yourself on a real phone, because numbers do not see smears.

When a tool page is not enough

With a thousand pictures or a build step, let your framework or an image CDN make the sizes. For a shop with ten products, a newsletter, or a landing page this afternoon, do it here.

Questions

WebP or AVIF?

AVIF is usually smaller, takes longer to make, and is read by current browsers. WebP is the safe choice without a fallback.

Why does the HTML leave alt empty?

Only you know what the picture means there. Write one sentence, or leave it empty on purpose for decoration.