Skip to content

Guide

How to optimize images for a website

Images are often the heaviest part of a web page, and most are far larger than they need to be. Four steps, in this order, make them small without making them look worse.

By Mark Stoumann ·

The order matters. Each step works on the result of the one before, and the first one usually saves the most. Images also tend to be the Largest Contentful Paint (LCP) element, the biggest thing visible when the page opens. Google counts an LCP of 2.5 seconds or less as good, so a heavy hero image costs more than bandwidth.

1. Pick the right pixel size

A photo straight from a phone or camera is 3,000 to 8,000 pixels wide. A web page rarely shows an image wider than about 1,200 CSS pixels, the unit your stylesheet uses. Every pixel beyond what the screen can show is downloaded and then thrown away.

The screen can show more than one real pixel per CSS pixel, though. The ratio is called the device pixel ratio (DPR): 1 on a classic monitor, 2 on most laptops with high-resolution screens, and often 3 on phones. So the size you need is:

file width = display width in CSS pixels × device pixel ratio

A worked example, for a blog with an article column 800 CSS pixels wide:

  • On a laptop with a DPR of 2, the image fills 800 × 2 = 1,600 pixels.
  • On a phone 390 CSS pixels wide with a DPR of 3, the column is about 358 CSS pixels after 16-pixel margins, so 358 × 3 = 1,074 pixels.
  • On an older office monitor with a DPR of 1, 800 pixels is enough.

A 1,600-pixel version covers all three, and step 4 shows how to give phones and old screens a smaller file as well. Find the display width by inspecting the image in your browser’s developer tools, which show both the rendered size and the file’s real size. Then shrink it with the image resizer, which keeps the proportions and never enlarges.

2. Choose the format by what the image shows

ImageBest formatWhy
PhotosWebP or AVIF, JPG as fallbackLossy compression hides losses in fine detail
Logos, icons, charts, diagramsSVGDrawn from shapes, so sharp at any size and usually tiny
Screenshots, line artPNG, or WebP at high qualityLossless keeps text and edges crisp
Cut-outs with transparencyWebP (PNG as fallback)Keeps the transparent background at a fraction of PNG’s size

Mozilla’s guidance is the same: prefer WebP or AVIF for raster images, JPG as a fallback for photos, SVG for anything that must stay sharp at every size. Why a photo saved as PNG balloons is explained in lossy vs lossless compression, and where each newer format works in WebP, AVIF and JPEG XL support. For an existing site, JPG to WebP and PNG to WebP handle most images.

3. Set the quality, and check by eye

We ran one photo through the same browser encoders TinyPixr uses: a detailed 3,000 × 2,002 landscape of a waterfall, rocks and plants, saved as a 2.4 MB JPG by the camera. Detail like this is hard to compress, so a simpler photo would shrink further.

VersionPixelsFile sizeOf original
Camera original (JPG)3000 × 20022.4 MB100%
JPG 75%, not resized3000 × 2002985 KB40%
JPG 85%1600 × 1068416 KB17%
JPG 75%1600 × 1068310 KB13%
WebP 85%1600 × 1068355 KB15%
WebP 75%1600 × 1068241 KB10%
PNG (lossless)1600 × 10683.6 MB150%
WebP 75%, for 1× screens800 × 53478 KB3%

Three things stand out:

  • Resizing did the heavy lifting. At the same 75% quality, going from 3,000 to 1,600 pixels wide took the JPG from 985 KB to 310 KB.
  • WebP saved a further 20% or so at the same setting: 241 KB against 310 KB. The numbers on the two sliders aren’t the same scale, though, so compare the pictures, not the percentages.
  • PNG is the wrong tool for photos. The lossless version was larger than the camera’s original.

For photos on the web, start around 75–80% and look at the result at its real size, zooming in on edges, skies and skin. Lower it until you notice blur or blocks, then go one step back up. Screenshots and images with text need 90% or more. If a CMS or form has a hard limit, TinyPixr’s JPG and PNG compressor can find the highest quality that fits under a size in KB.

4. Deliver it with the right HTML

A well-made file can still load slowly, or make the page jump. Four attributes on the <img> element take care of that:

<img
  src="harbor-1600.webp"
  srcset="harbor-800.webp 800w, harbor-1200.webp 1200w, harbor-1600.webp 1600w"
  sizes="(max-width: 832px) calc(100vw - 32px), 800px"
  width="1600" height="1068"
  alt="Fishing boats in the harbor at sunset"
  fetchpriority="high">
  • srcset and sizes offer several widths and tell the browser how wide the image will be shown. The browser multiplies that by the screen’s pixel ratio and downloads the smallest file that is big enough, so the old monitor from step 1 gets the 800-pixel file and the phone gets 1,200.
  • width and height give the image’s proportions before it downloads, so the browser reserves the space and the text below doesn’t jump. Add width: 100%; height: auto in CSS and it still scales with the column.
  • loading="lazy" delays images until the reader scrolls near them. Use it for images further down the page, never for the main image at the top: that one is often the LCP element, and lazy-loading it makes the page feel slower.
  • fetchpriority="high" goes on that main image instead. Browsers start images at low priority until they know where they appear; this hint lets the main one start at once. In a web.dev test on the Google Flights homepage, it moved LCP from 2.6 to 1.9 seconds. Use it on one image per page, not all of them.

To offer AVIF or WebP with a JPG fallback, wrap the same attributes in a <picture> element, as shown in the format support guide.

A quick checklist

  • Is the file no wider than its display width × 2?
  • Is it a photo in WebP or JPG, and a logo or icon in SVG?
  • Did you check the quality at full size, on edges and skies?
  • Do all images have width, height and alt?
  • Is the top image eager with high priority, and the rest lazy?

TinyPixr does steps 1 to 3 in your browser, several images at once, without uploading them, and the new files it saves leave out camera metadata such as GPS location. See how to remove location data from photos for why that matters.

Frequently asked questions

What is the best image size for a website?

There is no single size. Measure how wide the image is shown in CSS pixels, then multiply by 2 for sharp results on high-resolution screens. An image shown 800 pixels wide needs a 1600-pixel file at most. A 1920-pixel image is plenty for most full-width pictures.

Which matters more, the format or the pixel size?

Usually the pixel size. In our test, resizing a 3000-pixel photo to 1600 pixels cut the file by more than two thirds at the same JPG quality, while switching from JPG to WebP at the same size saved another 15–22%. Do both, but resize first.

Should I lazy-load every image?

No. Lazy-load images further down the page, but not the ones visible when the page opens, and especially not the largest one. Google’s web.dev guidance warns that lazy-loading the main image delays Largest Contentful Paint, one of the Core Web Vitals.

Why should I set width and height if CSS sizes the image?

Browsers use the two attributes to work out the image’s aspect ratio before it downloads, so they can reserve the right amount of space. Without them, text jumps down when the image arrives. With width: 100% and height: auto in CSS, the image still scales with its container.

Does TinyPixr make AVIF files?

No. TinyPixr saves JPG, PNG and WebP, the formats browsers can create from a web page. For AVIF you need a desktop encoder or an image CDN; serve it with a WebP or JPG fallback.