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

To remove the autoplay feature from a video background in Webflow and play the video on button click, you'll need to use some custom code. Here's a step-by-step guide:

​

1. Add a Video element to your Webflow project and upload your video file.

2. Set the "Autoplay" option to off in the Video settings panel.

3. Give your video element a unique class name. You can do this by selecting the element and giving it a class in the settings panel.

4. Add a button element to your design, positioning it wherever you want it.

5. Give your button a unique class name as well.

6. Double-click the video element to open the video settings panel again. Scroll down to the bottom and click on "Copy Video Settings" to get the video element's settings.

​

Now, you'll need to add some custom code to control the video playback:

​

7. Go to the Webflow Designer and open the Custom Code panel (under the Project Settings menu).

8. Click on the </> icon to add some custom code and select "Before </body> tag" from the placement options.

9. Paste the following code into the editor:

​

```javascript

<script>

document.addEventListener('DOMContentLoaded', () => {

  const video = document.querySelector('.your-video-class'); // Replace "your-video-class" with the class name you gave to your video element

  const playButton = document.querySelector('.your-button-class'); // Replace "your-button-class" with the class name you gave to your button element

​

  playButton.addEventListener('click', () => {

    video.play();

  });

});

</script>

```

​

Make sure to replace the ".your-video-class" and ".your-button-class" placeholders with the actual class names you assigned to your video and button elements.

​

10. Save the changes and publish your Webflow project.

​

Once your site is published, the video will no longer autoplay. Instead, it will start playing when the button is clicked.

​

This custom code binds a click event listener to the button element. When the button is clicked, it triggers the `play()` function on the video element, which starts playing the video.

Rate this answer

Other Webflow Questions