How to add a looping video to a website hero
Updated
A video behind the headline makes a page feel alive in the first second. It also fails in quiet ways: it will not start on an iPhone, it shows a black box while it loads, it jumps every time it loops, or it doubles the page's weight. Each has a small fix.
Objects for slow mornings.
The code
<video class="hero-video" autoplay muted loop playsinline
preload="auto" poster="/hero.jpg" aria-hidden="true">
<source src="/hero.webm" type="video/webm">
<source src="/hero.mp4" type="video/mp4">
</video>- autoplay and muted: browsers only start a video on their own if it is silent. Chrome and Safari both allow muted autoplay; drop muted and it will not start.
- playsinline: without it, an iPhone plays the video full screen, or not at all, instead of inside the page.
- loop: starts again from the first frame at the end.
- poster: a still shown until the video plays, and in its place when a phone refuses to play it, as some do in Low Power Mode.
- Two sources: the browser plays the first it can. WebM is often smaller; MP4 plays everywhere.
- aria-hidden: a background video is decoration, so screen readers can skip it. Put anything that matters in the page's text.
Filling the space
.hero { position: relative; overflow: hidden; }
.hero-video {
position: absolute; inset: 0;
width: 100%; height: 100%;
object-fit: cover;
}object-fit: cover fills the hero at any width and crops the overflow, so keep the important part of the motion near the middle. For text over a busy video, lay a dark gradient between the two.
Respect reduced motion
Some people set their device to reduce motion, because movement makes them unwell. Stop the video for them, and the poster stays in its place:
const video = document.querySelector(".hero-video");
if (matchMedia("(prefers-reduced-motion: reduce)").matches) {
video.removeAttribute("autoplay");
video.pause();
}A loop that does not jump
The loop attribute cuts straight from the last frame back to the first. If they differ, the jump shows every few seconds, and on a page people stay on, that is many times. Footage that repeats (a turn, a stride) loops well; a one-way event (a pan, a pour) never will. Templates in SOH Motion are made as loops, so the last frame runs into the first.
Keep it light
- Keep it short. Six to ten seconds is plenty for something that repeats.
- 1080p wide is enough for most heroes. 4K is rarely worth its weight here.
- 30 fps is half the frames of 60 and looks the same for slow motion.
- Make the poster the video's first frame, so nothing changes when it starts.
A hero that sits on the page itself
A video with a transparent background lets the page itself show through. Export a WebM with Background set to None in the editor; Chrome, Edge and Firefox play its transparency. Test Safari before you rely on it, and keep the poster as a fallback. More in how to export video with a transparent background.
Try it with your own pictures.
Open Feed