How to optimize images for the web: WebP, AVIF and sizing

Learn how to optimize images for the web with the right format, dimensions, compression and lazy loading, plus the exact HTML that keeps pages fast and sharp.

To optimize images for the web, do four things: serve them at the size they're actually displayed, use a modern format like WebP or AVIF, compress them to a sensible quality, and load them in the right order (the main image early, everything below the fold lazily). Do those and images stop being the reason your site is slow.

Images are usually the heaviest part of a page. On slow sites I work on, the single biggest win is often a hero photo uploaded straight from a camera or stock site at 5,000 pixels wide, scaled down by the browser to 1,200. The visitor downloads several megabytes to see something that could have been 150 KB.

This guide explains each step, with HTML you can use and settings I use in practice.

Why images matter for speed and SEO

The largest image near the top of the page is very often the Largest Contentful Paint (LCP) element. LCP is one of Google's Core Web Vitals, and the target is 2.5 seconds or less. A heavy hero image makes that hard on mobile connections.

Images also cause layout shift when the browser doesn't know their size in advance, which hurts Cumulative Layout Shift (CLS). And for image search, descriptive file names and alt text help Google understand what the picture shows.

So image optimisation touches speed, stability and discoverability at the same time.

Choosing a format: JPEG, PNG, WebP, AVIF, SVG

Here's how I pick:

FormatBest forNotes
AVIFPhotos and complex imagesUsually the smallest files at good quality; slower to encode
WebPPhotos and graphics with transparencySmall files, supported by all current major browsers
JPEGPhotos, as a fallbackUniversal; larger than WebP/AVIF at the same quality
PNGScreenshots, graphics needing lossless qualityOften very large for photos; avoid for photography
SVGLogos, icons, simple illustrationsVector, sharp at any size, often tiny

In most projects I serve AVIF with a WebP or JPEG fallback, or just WebP if the platform makes AVIF awkward. The difference between WebP and AVIF is real but smaller than the difference between either one and an unoptimised JPEG.

For logos and icons, use SVG wherever you have the vector file. A logo saved as a 2,000-pixel PNG is a common waste.

Getting the dimensions right

Format matters less than size. A WebP at 4,000 pixels wide is still too big.

Work out the largest size the image is shown at. If your content column is 800 pixels wide, a blog image doesn't need to be more than about 1,600 pixels wide (twice the width, for sharp display on high-density screens). A full-width hero might need versions up to around 2,000 to 2,400 pixels for large desktop screens, but phones should get a much smaller version.

That's where responsive images come in. You give the browser several widths and let it choose.

Responsive images in HTML

This is the pattern I use when writing HTML by hand:

<picture>
  <source
    type="image/avif"
    srcset="/img/hero-640.avif 640w, /img/hero-1280.avif 1280w, /img/hero-1920.avif 1920w"
    sizes="100vw">
  <source
    type="image/webp"
    srcset="/img/hero-640.webp 640w, /img/hero-1280.webp 1280w, /img/hero-1920.webp 1920w"
    sizes="100vw">
  <img
    src="/img/hero-1280.jpg"
    width="1920" height="1080"
    alt="Technician installing a heat pump outside a brick house"
    fetchpriority="high">
</picture>

What each part does:

  • <source type="..."> lets the browser pick the first format it supports.
  • srcset lists the available widths; sizes tells the browser how wide the image will display (here, full viewport width). For an image in an 800-pixel column, sizes="(max-width: 840px) 100vw, 800px" is a typical value.
  • width and height give the aspect ratio so the browser reserves space and the layout doesn't jump.
  • fetchpriority="high" tells the browser this is the important image. Use it only on the main above-the-fold image.

For images further down the page, add loading="lazy" so they load only as the visitor scrolls near them. Never lazy-load the hero image. That delays LCP, and I still see it on a lot of sites because a plugin applies lazy loading to everything.

Compression settings that work

Compression trades file size for quality. For photos, quality settings around 70 to 80 for WebP and JPEG are a good starting point. AVIF uses a different scale in many tools and can usually go lower while looking the same. Always look at the result at actual size before deciding.

My practical rules:

  • Strip metadata (camera data, GPS location) unless you need it. It adds weight and can leak information.
  • For a typical blog image, aim for well under 200 KB. A full-width hero can be more, but if it's over 300 to 400 KB, check whether it really needs to be.
  • Don't re-compress an already compressed JPEG several times. Start from the original whenever you can.
  • Screenshots with lots of text sometimes look better as PNG or lossless WebP. Test both.

Tools I use: Squoosh (a browser-based tool from the Chrome team) for one-off images, and command-line tools or build pipelines for bulk work.

Platform-specific shortcuts

You don't always have to do this by hand.

  • WordPress. Since version 4.4 WordPress creates several sizes on upload and outputs srcset automatically. It also supports uploading WebP and, in recent versions, AVIF (if your server's image library supports it). An image optimisation plugin can convert and compress your library in bulk. Make sure your theme isn't forcing the full-size original into small spaces. Elementor sites often need specific attention here, which I cover in Elementor speed optimisation.
  • Shopify. Shopify's image CDN resizes and serves modern formats automatically when themes use its image filters properly. Upload a high-quality original and let the theme request the sizes it needs.
  • Next.js. The next/image component generates resized, modern-format versions on demand and lazy-loads by default. You still need to set sizes correctly, or the browser may pick a larger version than it needs.

Alt text and file names

Alt text describes the image for people using screen readers and for search engines. Write what the image shows in the context of the page. "Blue linen shirt, front view, on a model" is helpful. "shirt shirt linen shirt buy" is not. Purely decorative images can use an empty alt="" so screen readers skip them.

File names should be descriptive too: blue-linen-shirt-front.webp rather than IMG_4821.webp. It's a small signal, but it costs nothing.

A quick image optimisation checklist

  1. Find the largest images on each key page (Chrome DevTools Network tab, filter by Img, sort by size).
  2. Resize originals to no more than about twice their displayed width.
  3. Convert photos to WebP or AVIF; use SVG for logos and icons.
  4. Compress to around quality 75 and check visually.
  5. Add srcset and sizes, or confirm your platform does it.
  6. Set width and height on every image.
  7. Load the hero early with fetchpriority="high" and no lazy loading.
  8. Lazy-load everything below the fold.
  9. Write useful alt text and file names.
  10. Re-test with PageSpeed Insights.

For more on the LCP side, see my guide on fixing Largest Contentful Paint. Google's web.dev also has thorough articles on responsive images.

The short version

Most image problems are size problems. Resize to what's displayed, use WebP or AVIF, compress to a sensible quality, set width and height, prioritise the hero and lazy-load the rest. That alone fixes a large share of the slow sites I see.

If your pages are still heavy after that, I can find out what else is holding them back. Have a look at my speed optimisation service or get in touch.

Available for new projects

Need a hand with this?

Tell me what you need: a new site, a fix, a SaaS or an app. You'll get a clear plan and price within a day.

Start a project WhatsApp me