GIFs have been the de facto way to display short-looping images for a long time. But they have been superseded by the video element.
That’s because you can set autoplay, loop, muted, and playsinline on a video to get the same short-looping behaviour. This is better than GIFs because it is smoother and has less weight.
<video src="..." autoplay loop muted playsinline></video>Each property does something specific here:
autoplaystarts the video without user input, like a GIF does.looprestarts it when it ends.mutedis required — browsers block autoplay for videos with sound.playsinlinekeeps the video in the page on iOS Safari instead of opening the native fullscreen player.controlsis left out since GIFs don’t have any.
GIF replacement with Splendid Astro’s Video component
The gifReplacement property sets all the properties you need for this feature.
<Video src="..." gifReplacement />That’s all you need! Here’s a demo:
Background videos
Background videos work the same way as the background images we covered earlier — put the video and the content into a stack, then give the video the object-background class.
<div class="stack"> <video class="object-background" autoplay loop muted playsinline> <source src="..." type="video/mp4" /> </video> <div><!-- content goes here --></div></div>Video also has the same backgroundMode as Image, so you can simply use it instead of writing the code above manually.
---import { Video } from '@splendidlabz/astro'---
<div class="stack"> <Video src="/src/assets/hero.mp4" gifReplacement backgroundMode /> <div><!-- content goes here --></div></div>Background video
The content sets the height, and the video fills the space behind it.
Code for you to copy
GIF replacement
---import { Video } from '@splendidlabz/astro'---
<Video src="/src/assets/my-video.mp4" gifReplacement />Background video
---import { Video } from '@splendidlabz/astro'---
<div class="stack"> <Video src="/src/assets/hero.mp4" gifReplacement backgroundMode /> <div><!-- content goes here --></div></div>Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson