Sub topic 10
Transparency and animation across formats
This chapter assumes you know the difference between raster and vector images, and between lossy and lossless compression. It also assumes you have a basic idea of what JPEG, PNG, GIF, WebP, AVIF, and SVG are. Here you will compare how those formats handle transparency and animation. You will then pick a format for a transparent or moving image.
Two separate features
Transparency and animation are different features. Some formats have one, some have both, and some have neither.
- Transparency means some pixels are partly or fully see-through, so the page background shows through.
- Animation means a file holds several frames that play in sequence.
Designers often mix the two up. A GIF can animate and has transparency, but only a crude kind. A PNG has excellent transparency but does not animate in the standard form. Check each feature on its own.
How transparency is stored
Raster formats store color in channels. A normal image has red, green, and blue channels. A transparent image adds a fourth, the alpha channel. Alpha says how opaque each pixel is.
There are two kinds of transparency:
- Binary transparency. Each pixel is either fully visible or fully transparent. There is nothing in between.
- Alpha transparency. Each pixel has a level of opacity, usually 256 steps from 0 (invisible) to 255 (solid).
Binary transparency causes jagged edges. Suppose you cut out a round logo and place it on a dark page. The edge pixels are a blend of the logo color and the original light background. With only on and off, the exporter must either keep those blended pixels, which leaves a light halo, or drop them, which leaves a stair-stepped edge. Alpha transparency avoids both, because edge pixels can be partly see-through.
Format comparison
| Format | Transparency | Animation | Notes |
|---|---|---|---|
| JPEG | No | No | Transparent areas become a solid color, usually white or black. |
| PNG | Full alpha | Not in the standard form | Lossless. Large for photos. |
| GIF | Binary only | Yes | Limited to 256 colors per frame. |
| WebP | Full alpha, lossy or lossless | Yes | One format covers both features. |
| AVIF | Full alpha | Yes | Smaller files, slower to encode, and animation support is narrower than WebP. |
| SVG | Full alpha, plus per-shape opacity | Yes, through CSS or SMIL | Vector only. Not suitable for photos. |
A few points need more detail.
JPEG cannot be transparent
If you export a transparent layer as JPEG, the tool fills the empty area with a matte color. Pick that color to match the page background. A white matte on a dark page leaves a visible white box.
PNG has the best-known transparency
PNG-24 with alpha is the standard choice for a transparent raster image with soft edges, such as a drop shadow or a cut-out product photo. Some tools also offer PNG-8, which uses a palette of up to 256 colors. PNG-8 can carry transparency and gives smaller files for flat graphics, but gradients and shadows may show banding.
A standard PNG holds one image. An animated variant, APNG, exists and is supported by current browsers, but few design tools export it, so it is rare in practice.
GIF is limited on both counts
GIF allows 256 colors per frame and binary transparency only. That is why GIF gradients look grainy and GIF cut-outs show halos. GIF files of video-like content are also very large. If you need a short looping animation, prefer one of the alternatives below.
WebP and AVIF combine both features
WebP supports alpha in both its lossy and lossless modes. This is a useful property. A lossy WebP with alpha is often much smaller than the equivalent PNG, because the color data is compressed lossily while the alpha channel is stored separately. WebP also supports animation, and an animated WebP is usually far smaller than the same animation as a GIF.
AVIF supports alpha and animation too. Its files are often the smallest, but encoding is slow and support for animated AVIF is less uniform across tools and browsers than for animated WebP. Test before you rely on it.
SVG animates through code
An SVG is a text description of shapes, so it can animate through CSS or its own animation elements. An icon that spins or a line that draws itself is usually best as SVG. The file stays tiny and stays sharp at any size. This only works for shapes, not photographic content.
Choosing for moving images
Not every moving image should be an image file. Consider three options:
- Shape-based motion, such as icon animations and loaders: use SVG.
- Short, simple clips with flat color, such as a looping UI demo: use animated WebP, or AVIF if you have tested it.
- Photographic or screen-recorded motion: use a video file in the page instead. Video codecs compress frames against each other, so the result is much smaller than any image format. It also gives the viewer a pause control, which matters for accessibility.
Exporting a transparent image
The following command converts a PNG with alpha into a lossy WebP and keeps the transparency. The -alpha_q option sets the quality of the alpha channel, from 0 to 100. The default is 100, which is lossless alpha.
cwebp -q 80 -alpha_q 90 badge.png -o badge.webpTo check the result, place the file over a dark and a light background and zoom in on the edges. Look for halos, hard stair steps, and banding in shadows.
You can serve the transparent image with a fallback, using the picture element from the responsive images chapter:
<picture>
<source type="image/webp" srcset="badge.webp">
<img src="badge.png" width="200" height="200" alt="Award badge">
</picture>Common errors
- Exporting a transparent image as JPEG. The empty area turns solid. Switch format, or set the matte to the page color.
- Shipping a GIF where a WebP or video would do. The file is often several times larger.
- Cutting out an image on a white background and then placing it on a colored one. Edge pixels still carry white. Re-export the cut-out against the real background color, or use a clean alpha mask.
- Using PNG for a photo with a transparent edge. The file can be several megabytes. Try lossy WebP with alpha.
- Autoplaying a long animation with no way to stop it. Provide a pause control, and respect the user's reduced-motion setting.
- Assuming a format works everywhere. Provide a fallback through the picture element for newer formats.
Exercise
A designer has a product photo cut out from its background, with a soft shadow at the bottom. It must sit on both a white and a dark page section. Which format should they export, and why is JPEG wrong?
Show answer
Export WebP with alpha, with PNG as a fallback. The soft shadow needs full alpha transparency, which also avoids halos on both backgrounds. JPEG has no transparency, so the cut-out area would become a solid matte color that shows as a box on at least one of the two backgrounds.
Exercise
A 6-second looping clip shows a person using an app. The team proposes a GIF. Name two problems and a better choice.
Show answer
A GIF is limited to 256 colors per frame, so the screen recording will look grainy, and the file will be very large. A video file is the better choice because it compresses far more and can include a pause control. Animated WebP is an acceptable second choice if a video is not possible.