Images often account for the bulk of a page’s load size. The same image can differ more than tenfold in size after optimization, with almost no visible difference. Image optimization is one of the highest-return performance efforts.
1. Choosing image formats
Different formats suit different content; the wrong choice wastes bytes.
JPEG suits photos and complex-color images. It is lossy, with size controlled by a quality parameter. Use 75 to 85 for a sharp size reduction with differences hard to see.
PNG suits transparency, icons, and line art. It is lossless but usually larger. Do not store photos as PNG.
WebP supports lossy, lossless, and transparency, and is about 25 to 35 percent smaller than JPEG at equal quality. Mainstream browsers support it fully; it is the recommended web format.
AVIF compresses even better, over 20 percent smaller than WebP, but encodes slowly and is unsupported in some older browsers. Best for extreme load-speed demands.
SVG is vector, for icons and simple graphics. It is resolution-independent, stays crisp at any scale, and is tiny. But complex graphics may end up larger as SVG than as raster.
2. How to compress correctly
Crop before compressing. Many compress a full large image when only a small part is displayed. Crop to the needed size and composition first, then compress — far better than simply lowering quality.
Match actual display size. If the maximum display width is 800 pixels, do not upload a 3000-pixel original. Prepare at twice the display size to suit high-DPI screens.
Choosing lossy quality. Higher is not better. Dropping from 100 to 85 may halve the size with almost no visible change, but from 85 to 70 can cause visible blocking and blurred edges. Test between 75 and 85.
Metadata cleanup. Camera photos carry EXIF data, which may include geolocation. Removing it reduces size and protects privacy.
3. Responsive image solutions
Different devices need different sizes. Responsive images load the right size on demand.
The srcset attribute offers multiple size versions, and the browser picks based on screen width and pixel density. For example, prepare 480, 800, 1200, and 1600 pixel widths.
The picture element handles more complex scenarios, loading different formats by condition. For example, try AVIF first, fall back to WebP, then JPEG.
Lazy loading defers off-screen images until they scroll into view, significantly speeding up the first screen. The native loading=”lazy” attribute works directly.
4. Easily overlooked optimizations
Placeholders. Set explicit aspect ratios so layout does not shift during loading. Layout shift hurts user experience and search rankings.
Image CDN. A CDN with automatic format conversion and resizing lets you optimize images in bulk without code changes.
Icon fonts and sprite sheets. Both older techniques have largely been replaced since HTTP/2 and SVG became widespread. New projects should use inline or standalone SVG.
5. A checklist
Before launch, verify: format matches content, size matches display needs, metadata is cleaned, responsive versions exist, off-screen images are lazy-loaded, and aspect ratios prevent layout shift.
Image optimization never ends, but doing the first four usually captures over 80 percent of the benefit.
