Ch. 20 · Optimizing Web Images and Animations

한국어日本語
7 min

Web Image Formats: What to Use and When

Photos go in a lossy format (JPEG, WebP, AVIF); logos and screenshots go in a lossless one (PNG, lossless WebP). Newer formats tend to be smaller, but you have to weigh support and encoding time, so keep the most widely supported file as the fallback.

The principles behind image codecs were covered in Chapter 4. This chapter doesn't repeat them; it focuses on what to pick when you actually put an image on a web page. One thing up front: the measurement articles on this site have only covered video (video codecs), and we have not measured the size or quality of image formats. So this chapter has no image numbers measured by this site, and the size comparisons between formats stay at the 'is known to' level of explanation given in Chapter 4.

First, sort images by type

Image typeExamplesDefault choiceWhy
Photos and images with lots of gradientsProduct photos, article hero imagesLossy format (JPEG / WebP / AVIF)Discarding fine detail is hard to notice, so the size savings are large
Images with lots of lines, text, and flat color areasLogos, charts, UI screenshotsLossless (PNG / lossless WebP) or SVGLossy compression smears the area around edges (ringing). Ringing shows up in images as well as video
Images that need a transparent backgroundStickers, cut-out logosPNG / WebP / AVIFThese three formats support an alpha channel. JPEG has no alpha
Moving imagesShort demos, reaction clipsShort video (MP4/WebM)See Lesson 5. GIF is a last resort

One line per format

  • JPEG — Opens on nearly every device. It is the last-resort fallback for environments that can't handle newer formats. No alpha (transparency).
  • PNG — Lossless, with alpha support. Files tend to get large with photos; it suits charts and screenshots.
  • WebP — One format that supports VP8-based lossy, a separate lossless mode, alpha, and animation (Chapter 4 comparison table). Its wide support makes it a common 'next choice after JPEG'.
  • AVIF — Based on AV1 intra frames. It is known to be 40–50% smaller than JPEG on average at the same quality (Chapter 4). The trade-off is that encoding tends to be slow; the upside is broad color and HDR support.
  • JPEG XL — Its distinguishing feature is that it can recompress an existing JPEG losslessly. However, browser support has changed repeatedly over a long period, so follow the checking method in Lesson 2.
The 'smallest format' is not the 'best choice'

Even if AVIF is smaller, encoding takes longer, and on an environment that doesn't support it, nothing shows up at all. The principle of this chapter is: always keep one file that opens almost everywhere, and layer the more efficient formats on top as an option. It's the same idea as the web embed recipe, where H.264 was the fallback for video.

In the field: compare using the same photo yourself

A general claim like 'AVIF is X% smaller' varies with the type of photo. Just as the CRF result split widely from clip to clip for the same video (Why the same CRF gives different results), the surest approach for images is to convert one or two of your own photos and compare file size and visual quality. This site does not vouch for those numbers for images.

Alpha channelAlpha channel

A channel that stores per-pixel transparency separately from color (RGB). PNG, WebP, and AVIF support it; JPEG does not.

Lossy / LosslessLossy / Lossless

Lossy shrinks the file by discarding information that's less noticeable; lossless compresses so the original can be restored exactly.

FallbackFallback

A more widely supported substitute file prepared for browsers that don't support the new format.

Check yourself

You're putting a company logo (flat color areas and lines, transparent background) on the web. What should you consider first?

Check yourself

Which statement matches this chapter's stance on size comparisons between image formats?