Guides

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.

Feed, looping behind a headline.

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>

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

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

Sources

Your work,
made to move.

Try it free

Have an account? Log in