How can I remove autoplay from a video background in Webflow and play the video with a button click? The code I have tried doesn't seem to work.

TL;DR
  • Disable autoplay in Webflow Designer for the video background.
  • Add and style a button, then use custom JavaScript to trigger video playback on button click by replacing CSS selectors with actual element identifiers.
  • Publish the changes to implement functionality.

To remove autoplay from a video background in Webflow and set it to play with a button click, you need to make specific adjustments to your Webflow project settings. Follow these steps to achieve the desired effect:

1. Remove Autoplay from Video Background

  • Go to Webflow Designer and click on the element containing the video background.
  • Select the video element in the Navigator panel.
  • In the Settings panel, make sure the autoplay option is not selected for your video element.

2. Add a Button for Video Playback

  • Navigate to the Add Elements panel in Webflow Designer.
  • Drag a Button element onto your canvas and place it in a suitable location, preferably close to or on top of the video.
  • Style the button as desired using the Style panel.

3. Implement Custom Code for Button Click

  • Go to your Webflow project’s dashboard and open the Custom Code section.
  • In the Before </body> tag area, paste the following JavaScript to enable video playback on button click:

  ```

  <script>

  document.addEventListener('DOMContentLoaded', function() {

    const videoElement = document.querySelector('YOURVIDEOELEMENT_SELECTOR');

    const playButton = document.querySelector('YOURBUTTONSELECTOR');

    if (videoElement && playButton) {

      playButton.addEventListener('click', function() {

        videoElement.play();

      });

    }

  });

  </script>

  ```

  • Replace YOURVIDEOELEMENT_SELECTOR with the CSS selector for your video element and YOURBUTTONSELECTOR with the selector for your button.

4. Publish Your Webflow Site

  • Click on the Publish button in the upper-right corner of Webflow Designer.
  • Select the domain to which you want to publish.
  • Check that the button now starts the video playback when clicked.

Summary

To remove autoplay and enable video play with a button, disable autoplay in Webflow Designer, add a button element, and use custom JavaScript to trigger video playback on button click. Ensure that you replace the placeholders with your actual element selectors for a smooth operation.

Rate this answer

Other Webflow Questions