Full Screen Video Background in HTML5

Fullscreen HTML5 Page Background Video Example
Code Snippet:Fullscreen HTML5 Page Background Video
Author: Dudley Storey
Published: January 14, 2024
Last Updated: January 22, 2024
Downloads: 950
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a full-screen video background in HTML5 and CSS. It uses the <video> element to display a background video on a webpage. The video is muted and set to autoplay with a loop, ensuring it plays seamlessly without sound. The background is styled with CSS to cover the entire viewport and maintain responsiveness.

The code also includes additional styling for text content and a button. Moreover, it is helpful for adding an engaging video background to your website.

How to Create Full Screen Video Background In HTML5

1. To get started, open your preferred code editor and create an HTML file. Copy and paste the following code as your starting point. Replace "your-poster-image.jpg", "your-video.webm", and "your-video.mp4" with the actual file paths or URLs of your poster image and video files.

<video poster="your-poster-image.jpg" id="bgvid" playsinline autoplay muted loop>
- Video needs to be muted, since Chrome 66+ will not autoplay video with sound.
WCAG general accessibility recommendation is that media such as background video play through only once. Loop turned on for the purposes of illustration; if removed, the end of the video will fade in the same way created by pressing the "Pause" button  -->
<source src="your-video.webm" type="video/webm">
<source src="your-video.mp4" type="video/mp4">

<div id="polina">
<p>filmed by Alexander Wagner 2011
<p><a href="http://thenewcode.com/777/Create-Fullscreen-HTML5-Page-Background-Video">original article</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur porta dictum turpis, eu mollis justo gravida ac. Proin non eros blandit, rutrum est a, cursus quam. Nam ultricies, velit ac suscipit vehicula, turpis eros sollicitudin lacus, at convallis mauris magna non justo. Etiam et suscipit elit. Morbi eu ornare nulla, sit amet ornare est. Sed vehicula ipsum a mattis dapibus. Etiam volutpat vel enim at auctor.</p>
<p>Aenean pharetra convallis pellentesque. Vestibulum et metus lectus. Nunc consectetur, ipsum in viverra eleifend, erat erat ultricies felis, at ultricies mi massa eu ligula. Suspendisse in justo dapibus metus sollicitudin ultrices id sed nisl.</p>

Inside the <div id="polina"> element, you can add your website’s content. This can include headings, paragraphs, buttons, and other elements. Customize this section to suit your website’s needs.

2. Next, create a CSS file (styles.css) in the same directory as your HTML file. Copy and paste the following CSS code into styles.css:

body {
  margin: 0;
  background: #000; 

video { 
    position: fixed;
    top: 50%;
    left: 50%;
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    z-index: -100;
    transform: translateX(-50%) translateY(-50%);
 background: url('//demosthenes.info/assets/images/polina.jpg') no-repeat;
  background-size: cover;
  transition: 1s opacity;
.stopfade { 
   opacity: .5;

#polina { 
  font-family: Agenda-Light, Agenda Light, Agenda, Arial Narrow, sans-serif;
  background: rgba(0,0,0,0.3);
  color: white;
  padding: 2rem;
  width: 33%;
  float: right;
  font-size: 1.2rem;
h1 {
  font-size: 3rem;
  text-transform: uppercase;
  margin-top: 0;
  letter-spacing: .3rem;
#polina button { 
  display: block;
  width: 80%;
  padding: .4rem;
  border: none; 
  margin: 1rem auto; 
  font-size: 1.3rem;
  background: rgba(255,255,255,0.23);
  color: #fff;
  border-radius: 3px; 
  cursor: pointer;
  transition: .3s background;
#polina button:hover { 
   background: rgba(0,0,0,0.5);

a {
  display: inline-block;
  color: #fff;
  text-decoration: none;
  padding: .5rem;
  transition: .6s background; 
@media screen and (max-width: 500px) { 
@media screen and (max-device-width: 800px) {
  html { background: url(https://thenewcode.com/assets/images/polina.jpg) #000 no-repeat center center fixed; }
  #bgvid { display: none; }

3. Finally, add the following JavaScript function between <script> tag just before closing the body element. This will control the video play/pause functionality on click event.

var vid = document.getElementById("bgvid");
var pauseButton = document.querySelector("#polina button");

if (window.matchMedia('(prefers-reduced-motion)').matches) {
    pauseButton.innerHTML = "Paused";

function vidFade() {

vid.addEventListener('ended', function()
// only functional if "loop" is removed 
// to capture IE10

pauseButton.addEventListener("click", function() {
  if (vid.paused) {
    pauseButton.innerHTML = "Pause";
  } else {
    pauseButton.innerHTML = "Paused";

That’s all! hopefully, you have successfully created a Full-Screen video background on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link