Images are often among the largest resources on a web page, and a single unoptimized hero image can outweigh all the HTML, CSS, and JavaScript combined. Reducing them is usually one of the simplest performance improvements available.

This guide covers choosing a format, compressing, serving responsive images, and lazy loading.

Why Image Size Matters

Images affect two of Google's three Core Web Vitals. Largest Contentful Paint (LCP) measures when the largest visible element finishes rendering, and that element is often an image. Cumulative Layout Shift (CLS) measures how much content jumps around, which happens when images load without reserved space. A 3 MB hero JPEG will hurt LCP on most connections.

Slower pages are harder to use on mobile networks, and oversized images also add to your bandwidth bill.

Image Formats Compared

Choosing the right format is the first and often the most impactful optimization decision. Here is how the major formats stack up:

Format Best For Compression Transparency Browser Support
JPEG Photos, complex images Lossy No Universal
PNG Screenshots, graphics with text Lossless Yes Universal
WebP General purpose replacement Both Yes All current browsers
AVIF Maximum compression Both Yes All current browsers (Safari 16.4+)
SVG Icons, logos, illustrations Vector (N/A) Yes Universal

JPEG

JPEG remains the standard for photographs. It uses lossy compression, discarding visual information to reduce file size. At quality 80 to 85, most photos look very close to the original. The downsides are no transparency and quality loss with each re-save.

PNG

PNG uses lossless compression, so every pixel is preserved exactly. It suits screenshots, diagrams, and images with sharp text. A photograph saved as PNG will usually be several times larger than a JPEG of similar visual quality.

WebP

WebP was developed by Google and supports lossy and lossless compression plus transparency. Google's published comparison found lossy WebP 25 to 34% smaller than JPEG at equal quality, and lossless WebP about 26% smaller than PNG. Results vary by image, so test your own. The Image Format Converter can convert to WebP in browsers that can encode it.

AVIF

AVIF is based on the AV1 video codec and often beats WebP on file size, especially at lower quality settings. The trade-off is slow encoding. Browser support is good (Chrome 85, Firefox 93, Safari 16.4). The image tools on this site cannot write AVIF, so use a command-line encoder:

# WebP, using libwebp
cwebp -q 80 photo.jpg -o photo.webp

# AVIF, using libavif
avifenc -q 60 photo.jpg photo.avif

SVG

SVG is a vector format, so it scales to any size without losing quality. It suits logos, icons, and simple illustrations. SVGs are XML text, so they can be styled with CSS and compressed with gzip. The SVG Converter goes the other way, turning an SVG into a PNG or JPG for places that do not accept SVG.

Compression: Lossy vs. Lossless

The choice is a trade-off between file size and visual quality.

Lossy compression permanently removes visual information that the eye is less sensitive to. Each step down in quality saves less and costs more: moving from quality 100 to 85 often saves a large share of the file with little visible change, while lower settings start to show artifacts. Quality 75 to 85 is a common starting range for photos, and encoders differ, so compare the results by eye.

Lossless compression reduces file size without discarding any information by finding and encoding patterns more efficiently. PNG and lossless WebP use this approach. The compression ratios are smaller, but you get a bit-perfect reproduction.

You can compare quality levels with the Image Compressor, which accepts PNG, JPEG, and WebP and does its work in your browser.

Responsive Images

A single image size does not fit all screens. Serving a 2400px-wide hero image to a 375px phone viewport wastes enormous bandwidth. HTML provides several tools to solve this.

The srcset Attribute

The srcset attribute lets you provide multiple image sources at different widths. The browser picks the best one based on the viewport and device pixel ratio:

<img
  src="hero-800.jpg"
  srcset="hero-400.jpg 400w,
          hero-800.jpg 800w,
          hero-1200.jpg 1200w,
          hero-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw,
         (max-width: 1200px) 80vw,
         1200px"
  alt="Mountain landscape at sunset"
>

The sizes attribute tells the browser how wide the image will be rendered at different viewport widths, so it can make an informed choice before the image downloads.

The Picture Element

For format negotiation and art direction, use the <picture> element. This lets you serve AVIF to browsers that support it, WebP as a fallback, and JPEG as the final fallback:

<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img src="hero.jpg" alt="Mountain landscape at sunset">
</picture>

You can use our Image Resizer to generate the multiple size variants you need for responsive images.

Lazy Loading

Lazy loading defers the loading of offscreen images until the user scrolls near them. This is one of the simplest performance wins you can implement.

Native Lazy Loading

Modern browsers support the loading="lazy" attribute natively. Just add it to your image tags:

<img src="product-photo.jpg" loading="lazy" alt="Product photo">

Do not lazy-load images that are visible on first load. Your hero image should load eagerly, and it can be given fetchpriority="high" so the browser requests it early. Lazy loading it delays LCP.

Intersection Observer

For more control, you can use the Intersection Observer API to add custom logic like fade-in animations or placeholder swapping. The key idea is to store the real URL in a data-src attribute and swap it into src when the element enters the viewport. Setting a rootMargin of 200 pixels gives images a head start before the user scrolls to them.

Image CDNs and Automated Optimization

Image CDNs like Cloudflare Images, Imgix, and Cloudinary can automatically resize, compress, and convert images on the fly. You pass parameters in the URL, and the CDN generates the optimized variant on demand:

<!-- Cloudflare example -->
<img src="/cdn-cgi/image/width=800,quality=80,format=auto/original.jpg">

<!-- Imgix concept -->
<img src="https://example.imgix.net/photo.jpg?w=800&q=80&auto=format">

These services charge for usage. For a handful of images, you can resize and convert them yourself with the Image Compressor or Image Format Converter, though they do not replace automatic per-device delivery on a site with many images.

Advanced Techniques

Base64 Inline Images

For very small images (under 2 KB), you can embed them directly in your HTML or CSS as Base64 data URIs. This eliminates an HTTP request at the cost of roughly 33% larger file size due to Base64 encoding. Our Image to Base64 Converter makes this conversion instant.

Upscaling

When you only have a low-resolution source, interpolation can enlarge it, though it cannot add real detail. The Image Upscaler offers Bilinear, Bicubic, Lanczos, and Nearest Neighbor scaling at 2x, 3x, or 4x with optional sharpening, and it runs in your browser. Nearest Neighbor suits pixel art.

Practical Optimization Checklist

A checklist for a site's images:

  1. Audit your current images. Open DevTools, go to the Network tab, filter by "Img", and sort by size. Identify the biggest offenders.
  2. Choose the right format. Photos get JPEG or WebP. Graphics with transparency get PNG or WebP. Icons and logos get SVG. Use AVIF if your audience supports it.
  3. Resize to actual display dimensions. Never serve a 4000px image that displays at 800px. Generate multiple sizes for responsive layouts.
  4. Compress at the right quality level. Start at quality 80 for JPEG/WebP and compare visually. Drop to 70 or 75 if the savings are significant and the quality is still acceptable.
  5. Implement responsive images. Use srcset and sizes for resolution switching. Use <picture> for format negotiation.
  6. Add lazy loading. Apply loading="lazy" to every image below the fold. Keep above-the-fold images eager.
  7. Set explicit dimensions. Always include width and height attributes (or use CSS aspect-ratio) to prevent layout shift (CLS).
  8. Use a CDN. Serve images from a CDN with proper cache headers. Set Cache-Control: public, max-age=31536000, immutable for versioned assets.
  9. Test with Lighthouse. Run an audit and read the image findings. Newer versions group them under "Improve image delivery", and older versions list "Properly size images" and "Serve images in next-gen formats".

Tools Mentioned in This Guide

These all process images in your browser, with no upload and no sign-up: