Sub topic 10
Transparency and animation across formats
This chapter assumes you can export images from a design tool and know the difference between lossy and lossless compression. It covers two features that decide which format you can use: transparency and animation.
How transparency works
Transparency lets the page background show through parts of an image. A format stores it in one of two ways:
- Binary transparency: each pixel is either fully visible or fully invisible. Edges look jagged on curved shapes.
- Alpha transparency: each pixel has an alpha value, a number that sets its opacity from fully clear to fully solid. This gives smooth edges and soft shadows.
| Format | Transparency | Animation |
|---|---|---|
| JPEG | None | No |
| PNG | Full alpha | No (APNG is a rarely used extension) |
| GIF | Binary only, 256 colors | Yes |
| WebP | Full alpha, lossy or lossless | Yes |
| AVIF | Full alpha | Yes |
| SVG | Full alpha, as vector shapes | Yes, through CSS or SMIL |
Common transparency errors
The most common error is exporting a logo with a soft shadow as a GIF. GIF has only on/off transparency, so the shadow turns into a hard, dirty fringe. Use PNG, WebP, or AVIF instead.
A second error is exporting a transparent area as JPEG. JPEG cannot store alpha, so the design tool fills the area with white or black. On a colored page, this shows up as a box around the image.
A third error is a halo. It appears when an image is exported with transparency over a light background and then placed on a dark one. Export with a transparent canvas, not a flattened one, and check the result on both light and dark backgrounds.
Animation
Animation is a sequence of frames shown over time. You have three practical choices:
- Animated GIF: works everywhere, but files are large and colors are limited to 256.
- Animated WebP or AVIF: much smaller files with full color and alpha. Browser support is good for WebP and newer for animated AVIF.
- Video: an MP4 or WebM file is usually the smallest option for long or photographic motion. It has no alpha in common formats.
For simple icon motion, such as a spinner, animate an SVG with CSS instead of using frames. The result is tiny and sharp at any size.
Providing a fallback
Use a picture element so older browsers get a format they can show. This example serves a transparent logo in WebP, with a PNG fallback.
<picture>
<source type="image/webp" srcset="badge.webp">
<img src="badge.png" width="200" height="200" alt="Award badge">
</picture>Exercise
A designer exports a product cutout with a soft drop shadow as a GIF to keep the file small. The shadow looks rough on the white page. Why, and what should they use instead?
Show answer
GIF supports only binary transparency, so each pixel is either solid or clear, and a soft shadow needs partial opacity. It also limits colors to 256. They should export PNG, or WebP or AVIF with a PNG fallback, because these support full alpha transparency.
Exercise
A 6-second screen recording of an app needs to loop on a landing page. Which of the three animation choices is usually smallest, and what does it give up?
Show answer
Video (MP4 or WebM) is usually smallest for long, photographic motion. It gives up alpha transparency in the common formats, so it cannot show the page background through it.