Sub topic 7
GIF: why it is rarely the right choice
This chapter assumes you know the raster/vector split, the idea of lossless versus lossy compression, and the basics of PNG and JPEG from earlier chapters. You should also know what a color palette is in a design tool. By the end you will know what GIF does, where it fails, and what to export instead.
What GIF actually is
GIF (Graphics Interchange Format) dates from 1987, with animation and transparency added in the 1989 revision. It is a raster format that stores each frame as an indexed image: a palette of at most 256 colors, plus one palette index per pixel. The index data is compressed with LZW, a lossless method.
Three limits follow from that design:
- 256 colors per frame. Photos, gradients, and soft shadows need thousands of distinct colors. GIF approximates them with dithering, which scatters dots of palette colors to imitate in-between shades. The result looks grainy, and the dots hurt compression.
- 1-bit transparency. A pixel is either fully transparent or fully opaque. There are no partial alpha values, so anti-aliased edges and drop shadows cannot blend smoothly.
- No inter-frame video compression. GIF can skip pixels that did not change between frames, but it has no motion prediction. Every frame carries much more data than a video codec would store for the same scene.
GIF is lossless only after the palette reduction. The reduction itself is the lossy step, and it happens in your export dialog or in the converter you use.
Where GIF breaks down
Photos and gradients
A photograph exported as GIF is usually larger than the JPEG and looks worse. A smooth gradient shows visible bands where the palette runs out of intermediate colors. Dithering hides the bands but adds noise.
Edges on varied backgrounds
With 1-bit transparency, the exporter must decide how to treat the soft edge of a shape. If you export against a white matte and the page is dark, a light fringe (a halo) shows around the shape. If you export with no matte, the edge looks jagged. PNG, WebP, and AVIF avoid both problems with 8-bit alpha. See the transparency chapter for the details.
Animation weight
Animated GIFs are the main reason people still use the format, and they are also where it costs the most. A few seconds of screen recording or video can reach several megabytes. That is often larger than the rest of the page combined, and it competes with your other images for bandwidth.
When GIF is still acceptable
GIF is the right answer in few situations:
- Email. Many email clients block scripts and video but display animated GIFs. Keep them small, and make the first frame carry the message, because some clients (notably older Outlook versions) show only that frame.
- Platforms that only accept GIF. Some chat tools, forums, and CMS fields take only GIF for animation.
- Tiny, flat, short loops. A 32-pixel spinner with four colors and ten frames can be a few kilobytes. At that size, an alternative may not be worth the effort.
Outside those cases, pick something else.
Alternatives by use case
| Need | Better choice | Why |
|---|---|---|
| Screen recording or video clip | MP4 or WebM in a `video` element | Motion-aware codecs, usually a small fraction of the GIF's size |
| Animated image that must stay an `img` | Animated WebP or animated AVIF | 24-bit color, 8-bit alpha, smaller files |
| Animated icon or UI illustration | SVG with CSS animation, or a Lottie file | Vector, sharp at any size, tiny |
| Static image with transparency | PNG, WebP, or SVG | Full alpha, more colors |
| Animated lossless pixel art | APNG or animated WebP (lossless) | Full color, alpha |
APNG (Animated PNG) is supported by current browsers, though design tools rarely export it directly.
Converting a GIF you already have
Designers often receive a GIF and have no source file. Convert it with command-line tools. ffmpeg handles the video formats, and gif2webp ships with the WebP tools.
# MP4 (H.264). The scale filter forces even dimensions, which yuv420p requires.
ffmpeg -i demo.gif -movflags +faststart -pix_fmt yuv420p \
-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -an demo.mp4
# WebM (VP9), usually smaller than the MP4 at similar quality.
ffmpeg -i demo.gif -c:v libvpx-vp9 -b:v 0 -crf 33 -an demo.webm
# Animated WebP from the same GIF.
gif2webp -lossy -q 75 -m 6 demo.gif -o demo.webpIn the first two commands, -an drops the audio track, which a GIF never had. In the third, -m 6 makes the encoder work slowest and smallest, and -lossy allows lossy compression, which usually beats the default lossless mode for photographic content.
Then compare the sizes:
ls -l demo.gif demo.mp4 demo.webm demo.webpEmbedding a video as a GIF replacement
A video replaces a GIF only if it behaves like one: it plays by itself, loops, and makes no sound. Browsers allow autoplay only for muted video, so the attributes below are required.
<video autoplay loop muted playsinline
width="480" height="270"
poster="demo-poster.jpg"
aria-label="Screen recording: dragging a card between columns">
<source src="demo.webm" type="video/webm">
<source src="demo.mp4" type="video/mp4">
</video>playsinlinestops iOS from forcing full-screen playback.postershows a still image while the video loads, and it is your fallback if playback is blocked.widthandheightreserve space so the page does not shift when the video loads, the same rule as forimg.- The browser takes the first
sourceit can play, as withpicture.
Exporting a GIF when you must
If GIF is the required format, reduce its cost:
- Cut dimensions. Export at the displayed size, not 2x. Width and height drive the size more than any other setting.
- Cut frame rate. 10 to 15 frames per second is enough for most interface demos. Dropping from 30 to 15 roughly halves the frame data.
- Cut length. Trim idle time at the start and end, and loop short sequences.
- Limit colors. Try 64 or 128 instead of 256 and compare the result.
- Prefer flat color. Remove gradients and noise from the source before export, since both defeat compression.
- Choose dithering deliberately. Use none for flat art and diffusion for photos. Dithering enlarges the file.
- Use an optimizer. Tools such as gifsicle can recompress and merge unchanged regions between frames:
gifsicle -O3 --colors 128 --lossy=40 input.gif -o output.gifThe --lossy option exists in the gifsicle fork maintained by Kornel Lesiński. Check gifsicle --help for your version.
Common mistakes
- Exporting a static logo as GIF because "it has transparency." Use SVG or PNG.
- Using a GIF for a hero animation and then finding that it is 8 MB. Check the size against your budget before handoff.
- Putting a looping GIF next to body text with no way to pause it.
- Forgetting
alttext on an animatedimg. Describe what the animation shows, as you would for any image.
Exercise
A teammate sends you a 6 MB, 10-second GIF screen recording for a landing page and asks you to make it load faster. Name the format you would export, give the two attributes the HTML must include for it to behave like a GIF, and explain one accessibility requirement.
Show answer
Convert it to MP4 (H.264) and WebM, and embed both in a video element. The element needs muted and autoplay (plus loop, and playsinline for iOS), because browsers only autoplay muted video. For accessibility, the animation lasts more than five seconds and starts automatically, so WCAG 2.2.2 requires a way to pause or stop it, for example a visible control or respecting the reduce-motion setting. Also add width and height to prevent layout shift.
Exercise
Why does a GIF of a soft drop shadow look wrong on a dark page when it was exported against a white background?
Show answer
GIF has only 1-bit transparency, so partially transparent shadow pixels cannot be stored. The exporter blends them with the white matte color, which leaves a light fringe on the dark background. PNG, WebP, or AVIF with 8-bit alpha avoids this.