Sub topic 13
Choosing formats and compression
This chapter assumes you can write an img element, use srcset and sizes to serve different widths, and have already chosen your crops. Here you pick the file format and the compression level for each of those sizes.
Why encoding still matters
Serving the right dimensions removes most wasted bytes. The rest depends on how the pixels are stored. The same 800 px photo can weigh 300 KB as a PNG and 60 KB as a well-tuned WebP. The two look almost identical.
Formats for photos
| Format | Compression | Good for | Watch out for |
|---|---|---|---|
| JPEG | Lossy | Photos everywhere, safest fallback | No transparency, visible blocks at low quality |
| WebP | Lossy or lossless | Photos with broad browser support | Slightly less efficient than AVIF |
| AVIF | Lossy or lossless | Smallest photo files | Slow to encode, can look soft at low quality |
| PNG | Lossless | Screenshots, flat graphics, transparency | Very large for photos |
Lossy compression discards detail the eye rarely notices. Lossless compression keeps every pixel and shrinks the file less. For photographs, lossy is almost always the right choice.
Setting the quality
Quality is a number from 0 to 100, but the scales differ between encoders. Settings that look similar are often around 75 to 82 for JPEG and WebP, and around 50 to 65 for AVIF. Treat these as starting points and compare the result at the size it will display.
Follow this order:
- Keep an uncompressed or high-quality master file.
- Crop and resize from the master for each target width.
- Compress each size once, as the last step.
Never re-save an already compressed JPEG as a new JPEG. Each pass adds artifacts, which are visible flaws such as blockiness or smearing.
Serving modern formats with a fallback
The picture element lets the browser choose. It uses the first source whose type it supports. Each source can carry its own srcset and sizes.
<picture>
<source type="image/avif"
srcset="team-400.avif 400w, team-800.avif 800w, team-1600.avif 1600w"
sizes="(min-width: 960px) 50vw, 100vw">
<source type="image/webp"
srcset="team-400.webp 400w, team-800.webp 800w, team-1600.webp 1600w"
sizes="(min-width: 960px) 50vw, 100vw">
<img
src="team-800.jpg"
srcset="team-400.jpg 400w, team-800.jpg 800w, team-1600.jpg 1600w"
sizes="(min-width: 960px) 50vw, 100vw"
width="800" height="600"
alt="Four colleagues around a whiteboard">
</picture>The img is required. It holds the fallback JPEG, the alt text, and the width and height that reserve space. Browsers that don't recognize picture still show the img.
Common mistakes
- Saving photos as PNG because it "looks sharper". The file is often five times larger.
- Compressing before cropping, then cropping and compressing again.
- Judging quality at 300% zoom instead of at display size.
- Leaving metadata in the file. Cameras embed EXIF data, which can include GPS coordinates. Strip it before publishing.
A rough budget
A full-width hero photo should usually stay under 200 KB, and a card thumbnail under 40 KB. These are guidelines, not rules. If a file is over budget, lower the quality a few points or serve a smaller width before changing the crop.
Exercise
You export a 1600 px team photo as a JPEG at quality 90, then open it, crop it, and save it as a JPEG again at quality 80. What two problems does this workflow have, and how do you fix them?
Show answer
It compresses twice, so artifacts build up, and it crops after compression instead of from the master. Fix it by keeping the original master, cropping and resizing from it, and compressing each output once as the last step.