Image Optimisation — Performance

The browser downloads every pixel of the file, whatever size it's displayed at: a 4000 × 3000 photo in an 800 × 600 box wastes 25 times the pixels, so resize…

A product photo straight from a phone camera: 4000 × 3000 pixels, about 3.6 MB. On slow 4G that photo takes 18 seconds to arrive, while on screen it only fills an 800 × 600 box. Your visitors wait for pixels they never see.

Optimising images means two things: making the image file as small as possible without it looking bad, and telling the browser the image's size before the file arrives. Images are often the biggest part of a page's weight, so this is where savings come easiest. It's like sending a photo on WhatsApp: a 3.6 MB photo from the camera gets scaled down and compressed to a few hundred KB, and it still looks good on your friend's phone.

The right file size

CSS only changes the displayed size. If the file is 4000 × 3000, the browser still downloads every pixel, then shrinks it on screen. The rest is thrown away for nothing.

Count the pixels: 4000 × 3000 = 12 million pixels, while 800 × 600 = 480 thousand pixels. It's 5 times wider and 5 times taller, so 5 × 5 = 25 times more pixels. At the same quality, the file gets roughly 25 times smaller too: 3.6 MB becomes about 144 KB.

Here's the same photo from the Lab, step by step:

Step File size Download on slow 4G (1.6 Mbps)
Straight from the camera, 4000 × 3000, JPEG quality 95 about 3.6 MB 18 seconds
Resized to 800 × 600 about 144 KB 0.7 seconds
Compressed to quality 80 about 80 KB 0.4 seconds
Saved as WebP about 53 KB 0.3 seconds

A sharp phone screen sometimes needs twice the pixels so the image isn't blurry. How to offer several sizes at once is in the Responsive Images lesson.

Compressing: lossy and lossless

So the most effective way to shrink a file without the quality visibly dropping: compress losslessly (drop the metadata), then convert to a modern format like WebP. Tools: squoosh.app in the browser, or the sharp library in Node.js:

import sharp from 'sharp';

await sharp('original-photo.jpg') // 4000 × 3000, about 3.6 MB
  .resize({ width: 800 })         // 800 × 600, same ratio
  .webp({ quality: 80 })          // compress to WebP
  .toFile('photo.webp');          // about 53 KB, metadata dropped too

Say how big it is: width and height

Without width and height, the browser doesn't know the image's height until the file has downloaded. So it leaves a height of 0, lays out the text below, and when the photo arrives all that text gets pushed down. This sudden movement is a layout shift, and the total is measured by CLS (Cumulative Layout Shift): good up to 0.1, poor above 0.25.

In the Lab, the article opens on a phone 360 px wide. The photo shows at 328 × 246 px. Without a size, the paragraph under it jumps 246 px: CLS 0.27, red. With width="800" height="600", the browser knows the ratio is 4:3 straight away and keeps a 328 × 246 box free from the start: CLS 0.

<style>
  img { max-width: 100%; height: auto; }
</style>

<img
  src="photo.jpg"
  width="800"
  height="600"
  loading="lazy"
  alt="Mountain view at sunrise"
/>

⚠️ What people miss when they add an image

TL;DR: The browser downloads every pixel of the file, whatever size it's displayed at: a 4000 × 3000 photo in an 800 × 600 box wastes 25 times the pixels, so resize the file. Then compress it (lossy at about quality 80, lossless to drop the metadata) and use a modern format like WebP: 3.6 MB can become about 53 KB. Always write width and height (numbers without px) so the browser keeps space free and CLS stays 0, with the CSS max-width: 100%; height: auto;. Give images below the first screen loading="lazy", and give every photo an alt that describes it.

⚡ Resize the file, not just how it shows

Guess first: a 4000 × 3000 photo shows in an 800 × 600 box. How many times the pixels still get downloaded?

25 times. Five times wider and five times taller. CSS only changes the displayed size, the browser still downloads every pixel.

Meaning: Resize the file to the size it's shown at. 3.6 MB becomes about 144 KB.

⚡ Compress, then a modern format

Write:

await sharp('original-photo.jpg')
  .resize({ width: 800 })
  .webp({ quality: 80 })
  .toFile('photo.webp');

Meaning: The most effective way to shrink a file without the quality visibly dropping: compress losslessly (drop the metadata), then convert to a modern format like WebP. That photo ends up about 53 KB.

⚡ Say how big it is

Write:

<img src="photo.jpg" width="800" height="600" loading="lazy" alt="Mountain view at sunrise" />

Meaning: width and height (numbers without px) let the browser keep space free from the start, so CLS stays 0. A photo below the first screen gets loading="lazy", and alt describes what's in it.

What you will learn