How to compress images for a website
Compressing images for a website is three steps in a fixed order: resize each image to the largest width it is displayed at (about twice that for sharp phone and laptop screens), choose the format (WebP or JPG for photos, PNG or SVG for graphics), then compress to a file size that suits the image's role on the page.
Doing them in that order matters: compression alone cannot fix an image that is three times wider than the space it fills.
Why images are the first thing to fix
Google's Search Central documentation puts it plainly: “images are often the largest contributor to overall page size, which can make pages slow and expensive to load.” Every visitor downloads every byte, so the gap between a 3 MB and a 150 KB photo is paid again on every page view.
Step 1: resize to the displayed size
Find how wide the image actually appears in your layout, for example a content column of 800 CSS pixels or a full-width banner. Google's Lighthouse documentation is direct about the rest: “your page should never serve images that are larger than the version that's rendered on the user's screen. Anything larger than that just results in wasted bytes and slows down page load time.” Lighthouse flags an image when it is at least 4 KiB larger than needed.
The one twist is high-density screens. MDN notes that “some devices have high resolution screens that need larger images than you might expect to display nicely”, with two or more device pixels per CSS pixel. So a slot 800 CSS pixels wide wants an image about 1600 pixels wide for those screens, and an 800 px version for the rest. Theimage resizer or the bulk resizer makes both.
Step 2: choose the format by content
- Photos: WebP, with JPG as a safe fallback. Google's WebP page reports lossy WebP files 25–34% smaller than comparable JPEGs at equivalent quality.
- Logos, icons, screenshots, diagrams: PNG, reduced to a palette. MDN advises lossless encoding for these, because “blurring and colored fringes around text and sharp edges are very visible.”
- Simple icons and logos that exist as vectors: SVG. MDN says to “prioritize the SVG version whenever possible.” Run it through the SVG compressor.
- Transparency over a photo: WebP, which supports transparency in both lossy and lossless modes.
A photo saved as PNG is the easiest oversized file to fix. To turn existing JPGs into WebP, use JPG to WebP; for the wider format question, see thebest image format for the web.
Step 3: compress to a budget
Rather than picking a quality number for each image, it is easier to give each kind of image a file-size budget and let a tool find the best quality that fits. Budgets are your call; a hero photo can afford more than a thumbnail in a grid of twenty. Our target-size compressor searches for the highest quality that fits the budget and, if allowed, scales the image down when quality alone is not enough.
Read the table backwards and it tells you something useful: if your slot needs about 1600 px, a budget near 100 KB is realistic for a JPG of this kind; asking for 50 KB means the tool has to shrink it below that width. If you resized in step 1 and the budget suits that size, the compressor keeps your dimensions and only adjusts quality.
Step 4: serve the files sensibly
Compressed files help most when the page asks for the right one. Two HTML features do most of the work:
srcsetandsizeslist several widths of the same image and let the browser choose. MDN's example shows the browser picking a 480 px file instead of the 800 px one on a small screen. Google recommends always keeping a plainsrcas the fallback.loading="lazy"on images further down the page. MDN describes it as telling the browser to defer off-screen images “until the user scrolls near them”, so that “non-critical resources” load only if needed. Leave it off the main image people see as soon as the page opens.
<img src="team-800.webp"
srcset="team-800.webp 800w, team-1600.webp 1600w"
sizes="(max-width: 800px) 100vw, 800px"
loading="lazy" alt="Our team at the spring workshop">A whole site's worth of images
For an existing site, the bulk image compressor applies one KB target or one quality to many files and returns a ZIP. Resize the batch first if the originals are camera-sized. If you want to understand what the quality setting throws away, thelossy vs lossless guide explains it.