On this page
An old, open briefcase with a mini beach and sun in it

Optimizing background videos for fast loading. Tips and strategies for improved user experience

Enhance lazy loading of media by skipping fetch if not in viewport for at least 1 sec. Pause media when not visible in viewport.

Adding videos to your website can be a double-edged sword. Web users love high-resolution videos, but if your website isn’t optimized to handle them, such content can cause significant performance problems, leading to higher bounce rates and a poor user experience.

When it comes to optimizing background videos for fast loading, you must consider the entire pipeline, from choosing the right codec and reducing file size to implementing modern loading strategies like lazy loading and adaptive bitrate streaming.

1. Optimize the video file

The most significant factor affecting the loading speed of background videos is the file size. A large video file will always load slower, especially on mobile networks. To reduce file size without sacrificing perceived quality, try these techniques:

  • Compress with modern codecs: use video compression tools like HandBrake or FFmpeg to reduce the file size. The latest codecs (like AV1) offer significantly better compression than older ones (like H.264). Adjust the bitrate and other settings to find a balance between quality and file size. Remember, a background video is often an aesthetic addition, not the main content.
  • Choose the right video format: for maximum compatibility and efficiency, use the following formats:
    • MP4 (H.264): the baseline for universal support across all browsers.
    • WebM (VP9): better compression than H.264, widely supported in modern browsers.
    • WebM (AV1): the best compression available today, though it requires more computational power to encode and decode. Use it as a progressive enhancement.
    Provide multiple sources with the <source> element to let the browser choose the best one.
  • Trim and loop efficiently: ensure your video is only as long as it needs to be for a seamless loop. Removing even a single second of footage from a 5-second loop can save a substantial amount of data.

2. Remove the audio track

Background videos are almost always muted. If your video has an audio track, you can remove it to save a significant amount of file size.

Use FFmpeg to strip the audio from your video file. The command is:

ffmpeg -i input.mp4 -an -c:v copy output.mp4

Alternatively, if you are creating the video yourself, export it without an audio track in your video editing software.

3. Use HTML5 supported formats

HTML5 allows developers to deliver video content without relying on third-party plugins. Convert your video files to modern, efficient HTML5 formats using an online converter or FFmpeg. Providing both MP4 (H.264) and WebM (VP9 or AV1) ensures broad browser compatibility.

The following table lists the most common video codecs used on the web:

Common video codecs for the web. Source: MDN Web Docs
Codec name (short)Full codec nameContainer support
AV1AOMedia Video 1MP4, WebM
AVC (H.264)Advanced Video Coding3GP, MP4
H.263H.263 Video3GP
HEVC (H.265)High Efficiency Video CodingMP4
TheoraTheoraOgg
VP8Video Processor 83GP, Ogg, WebM
VP9Video Processor 9MP4, Ogg, WebM

4. Optimize dimensions and aspect ratio

The video’s dimensions should always match its display area. Serving an oversized video wastes bandwidth and processing power.

  • Resize to the maximum display size: a 4K video is almost never necessary for a background. A 1920×1080 resolution is generally sufficient for desktop, while 720p is often more than adequate for mobile. Use CSS to upscale if needed.
  • Implement responsive design: use CSS like object-fit: cover; on the <video> element to ensure it covers its container without distortion, regardless of screen size.
  • Serve different resolutions for different devices: use JavaScript or the <source> element with media queries to serve smaller video files to mobile devices.
<video autoplay muted loop playsinline>
  <source src="background-1080p.webm" type="video/webm" media="(min-width: 1024px)">
  <source src="background-720p.webm" type="video/webm" media="(max-width: 1023px)">
  <source src="background-1080p.mp4" type="video/mp4" media="(min-width: 1024px)">
  <source src="background-720p.mp4" type="video/mp4" media="(max-width: 1023px)">
</video>

5. Lazy loading and deferred loading

Lazy loading is crucial for performance, especially if the video is not in the initial viewport.

Native Lazy Loading: the loading="lazy" attribute is now supported for <video> elements in modern browsers:

<video autoplay muted loop playsinline loading="lazy">...</video>

Intelligent lazy loading: for an even better user experience, implement a solution that only starts loading the video when it has been in the viewport for a sustained period (e.g., 2 seconds). This prevents the browser from downloading large files when a user is quickly scrolling past. You can read more about this in the article: Improved lazy loading for image, video, and audio.

For CSS Backgrounds: you cannot natively lazy load a video used via the CSS background-image property. The recommended approach is to use a container element with an absolutely positioned <video> element inside it. This allows you to use the native loading="lazy" attribute. See a working example here: Lazy loading for images, audio, and video (including CSS background simulation).

6. Preload the video strategically

The preload attribute provides a hint to the browser about how much of the video to fetch upfront.

  • preload="auto": the browser should download the entire video. This is ideal for a critical, above-the-fold background video but can hurt initial page load time.
  • preload="metadata": the browser fetches only video metadata (like dimensions, duration, first frame). This is a good default balance, but be aware that the metadata for some formats can be large.
  • preload="none": the browser should not load any video data until playback is requested. This is the best option for videos that are lazy-loaded or below the fold.
Use preload="metadata" with caution: it can sometimes download a significant amount of data (as seen in the screenshot below). For above-the-fold background videos, preload="auto" is often best. For below-the-fold or lazy-loaded videos, pair preload="none" with loading="lazy".
Browser developer console showing 66 video HTTP requests with preload='metadata' attribute, resulting in 35.8 MB of data transferred.
Even with preload="metadata", a significant amount of data can be downloaded.

7. Use a poster image

Always set a poster attribute on your video element. This image is displayed while the video is downloading or before it starts. It provides a visual placeholder that improves the user experience. Ensure the poster image itself is optimized (e.g., a compressed JPEG or WebP).

<video autoplay muted loop playsinline poster="optimized-poster.webp">...</video>

8. Consider a video hosting platform

Hosting videos on your own server can be resource-intensive. Platforms like Vimeo, YouTube, or Mux are optimized for global video delivery. They offer a key feature: Adaptive Bitrate Streaming (ABR). This automatically delivers the best possible quality based on the user’s connection speed, preventing buffering and ensuring a smooth experience.

9. Add CSS performance hints

For background videos that are positioned behind content, promote them to their own compositor layer to prevent repaints and layout shifts.

.background-video {
  will-change: transform; /* Hint to the browser to optimize for transforms */
  transform: translateZ(0); /* Creates a new compositor layer */
}

10. Provide a static image fallback

On very slow connections (e.g., 2G) or when the user has Save-Data enabled, you should replace the video with a high-quality static image. You can detect these conditions using the Network Information API or by using a <picture> element with media queries.

<video autoplay muted loop playsinline poster="fallback.jpg">
  <source src="video.webm" type="video/webm">
  <source src="video.mp4" type="video/mp4">
  <!-- Fallback content if video cannot be loaded -->
  <img src="fallback.jpg" alt="A descriptive background image">
</video>

11. Use the playsinline attribute

This is a critical attribute for mobile browsers. Without it, many mobile browsers (especially iOS Safari) will open the video in full-screen mode when it starts playing, which is disruptive for a background video. Always include playsinline.

<video autoplay muted loop playsinline>
  <source src="background.webm" type="video/webm">
</video>

Related posts

Comments

Leave a Reply

Search in sitelint.com

Is your site slow?

Discover performance bottlenecks to provide a better customer experience and meet your company’s revenue targets.