Guide

Image optimisation for the web: formats, sizes and what to do first

Published · 8 minute read

Images are usually the heaviest part of a web page, and the largest image is very often the thing that decides how fast the page feels. Most of the available saving comes from two decisions that have nothing to do with exotic tooling: sending the right dimensions and choosing the right format.

Step one: resize before you compress

The single most common waste is serving an image far larger than it is displayed. A photo straight from a phone is around 4,000 pixels wide. Shown in a 400-pixel-wide card, a hundred times as many pixels are downloaded and decoded as the screen can show.

File size grows with the pixel count, which is width multiplied by height. Halving both dimensions removes three quarters of the pixels. No quality setting comes close to that.

The target is the displayed size multiplied by the screen's pixel density. Most phones and many laptops have a density of 2 or 3, so an image shown at 400 CSS pixels wide should be about 800 pixels wide to look sharp. Going beyond twice the display size rarely shows any visible benefit. Use an image resizer to produce the sizes you need before thinking about anything else.

Step two: choose the format by content

FormatBest forNotes
JPEGPhotographsLossy. No transparency. Works everywhere
PNGScreenshots, diagrams, images with sharp edges or transparencyLossless. Very large for photographs
WebPEither of the aboveLossy and lossless modes, transparency, animation. Typically a quarter to a third smaller than JPEG at similar quality
AVIFPhotographs where size matters mostSmaller again than WebP, especially at low bitrates. Slower to encode
SVGLogos, icons, simple illustrationsVector: sharp at any size, often tiny
GIFAlmost nothing now256 colours and very large files; use video or animated WebP for animation

Two rules follow from how these formats work.

Do not save photographs as PNG. Lossless compression cannot shrink the fine noise in a photo, so a PNG photograph is routinely five to ten times the size of a good JPEG that looks identical.

Do not save screenshots and diagrams as JPEG. JPEG smears sharp edges and produces a haze of artefacts around text. PNG or lossless WebP keeps them crisp, and for flat-colour graphics is often smaller as well.

WebP is supported by every current browser and is a safe default. AVIF is also widely supported now and worth using for large photographs, with WebP or JPEG as a fallback for older browsers.

Step three: pick a quality setting by looking

Lossy formats trade size for fidelity through a quality number. The relationship is far from linear: going from 100 down to 85 often halves the file with no difference anyone can see, while going from 85 to 60 saves less and begins to show.

An image compressor with a side-by-side preview lets you lower the setting until you can see the damage, then step back up one notch.

Let the browser choose the size

One file cannot be right for both a phone and a large monitor. The srcset and sizes attributes describe the available versions and how wide the image will be displayed, and the browser downloads the best match:

<img src="photo-800.jpg"
     srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
     sizes="(max-width: 600px) 100vw, 600px"
     width="800" height="533" alt="Harbour at dusk">

To offer newer formats with a fallback, wrap the image in a picture element. The browser uses the first source it supports:

<picture>
  <source srcset="photo.avif" type="image/avif">
  <source srcset="photo.webp" type="image/webp">
  <img src="photo.jpg" width="800" height="533" alt="Harbour at dusk">
</picture>

Three attributes that cost nothing

SVG: small, sharp and worth cleaning

For logos and icons, SVG is usually both the smallest and the sharpest choice, because it stores shapes rather than pixels. Files exported from design tools carry a lot of baggage, though: editor metadata, hidden layers, needless precision in coordinates and unused definitions. Running them through an SVG optimiser commonly removes half the bytes with no visual change.

Two cautions. An SVG can contain scripts, so treat files from untrusted sources as you would HTML and sanitise them before displaying them inline. And a very complex illustration with thousands of paths can be larger and slower to draw than a well-compressed raster image.

Metadata and privacy

Photographs carry embedded metadata: camera model, settings, time and, from phones, often the exact GPS position where the picture was taken. It adds weight and can disclose more than you intended, such as the location of someone's home. Strip it from anything you publish. Keep the orientation and the colour profile when they matter, or apply the rotation to the pixels before removing the data.

A workflow you can repeat

  1. Keep the original, untouched.
  2. Decide the largest size at which the image will be shown, and export at up to twice that.
  3. Choose the format: SVG for vector artwork, WebP or AVIF for photographs, PNG or lossless WebP for screenshots and diagrams.
  4. Lower the quality until you can see a difference, then step back.
  5. Generate two or three widths and list them in srcset.
  6. Set width, height and alt, and lazy-load everything below the first screen.
  7. Strip metadata.
  8. Check the page on a throttled connection, because that is how many visitors will experience it.

Tools used in this guide

More guides