Webflow sync, pageviews & more.
NEW

How can I add a background video to my Webflow site using custom code?

TL;DR
  • Host an optimized MP4 video externally or via Webflow, then embed it using an HTML <video> tag inside Webflow's Embed component.
  • Apply appropriate CSS (absolute positioning, object-fit: cover, z-index: -1) and set the parent container to relative with defined height and overflow hidden for layout control.
  • Ensure the video is muted, autoplays, and includes fallback image support for unsupported browsers.

To add a background video in Webflow using custom code, you’ll need to use an HTML5 <video> element embedded via the Embed component or Page Settings.

1. Prepare Your Video Files

  • Convert your video to formats best supported across browsers: MP4 (mandatory) and optionally WebM and OGV for older browsers.
  • Optimize file size under 5MB if possible for performance.
  • Host your video externally, such as on Dropbox, Amazon S3, Google Cloud, or use Webflow’s file upload if the size is under 10MB.

2. Add an Embed Element in Webflow

  • In the Designer, drag an Embed component (from Add panel > Components > Embed) into your layout where the background should appear.

  • Paste the following HTML code into the Embed panel:

    <video autoplay muted loop playsinline preload="auto" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: -1;">
    <source src="https://YOUR_VIDEO_LINK.mp4" type="video/mp4">
    </video>

  • Replace https://YOUR_VIDEO_LINK.mp4 with the direct URL to your hosted video file.

  • Make sure styles like object-fit: cover and z-index: -1 are set so the video stays behind other content.

3. Adjust the Parent Container

  • Select the HTML Embed's parent section or div and set its position to relative to allow absolute positioning of the video inside.
  • Style the section with a fixed height, such as 100vh, to display the full video properly.
  • Ensure the section has no overflow issues—set overflow: hidden to crop excess.

4. Test on Mobile and Browsers

  • Most mobile browsers will only autoplay videos that are muted and do not use user data.
  • If autoplay fails, check if the video has muted, playsinline, and loop attributes.

5. Optional: Add Fallback Background

  • Add a background image to the same section so that users without video support see a still image.

Summary

To add a background video with custom code in Webflow, use the Embed component with an HTML <video> tag, ensure proper CSS positioning, and host your video file externally. Make sure it is muted, set to autoplay, and uses compatible formats like MP4.

Rate this answer

Other Webflow Questions