On most pages, images account for the majority of the bytes transferred. That makes format choice one of the highest impact performance decisions available, and it is one people usually make by uploading whatever came out of the camera.
Lossy and lossless
The first distinction is how compression works. Lossy compression discards information permanently to achieve a smaller file, accepting some quality reduction. Lossless compression reduces size without discarding anything, so the image reconstructs exactly.
Lossy suits photographs, where small imperfections are invisible against natural detail and gradients. Lossless suits graphics with flat areas and hard edges, where the artefacts lossy compression introduces are highly visible as fuzz around lines and text.
Lossy compression also compounds. Each save discards more information, so repeatedly editing and re-saving a photograph degrades it progressively. Keep an original in a lossless format and export compressed copies from it rather than editing the compressed version.
JPEG
The long standing default for photographs. It is lossy, it has universal support, and its quality setting gives fine control over the tradeoff between size and appearance.
Its weaknesses are that it has no transparency and handles sharp edges badly, so text and line art acquire visible fuzz. A quality setting between sixty and eighty is usually indistinguishable from maximum at a fraction of the size, and pushing to maximum wastes a great deal of bandwidth for a difference nobody can see.
PNG
Lossless, with full support for transparency including partial transparency, which is what allows soft shadows and antialiased edges to blend against any background.
It is the right choice for logos, icons, screenshots and anything with flat colour and crisp edges. It is a poor choice for photographs, where a lossless file will typically be several times the size of an equivalent JPEG with no perceptible benefit.
Two variants exist. The eight bit form uses a limited palette and produces very small files for simple graphics. The twenty four bit form supports millions of colours and is what most tools export by default.
WebP
WebP supports both lossy and lossless compression, handles transparency, and supports animation. It typically produces files around a quarter to a third smaller than an equivalent JPEG or PNG at comparable quality.
Support is now universal across current browsers, which makes it a reasonable default for new work. It genuinely replaces both older formats rather than sitting alongside them, which is unusual and is why it has been adopted so widely.
AVIF
AVIF compresses harder still, often producing noticeably smaller files than WebP at similar quality, with support for transparency, wide colour and high dynamic range.
The tradeoffs are encoding time, which is considerably slower, and support that while broad is not quite as complete as WebP. It is best deployed as an enhancement, offered first with a fallback behind it, rather than as the only version you publish.
SVG
SVG is a different kind of thing entirely. It describes shapes mathematically rather than storing pixels, so it scales to any size without losing sharpness and is usually tiny for simple graphics.
Use it for logos, icons, diagrams and charts. It cannot represent photographs usefully, and a complex illustration with thousands of paths can end up larger and slower to render than a bitmap would be.
Offering several formats
The picture element lets you supply modern formats with older ones as a fallback, and the browser selects the first it can display.
<picture> <source srcset="/img/photo.avif" type="image/avif"> <source srcset="/img/photo.webp" type="image/webp"> <img src="/img/photo.jpg" alt="A harbour at dusk"> </picture> The image element at the end is required and provides both the fallback and the alt text.
GIF is obsolete for animation
Animated GIF survives out of habit rather than merit. It is limited to a palette of 256 colours, compresses poorly, and an animation of any length produces a file far larger than a short video of the same content. Replacing an animated GIF with a muted, looping, autoplaying video typically cuts the size by an order of magnitude while improving the quality.
Static GIFs have no remaining use at all, since PNG does everything they do and does it better.
A workable default
Photographs as WebP with a JPEG fallback, graphics and icons as SVG where possible and PNG otherwise, and AVIF added for large hero images where the saving justifies the encoding cost. Resize files to the dimensions they will actually display at before compressing, because serving an image far larger than its rendered size wastes more bandwidth than any format choice can recover.