Image optimisation for the web: formats, sizes and what to do first
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
| Format | Best for | Notes |
|---|---|---|
| JPEG | Photographs | Lossy. No transparency. Works everywhere |
| PNG | Screenshots, diagrams, images with sharp edges or transparency | Lossless. Very large for photographs |
| WebP | Either of the above | Lossy and lossless modes, transparency, animation. Typically a quarter to a third smaller than JPEG at similar quality |
| AVIF | Photographs where size matters most | Smaller again than WebP, especially at low bitrates. Slower to encode |
| SVG | Logos, icons, simple illustrations | Vector: sharp at any size, often tiny |
| GIF | Almost nothing now | 256 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.
- For JPEG and WebP photographs, start around 75 to 85.
- AVIF uses a different scale, and lower numbers there give results comparable to higher JPEG ones; judge by eye rather than copying a setting across.
- Images shown small, such as thumbnails, tolerate much lower quality than a full-width hero image.
- Never recompress an already compressed JPEG repeatedly. Each generation adds artefacts. Keep the original and export from it.
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
widthandheight. With them, the browser reserves the right space before the image arrives. Without them, the page jumps as each image loads, which is the main cause of layout shift.loading="lazy"on images below the first screen, so they are fetched only as the reader approaches. Do not put it on the main image at the top of the page; that one should load as early as possible.alt. A description for people who cannot see the image, and for search engines. Purely decorative images take an emptyalt="".
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
- Keep the original, untouched.
- Decide the largest size at which the image will be shown, and export at up to twice that.
- Choose the format: SVG for vector artwork, WebP or AVIF for photographs, PNG or lossless WebP for screenshots and diagrams.
- Lower the quality until you can see a difference, then step back.
- Generate two or three widths and list them in
srcset. - Set
width,heightandalt, and lazy-load everything below the first screen. - Strip metadata.
- Check the page on a throttled connection, because that is how many visitors will experience it.