How to use a video as a background with CSS

To use a video as a background, put an absolutely positioned video inside a parent with position: relative and fill the area with object-fit: cover. Place the heading and controls above it in real HTML. Keep a readable static background and give visitors control over decorative motion.

Layer the video, overlay, and content

The example layers a video, a translucent overlay, and the main content over a gradient. isolation: isolate creates a stacking context so the video and overlay with negative z-index stay in the component. If the clip contains essential information or dialogue, use a separate accessible player with any captions needed. Only the decorative video is hidden from assistive technology with aria-hidden.

Prepare an MP4 and run the example

Save a short MP4 you recorded or have permission to use as background.mp4. Put the example in the same folder as video-background.html and open it. When uploading, set src to the actual media URL. MP4 is a container format, so the file extension alone tells you little about whether the browser supports the codec.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>A background video you can pause</title>
  <style>
    body { margin: 0; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    .hero { position: relative; isolation: isolate; overflow: hidden;
      min-height: 340px; display: grid; place-items: center;
      background: linear-gradient(135deg, #173f55, #102138); color: white; }
    .hero video, .shade { position: absolute; inset: 0; width: 100%; height: 100%; }
    .hero video { object-fit: cover; object-position: center; z-index: -2; opacity: 0; }
    .hero video[data-visible] { opacity: 1; }
    .shade { background: rgb(0 0 0 / 0.55); z-index: -1; pointer-events: none; }
    .hero-content { width: min(100%, 680px); padding: 32px 24px; text-align: center; }
    h1 { margin: 0 0 16px; font-size: clamp(28px, 5vw, 44px); line-height: 1.3; }
    button { min-height: 44px; padding: 8px 16px; font: inherit;
      color: #102138; background: white; border: 2px solid white; cursor: pointer; }
    button:focus-visible { outline: 3px solid #ffce68; outline-offset: 4px; }
    .status { min-height: 1.5em; margin: 8px 0 0; font-size: 14px; }
    main { max-width: 900px; margin: auto; padding: 24px; }
    h2 { font-size: 22px; }
  </style>
</head>
<body>
<section class="hero">
  <video id="background-video" src="background.mp4" muted loop playsinline preload="none" aria-hidden="true"></video>
  <div class="shade" aria-hidden="true"></div>
  <div class="hero-content">
    <h1>The content comes first</h1>
    <p>You can read the heading and description while the background video is paused.</p>
    <button id="toggle" type="button" hidden>Play background video</button>
    <p id="status" class="status" role="status"></p>
  </div>
</section>
<main>
  <h2>Main content</h2>
  <p>The video is decorative. Important information is in the HTML.</p>
</main>
<script>
  const video = document.querySelector('#background-video');
  const toggle = document.querySelector('#toggle');
  const status = document.querySelector('#status');
  const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');
  function updateButton() {
    toggle.textContent = video.paused ? "Play background video" : "Pause background video";
  }
  function showFallback() {
    video.pause();
    video.removeAttribute("data-visible");
    status.textContent = "The video could not play. The static background is available.";
    updateButton();
  }
  video.addEventListener('error', showFallback);
  video.addEventListener('playing', () => {
    video.setAttribute('data-visible', '');
  });
  async function startVideo() {
    try {
      await video.play();
      status.textContent = '';
    } catch (error) {
      if (error.name !== 'AbortError') showFallback();
    }
  }
  toggle.addEventListener('click', () => {
    if (video.paused) startVideo();
    else video.pause();
  });
  video.addEventListener('play', updateButton);
  video.addEventListener('pause', updateButton);
  reducedMotion.addEventListener('change', (event) => {
    if (event.matches) video.pause();
  });
  toggle.hidden = false;
  if (!reducedMotion.matches) startVideo();
</script>
</body>
</html>

The gradient is visible first. The film becomes visible on playing, and pausing stops it on the current frame. A file error or refused playback makes the video transparent again and shows a status message. The text remains readable without JavaScript and the inactive playback button stays hidden.

Adjust cropping and text contrast

cover preserves the original aspect ratio and fills the box, so it may crop the video along one axis. Try object-position: 70% center to shift the focus right. Switching to contain shows the entire frame and can leave space when the proportions differ. Compare the results, then restore cover and the centered position. See CSS Images · object-fit.

The section starts at min-height: 340px and can grow for longer text. Increasing the opacity of the black overlay darkens the footage without dimming the heading or button. Check that the heading and focus indicator stand out in bright scenes as well as dark ones.

Handle autoplay, pause, and reduced motion

The example uses muted for silent playback and playsinline to request inline playback, but the browser can still refuse autoplay. The play() promise handles that refusal. If a pause interrupts a pending play request, its AbortError does not show a failure message. See HTML Standard · play().

When reduced motion is already on, the example skips autoplay. Turning it on during playback pauses the film. Turning it off leaves the video paused until the visitor chooses to play it. See Media Queries · prefers-reduced-motion for the media query and WCAG 2.2 · Pause, Stop, Hide for when users need a way to pause automatic motion.

Check file size and diagnose playback failures

Encode a short clip at a resolution suitable for a decorative background. preload="none" is a hint to avoid loading media ahead of time. Playback still needs data. To delay loading until the visitor clicks, remove the last automatic start statement and leave the button callback in place.

Temporarily change the source to a nonexistent file, check that the message and fallback show, then restore the path. If it fails only after deployment, check the request URL and letter case, HTTP status, and Content-Type in developer tools. Then check codec support and autoplay rules in the target browser. If the clip plays after clicking the button, that helps distinguish an autoplay restriction from a media file problem.