Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

GIF Replacements and background video

1m:44s

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:

  • autoplay starts the video without user input, like a GIF does.
  • loop restarts it when it ends.
  • muted is required — browsers block autoplay for videos with sound.
  • playsinline keeps the video in the page on iOS Safari instead of opening the native fullscreen player.
  • controls is 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>