Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 37 ยท Images, Fonts and Media

Image Formats and Sizes

Choose between JPEG, PNG, WebP, AVIF, GIF and SVG, learn lossy versus lossless compression, how pixel dimensions drive file size, and how to compress images for the web.

Images are usually the heaviest part of a page, often more than half of all bytes. That makes them the most rewarding place to optimise: choosing the right format, the right dimensions and the right quality can cut an image's weight by 80 percent with no visible change.

Raster and vector

  • Raster images are grids of pixels (JPEG, PNG, WebP, AVIF, GIF). Their size grows with pixel count.
  • Vector images (SVG) describe shapes with math. They stay sharp at any size and are tiny for logos and icons, but are a poor choice for photographs.

Lossy and lossless

  • Lossless compression keeps every pixel exactly (PNG, lossless WebP). Files are larger.
  • Lossy compression throws away detail the eye barely notices (JPEG, lossy WebP, AVIF). Files are much smaller, and the quality setting controls the trade-off.

Choosing a format

Format Best for Transparency Notes
AVIF Photos and graphics Yes Smallest files at a given quality; slower to encode; wide support
WebP Photos and graphics Yes 25 to 35% smaller than JPEG; very wide support
JPEG Photographs No Universal fallback
PNG Screenshots, sharp graphics needing lossless Yes Large for photos
SVG Logos, icons, illustrations Yes Tiny, scalable, can be styled; minify and sanitise
GIF Avoid Binary Use video or animated WebP/AVIF instead

A typical strategy is to serve AVIF or WebP to browsers that support them, with JPEG or PNG as a fallback (see responsive images).

If your image is a flat-colour logo, try SVG first. If it is a photo, try AVIF or WebP at a quality of about 60 to 80 and compare against the original.

Dimensions matter more than format

File size grows roughly with the number of pixels. A 4000 by 3000 photo has 12 million pixels; on a 400 px wide phone slot it needs under 1 million even on a 2x screen (720 by 540 = 0.39 million). Sending the full photo wastes about 97% of the pixels.

JS Wasted pixels calculator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Take the CSS width of the slot, multiply by the device pixel ratio (DPR) of the screen, and you know the pixel width worth sending. Most phones are 2x or 3x; going beyond 2x to 3x rarely looks different for photos, so many sites cap at 2x.

Comparing formats in the browser

A <canvas> can export the same drawing as different formats. This demo draws a gradient and checks how each encoder behaves. Sizes depend on the browser, so it prints only reliable facts.

Export one canvas as PNG, JPEG and WebP

The quality slider is the heart of lossy compression: lower quality means smaller files and, past a point, visible artefacts. Always check with your eyes, not only the number.

Compressing images in practice

  1. Resize to the largest size the layout ever shows (times 2 for high-density screens).
  2. Choose the format: AVIF or WebP with a JPEG or PNG fallback for photos, SVG for icons.
  3. Compress with a tool: Squoosh in the browser, sharp or libvips in a build, or an image CDN that converts on the fly.
  4. Strip metadata (camera data, colour profiles you do not need) to save a few kilobytes.
  5. Automate it: manual image work does not survive a busy team.
flowchart LR
  A["Original upload"] --> B["Resize variants"]
  B --> C["Encode AVIF + WebP + JPEG"]
  C --> D["Serve the best one"]

An image pipeline makes good practice the default.

SVG tips

  • Remove editor metadata and unused definitions (SVGO does this automatically).
  • Inline small, frequently used icons to save requests; use <img> or a sprite for large or repeated ones.
  • Never embed untrusted SVG without sanitising: it can contain script.

Animated content

An animated GIF can be ten times bigger than the same animation as MP4 or WebM. Use <video autoplay loop muted playsinline> instead of GIFs (see video and audio performance).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which format suits a flat-colour logo best?

    SVG is tiny, scalable and sharp at any size for shapes and logos.
  2. 2. What mainly drives raster image file size?

    More pixels and higher quality settings create larger files.
  3. 3. Which is usually the smallest at similar visual quality for photos?

    AVIF typically produces the smallest photo files, followed by WebP, then JPEG.
  4. 4. What should replace a large animated GIF?

    Video codecs compress motion far better than GIF.

Summary

  • Pick SVG for vector art, AVIF or WebP for photos with a JPEG or PNG fallback, and avoid GIF.
  • File size is driven by pixel count and quality; size images for their slot times the screen density.
  • Automate resizing, encoding and metadata stripping in a pipeline.

Next: Responsive Images and Lazy Loading