Sub topic 7
GIF: why it is rarely the right choice
This chapter assumes you know the difference between lossy and lossless compression and between raster and vector images. It also assumes you can read a short HTML snippet.
What GIF is
GIF (Graphics Interchange Format) is a raster format from 1987. It stores each frame as an indexed image with a palette of at most 256 colors. It can hold several frames, which is why people use it for short looping animations.
Why it falls short
- Limited color. A photo or gradient squeezed into 256 colors shows banding and speckled dithering. The compression itself is lossless, but reducing the palette discards color information before it runs.
- Large files. GIF compresses each frame poorly and has no way to store only what changed between frames. A 3-second screen recording can easily exceed several megabytes. The same clip as MP4 or WebM is often 5 to 20 times smaller.
- Hard-edged transparency. Each pixel is fully transparent or fully opaque. Soft shadows and smooth edges leave a jagged halo on non-matching backgrounds.
- No audio, no playback control. The browser cannot pause the animation or let the user scrub it, and it will not skip frames on a slow device.
Better replacements
| Need | Use instead |
|---|---|
| Screen recording or video-like clip | MP4 or WebM in a `video` element |
| Short animation with transparency | Animated WebP or AVIF |
| Simple animated icon or logo | SVG with CSS animation |
| Still image | JPEG, PNG, WebP, or AVIF |
Convert an existing GIF to MP4 with ffmpeg. The scale filter rounds the dimensions to even numbers, which the H.264 codec requires.
ffmpeg -i demo.gif -movflags faststart -pix_fmt yuv420p \
-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" demo.mp4Then embed it so it behaves like a GIF. Browsers allow autoplay only for muted video, and playsinline keeps iOS from opening it fullscreen.
<video src="demo.mp4" width="640" height="360"
autoplay loop muted playsinline
aria-label="Dragging a card between columns"></video>If you need an image file instead, gif2webp -lossy -q 80 demo.gif -o demo.webp produces an animated WebP.
When GIF is still acceptable
- Email, where many clients block video and animated WebP.
- Tiny, flat-color animations of a few kilobytes, such as a small loading spinner.
- Platforms that accept only GIF uploads.
Exporting a GIF when you must
- Keep the dimensions at the displayed size. Do not export 2x GIFs.
- Cut the frame rate to 10 to 15 fps.
- Reduce the palette to 64 or 128 colors and check for banding.
- Trim the length and loop count to the minimum.
Exercise
A teammate exports a 4-second product demo with a photo background as a 6 MB GIF. Name two problems with this choice and the format you would use.
Show answer
The GIF's 256-color palette causes banding and dithering on the photo, and the file is far larger than it needs to be because GIF stores frames inefficiently. Export it as MP4 or WebM and embed it in a muted, looping video element.