
GIFs Without the .gif: The Most Performant Image and Video Options Right Now
So you want an auto-playing looping video without sound? In popular vernacular this is the very meaning of the word GIF. The word has stuck around but the image format itself is ancient and obsolete. Twitter, for example, has a “GIF” button that actually inserts a <video>
element with an MP4 file into your tweet — no .gif
in sight. There are a beguiling amount of ways to achieve the same outcome but one thing is clear: there’s really no good reason to use the bulky .gif
file format anymore.
It’s easy to recreate the behavior of a GIF using the HTML video element.
While the HTML video element itself has been supported for many years, the same can’t be said for the wide variety of video formats.
Videos are made up of two parts: the container and the video codec. (If your video contains audio then it is made up of three parts, the third being the audio codec.) Containers can store video, audio, subtitles and meta information. The two most common containers for video on the web are MP4 and WebM. The container is the same as the file type — if a file ends with a .mp4
extension, that means it’s using an MP4 container. The file extension doesn’t tell you the codec though. Examples of video codecs commonly used on the web include VP8, VP9, H.264 and HEVC (H.265). For your video to play online, the browser needs to support both the video container and the codec.
Browser support for video is a labyrinthine mess, which is part of the reason YouTube embeds are ubiquitous, but that doesn’t work for our use case. Let’s look at the video formats that are worth considering.
Containers
Codecs
An MP4 file using the H.264 codec will work everywhere, but it doesn’t deliver the best quality or the smallest file size.
The recently redesigned website from development consultancy Evil Martians is a testament to the file-size reduction that AV1 is capable of.
If you want to use newer video formats with fallbacks for older browsers, you can use multiple <source>
elements. The order of the source elements matter. Specify the ideal source at the top, and the fallback after.
Given the above code, cats.webm
will be used unless the browser does not support that format, in which case the MP4 will be displayed instead.
Using the above code the browser will select AV1 if it can play that format and fallback to the universally-supported H.264 if not. For AV1, the codecs
parameter always starts with av01
. The next number is either 0
(for main profile), 1
(for high profile) or 2
(for professional profile). Next comes a two-digit level number. This is followed either by the letter M
(for main tier) or H
(for high tier). It’s difficult to understand what any those things mean, so you could provide your AV1 video in a WebM container and avoid specifying the codec entirely.
You should use the most high-resolution source file you can. Obviously, once image quality is lost you can’t improve it through conversion to a superior format. Using a .gif
as a source file isn’t ideal because the visual quality of .gif
isn’t great, but you’ll still get the benefit of a large reduction in file size:
On Mac, you can download FFmpeg using Homebrew:
If you want to use the video as a background and place other elements on top of it, working with <video>
is slightly more challenging than a CSS background-image
, and requires code that goes something like this:
Whether it’s an animated WebP or animated AVIF file, using images rather than video comes with some benefits.
I’m not sure how many people actually want to art-direct their GIFs, but using the <picture>
element does open up some possibilities that couldn’t easily be achieved with <video>
. You could specify different animations for light and dark mode, for example:
We might want a video on mobile to be a different aspect ratio than on desktop. We could just crop parts of the image with CSS, but that seems like a waste of bytes and somewhat haphazard. Using a media query we can display a different animated image file based on the screen size or orientation:
All of this is possible with video — you can use matchMedia
to do any media queries in JavaScript and programmatically change the src
of a <video>
element:
I believe that whenever there’s a way to do something with markup it should be preferred over doing it JavaScript.
If you want a background video that takes up the entire screen, it’s slightly easier to position a background-image
than a HTML <video>
element:
As its name suggests, AVIF is based on the the AV1 video codec. Like WebP, AVIF can be used for both still images and animation. There’s not much difference between an animated AVIF file and an AV1 video in an MP4 container.
You can put a shadow on AVIF animation, e.g.:
Adoption remains lacking in design software. When viewing a prototype, Figma will play any animated GIFs included in the design. For AVIF, by contrast, you can’t even import or export a still image.
All the same codecs that Safari supports for <video>
are supported by <img>.
This means you can use MP4, H.264, and HEVC.
In Safari, video files will also work anyplace in CSS where you could use an image, like background-image
or border-image
:
By placing your videos in <img>
elements, the content loads faster, uses less battery power, and gets better performance.
[…]
The user can still decide to watch a certain video by right-clicking and pressing play in the menu, or enable autoplay for all videos on a specific website.
For any moving, blinking or scrolling information that (1) starts automatically, (2) lasts more than five seconds, and (3) is presented in parallel with other content, there is a mechanism for the user to pause, stop, or hide it unless the movement, blinking, or scrolling is part of an activity where it is essential.
With the <video>
element, you’ll achieve that criterion without any additional development.
There’s also a “reduce motion” user setting that developers can respect by reducing or removing CSS and JavaScript web animations.
There’s another downside. When using the <picture>
element in this way if the user has checked “reduce motion”there’s no way for them to see the animation. Just because a user prefers less animation, doesn’t mean they never want any — they might still want to be able to opt-in and watch one every now and then. Unlike the <video>
element, displaying a still image takes away that choice.
If you want to check that your <picture>
code is properly working and fallback images are being displayed, you can use the Rendering tab in Chrome DevTools to turn off support for AVIF and WebP image formats. Seeing as all browsers now support WebP, this is a pretty handy feature.
Once you’ve included the dependency you need to write a small amount of JavaScript code to get the animation to run:
You can optionally change those settings to only play after an event:
You can also programmatically play an animation backwards and change the playback rate.
I wish there was a TL;DR for this article. For now, at least, there’s no clear winner…
If you need help creating a digital marketing strategy for your business, don’t hesitate to contact one of Digidude’s consultants.
Post a Comment
You must be logged in to post a comment.