How to compress images for the web
How to get images down to a sensible size for the web without the loss of quality being visible.
Images are the largest single thing most websites load, and they are also the easiest thing to fix without touching hosting, code or design. A photo straight off a phone or camera is typically far larger, in both pixel dimensions and file size, than anything a website actually needs — and every one of those unnecessary bytes has to be downloaded before the page can finish rendering.
Three separate decisions determine an image's final size: how many pixels it is, what format it is saved in, and how aggressively it is compressed. Getting all three right, in order, is what actually moves the needle — doing only one of them leaves most of the saving on the table.
Step one: resize to the pixel dimensions you actually display
This is the step people skip most often, and it is usually the biggest saving of the three. If an image displays at 800 pixels wide on the page, uploading it at 4000 pixels wide means the browser downloads a file five times larger than it needs to, then shrinks it down in the browser — all of that extra data did nothing except slow the page down.
A hero image might occupy the full width of a large monitor, but on a phone screen the same slot could be a third of that. If the site is responsive, either export a size that comfortably covers the largest slot the image will ever fill, or use responsive srcset markup so the browser can choose an appropriately sized version for the device it is on.
Step two: choose the right format
| Format | Best for | Notes |
|---|---|---|
| WebP | Almost everything — photos, screenshots, graphics | Widely supported by current browsers; typically produces smaller files than JPEG or PNG at equivalent visual quality |
| AVIF | Photos where file size matters most | Generally compresses further than WebP; support is slightly newer, so keep a fallback for older browsers |
| JPEG | A safe fallback for photos | Universally supported, but produces larger files than WebP or AVIF at the same quality |
| PNG | Images that need transparency, or flat graphics with sharp edges | Lossless, which makes it large for photos — use it only when you specifically need transparency or crisp lines |
| SVG | Logos, icons, simple illustrations | Vector, not pixel-based — scales to any size from a tiny file, but unsuitable for photographs |
Serving WebP images covers the practical side of switching format, including how to keep a fallback for the small number of visitors on older software.
Step three: compress within the format
Every format above still has a quality setting, and the right amount of compression is more aggressive than most people expect. For photographic content, a moderate-to-high compression setting is usually visually indistinguishable from the uncompressed original at normal viewing size, while producing a substantially smaller file. The way to find the right setting for a given image is to compress it, view it at the size it will actually display on the page — not zoomed in — and only back off the compression if you can actually see a difference at that size.
Two ways to do this in practice:
- Compress before you upload. Desktop tools and many free web-based compressors let you resize and compress in one step before the file ever reaches your hosting account. This is the most reliable method because you can see the result before committing to it.
- Compress automatically on the platform. Content management systems including WordPress have plugins that resize and compress images on upload, and some also convert existing image libraries retroactively. This is convenient for a site with many contributors, at the cost of less control over each individual image.
Do not compress twice
Compression that removes image data — which is how JPEG, WebP and AVIF all work — loses a little quality each time it is applied. Re-compressing an image that has already been compressed, for instance by running it through two different plugins, or exporting an already-compressed JPEG at a lower quality again, degrades it for no size benefit worth having. Compress once, from the original, at the setting you actually intend to keep.
Beyond the image file itself
Two markup details, alongside the image file itself, affect how much the image slows the page down:
- Set
widthandheightattributes. This lets the browser reserve the right amount of space before the image has loaded, which also prevents the page jumping around — see fixing layout shift for the full explanation. - Use
loading="lazy"on images below the fold. This defers loading until a visitor scrolls near the image, so it does not compete for bandwidth with content that is visible immediately. Never apply it to the first, largest image on the page — see lazy-loading images for exactly where the line is.
<img src="photo.webp" width="800" height="533"
loading="lazy" alt="...">
A working process
-
Decide the maximum size the image will ever display at
Check the design or the page itself rather than guessing.
-
Resize to that dimension, or slightly above it
Do this before compressing — resizing an already-compressed image wastes the compression pass on pixels you are about to discard.
-
Save as WebP, or AVIF with a fallback
Both outperform JPEG and PNG for almost every use case on a website.
-
Compress at a setting you cannot see a difference at
Check at actual display size, not zoomed in on the original.
-
Add width, height and loading attributes when you place it
Free improvements to layout stability and load order that cost nothing in file size.
Images are usually the single biggest opportunity on a slow site, and the whole process above needs no hosting changes at all. If you have already compressed every image and the page is still heavy, reducing the number of requests a page makes and minifying CSS and JavaScript are the next places to look.
Related reading
How WebP compares to older image formats, and how to add a fallback for the rare browser that needs one.
How to lazy-load imagesHow native lazy-loading works, and the one image on every page that should never use it.
How to improve Largest Contentful PaintHow to find the specific element dragging down your LCP score, and the fixes that actually move it.
How to reduce the number of requests a page makesWhy every extra file a page loads adds delay, and how to cut the number down without changing how the page looks.