Image Formats for Speed: JPEG, PNG, WebP and AVIF
Pick the right image format for photos, logos and screenshots, then serve smaller files with fallbacks and responsive sizes.
Key takeaways
- Lossy compression discards detail you're unlikely to notice and suits photos, while lossless compression keeps every pixel and suits sharp-edged graphics.
- WebP and AVIF both handle lossy images, lossless images, transparency and animation, and AVIF also supports higher bit depths.
- Resizing an image to the dimensions it's actually displayed at usually matters as much as the format you choose.
- The
pictureelement lets you offer a newer format first and fall back to JPEG or PNG automatically. - Quality settings aren't comparable between encoders, so compare outputs by eye and by file size.
For most photographs on a website, AVIF or WebP will give you a noticeably smaller file than JPEG at similar visual quality. For logos, icons and line art, use SVG when you have a vector source, and lossless WebP or PNG when you don't. Keep JPEG and PNG as dependable fallbacks for tools and older software that don't read the newer formats.
Format is only half of the job, though. Serving an image at the right pixel dimensions often saves more than switching formats does. This guide covers both, with the markup and commands you need.
Lossy and lossless compression
Every common web image format makes one of two promises. Lossless compression stores the picture so that decoding returns exactly the original pixels. Lossy compression throws away information that human vision tends to miss, such as very fine color variation, in exchange for much smaller files.
Lossy compression works well on photographs because they're full of gradual shading and natural texture. It works badly on text, user interface screenshots and flat illustrations, where it produces fuzzy halos around hard edges.
Lossless compression is the reverse. It shrinks large areas of identical color very effectively, but a noisy photograph has few repeating patterns, so the file stays large.
Also note that lossy damage accumulates. Each time you open a JPEG, edit it and save it again as lossy, you lose a bit more. Keep an original master file and export web copies from that.
The four main formats
JPEG has been the web's photo format for decades. It's lossy in normal use, has no transparency, and opens in virtually everything. Progressive JPEG stores the image in passes, so a rough version appears early and sharpens as more data arrives.
PNG is lossless and supports full alpha transparency, meaning each pixel can be partly see-through. It's excellent for screenshots, diagrams and graphics with few colors. A palette-based PNG with 256 colors or fewer can be very small.
WebP was developed by Google and is supported by current mainstream browsers. It offers both lossy and lossless modes, alpha transparency and animation. One limit to know: each side of a WebP image can be at most 16,383 pixels.
AVIF stores images compressed with techniques from the AV1 video codec. It supports lossy and lossless modes, transparency, animation, and 10-bit and 12-bit color, which helps with high dynamic range images and smooth gradients. It often produces the smallest lossy files, especially at low to medium quality, but encoding takes considerably more processing time.
| Format | Compression | Transparency | Animation | Good fit |
|---|---|---|---|---|
| JPEG | Lossy | No | No | Photos, universal compatibility |
| PNG | Lossless | Yes | Only as APNG | Screenshots, diagrams, masters |
| WebP | Both | Yes | Yes | General web use |
| AVIF | Both | Yes | Yes | Photos where size matters most |
Two other formats come up often. GIF is limited to a 256-color palette per frame and makes very large animations; a muted looping video or animated WebP is usually far lighter. SVG describes shapes as text instead of pixels, so it stays sharp at any size. JPEG XL is another modern format, with browser support that has been uneven and changing, so check before relying on it.
Choosing by content
Vector files from design tools often carry editor metadata and needless precision. Running them through an SVG optimizer trims that without changing how they look. Be careful with SVG from untrusted sources, since the format can contain scripts.
Size the pixels first
A phone camera photo can be several thousand pixels wide. If your layout shows it in a column 800 CSS pixels wide, most of that data is wasted. A high-density screen benefits from up to about twice the CSS width, so roughly 1,600 pixels in this example. Beyond that, the visible gain is small.
Because pixel count grows with the square of the width, halving both dimensions leaves a quarter of the pixels. The file shrinks substantially, though not by an exact ratio.
Resizing removes pixels before any format choice is made. Hypothetical example: the numbers are made up to show the method, not a quote or an average.
You can resize in an editor, with an image resizer, or in a script. With ImageMagick:
magick photo.jpg -auto-orient -resize 1600x -strip -quality 80 photo-1600.jpg -auto-orient applies the camera's rotation flag before -strip removes metadata. Stripping metadata also removes location data, which you rarely want to publish. It removes color profiles too, so if your source uses a wide color space, convert it to sRGB first or keep the profile.
Converting to WebP and AVIF
The reference WebP encoder is cwebp:
cwebp -q 80 photo-1600.png -o photo-1600.webp
cwebp -lossless screenshot.png -o screenshot.webp For AVIF, avifenc from the libavif project is a common choice. Option names have changed between versions, so run avifenc --help to confirm what yours accepts. In recent versions:
avifenc -q 60 photo-1600.png photo-1600.avif Encode from the best source you have, preferably lossless, instead of from an already compressed JPEG. A quality number of 80 in one encoder doesn't mean the same thing as 80 in another. Pick a few representative images, export several settings, and view them at the size visitors will see. For a quick browser-based conversion, a WebP converter or image compressor works without installing anything.
Serving them with fallbacks
The picture element lists sources in order of preference. The browser takes the first type it understands, and the img inside is the fallback and the element that gets displayed.
<picture>
<source type="image/avif" srcset="photo-800.avif 800w, photo-1600.avif 1600w"
sizes="(max-width: 800px) 100vw, 800px">
<source type="image/webp" srcset="photo-800.webp 800w, photo-1600.webp 1600w"
sizes="(max-width: 800px) 100vw, 800px">
<img src="photo-800.jpg" srcset="photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 800px) 100vw, 800px"
width="800" height="533" alt="Cyclists crossing a stone bridge">
</picture> The srcset list tells the browser which widths exist, and sizes tells it how wide the image will be in the layout. It then picks a file to suit the screen, which is a core technique in responsive web design. The width and height attributes reserve space so the page doesn't jump when the image arrives.
An alternative is to keep one image URL and let the server or CDN choose a format from the browser's Accept header. That's content negotiation. If you do it, send Vary: Accept and confirm your cache honors it, or a visitor could receive a format their software can't read. Many content delivery networks offer on-the-fly resizing and conversion that handles this for you.
Images that arrive as uploads
Sites with many contributors can't rely on everyone exporting carefully. Handle it automatically. When a file is uploaded, check that it really is an image, apply its rotation, remove location data, and generate the widths your templates use. Store the original privately so you can regenerate variants when the design changes or a better encoder appears.
Set limits too. A maximum file size and maximum pixel dimensions protect your server, because decoding a very large image takes a lot of memory even when the file on disk is small. A lossless format stored for a photograph is another frequent accident: someone saves a camera picture as PNG, and it ends up several times larger than a JPEG of the same view. Your pipeline can detect photographic uploads or simply convert large PNGs without transparency to a lossy format, with a way to override for graphics that must stay exact.
Name files so that a changed picture gets a new address. That lets you cache each variant for a long time without visitors seeing stale versions.
What can go wrong
- Banding in skies and gradients. Raise quality or use a higher bit depth.
- Blurry text in screenshots. You used lossy compression; switch to lossless.
- A "newer" file that's bigger. This happens, especially when converting small or already optimized images. Serve whichever is smaller.
- Uploads that break elsewhere. Some email clients, office software and upload forms still reject WebP or AVIF, so keep JPEG or PNG copies for download.
- Slow builds. Encode AVIF once and cache the result instead of regenerating on every request.
Images are often the largest element on screen, so they strongly influence Core Web Vitals, particularly how quickly the main content appears. Once formats and sizes are sorted, defer off-screen pictures with lazy loading, and write useful alt text for the ones that carry meaning.
Common questions
Should I convert my whole existing library?
Start with the images that are large and frequently viewed, such as home page and article lead images. Converting thousands of tiny thumbnails that few people load gives little return.
Do I still need a JPEG fallback?
Current browsers read WebP widely, and AVIF support is broad, but feed readers, link preview generators and older devices vary. A fallback costs some storage and very little effort once your pipeline produces it automatically.
Does a different format help search visibility?
Faster pages are better for visitors, and search engines can index common modern formats. Descriptive filenames, alt text and surrounding text tell a search engine far more about an image than its file type does.