How to add a video background to a hero section in Next.js
Updated
In Next.js, render a <video autoPlay muted loop playsInline> absolutely positioned behind your hero content, with a poster and a fixed aspect ratio or height. Set muted on the DOM element as well, because React doesn't guarantee the muted attribute that iOS needs to autoplay.
A seamless 3D hero loop: the kind of video this guide is about
Option 1: a video element in a component
- 1Put your video files and poster in
public/(or a CDN) and create aHeroVideoclient component like the one in Option 2. - 2Position the video absolutely behind the content (
inset: 0,object-fit: cover) inside a section with a set height, so nothing shifts while it loads. - 3Use
preload="metadata"and aposter, and add aprefers-reduced-motionrule that hides the video. - 4Test on a real iPhone: if it doesn't autoplay, the muted attribute is the usual cause.
Limits to know
- iOS autoplays only muted (or silent) videos and needs
playsinlinefor inline playback. - React may not write
mutedas an HTML attribute (React issue #10389 is still open), so also set it on the element. - Give the video a width and height or an aspect ratio to avoid layout shift (CLS).
Option 2: a HeroVideo client component
This component sets muted on the element after it mounts, which keeps iOS autoplay reliable.
- 1Create
components/hero-video.tsxwith the code below. - 2Use it inside your hero section, which needs
position: relative, a height and your content on top. - 3For a third-party player script, load it with
next/script(default strategyafterInteractive). React 19 supports custom elements, so<hero-loop>works in JSX.
"use client";
import { useEffect, useRef } from "react";
export function HeroVideo() {
const ref = useRef<HTMLVideoElement>(null);
useEffect(() => {
const v = ref.current;
if (!v) return;
v.muted = true; // iOS needs the element muted to autoplay
v.play().catch(() => {}); // the poster stays if autoplay is blocked
}, []);
return (
<video
ref={ref}
className="absolute inset-0 -z-10 h-full w-full object-cover motion-reduce:hidden"
autoPlay muted loop playsInline preload="metadata"
poster="/hero-poster.webp" aria-hidden="true"
>
<source src="/hero-loop.av1.webm" type='video/webm; codecs="av01.0.08M.10"' />
<source src="/hero-loop.mp4" type="video/mp4" />
</video>
);
}Things that trip people up
- Don't put the video in a Server Component and expect the muted fix to run; the effect needs a Client Component.
- Large files in
public/are served on every visit. Keep each loop to a few megabytes or move video to a CDN.
The one-line version
Edgeloop loops ship with a small player that does all of the above for you: the right format per browser, a poster while it loads, pausing off-screen, reduced-motion support and a transparent version for your own background.
<script src="https://edgeloop.studio/hero-loop.js" defer></script>
<hero-loop src="/loops/your-loop/" focus="right center"></hero-loop>Collection 01 opens soon. Join the waitlist for 3 free loops.
Questions
Why won't my video autoplay on iPhone in React or Next.js?
iOS needs the video muted and playsinline. React doesn't guarantee the muted attribute, so set video.muted = true in an effect after mount.
Do custom elements like <hero-loop> work in Next.js?
Yes. React 19 supports custom elements fully, so you can use them in JSX and load their script with next/script.