Once upon a time, a student decided the best way is to create a video background is to copy snippets on the Internet. While the project worked in the end, Master Coffee cannot help but shake his head at the “Frankenstein code”. It’s actually fairly easy to do video background in modern HMTL CSS, let Master Coffee show you how. Let’s go.
CODE DOWNLOAD
I have released this under the MIT license, feel free to use it in your own project – Personal or commercial. Some form of credits will be nice though. 🙂
VIDEO TUTORIAL
1) THE HTML
<div id="vwrap">
<div id="vfront">Coffee</div>
<video id="vback" src="demo.mp4" poster="demo.webp" autoplay muted loop></video>
</div>
- Start with a
<div id="vwrap">
wrapper. - Put a
<div id="vfront">
container inside for the content. - Lastly, also put the
<video id="vback">
inside. Take note, forautoplay
to work on page load, the video must bemuted
.
2) THE CSS
/* (PART A) WRAPPER */
#vwrap {
width: 100%; height: 300px;
max-width: 600px; margin: 0 auto;
}
/* (PART B) LAYER FRONT & BACK */
#vwrap { position: relative; }
#vfront, #vback {
position: absolute; top: 0; left: 0;
width: 100%; height: 100%;
}
#vfront { z-index: 2; }
#vback { z-index: 1; }
/* (PART C) RESIZE VIDEO */
#vback { object-fit: cover; }
There is no such thing as “CSS background video” at the time of writing, so what we do here is to “stack” the content foreground on top of the video background.
- Start by setting the required dimensions of the wrapper –
#vwrap { width height }
- To layer the foreground and background:
- Set the wrapper to
position: relative
. - Set both foreground and background to
position: absolute
, and cover the wrapper withwidth: 100% height: 100%
. - Place the foreground on top with
z-index: 2
and background withz-index: 1
.
- Set the wrapper to
- Finally, set how the video resizes – with
object-fit: cover
. You can also trycontain
ornone
.
3) VIDEO AUTOPLAY
window.addEventListener("load", () => {
if (document.getElementById("vback").paused) {
let vbgplay = () => {
document.getElementById("vback").play();
document.removeEventListener("click", vbgplay);
};
document.addEventListener("click", vbgplay);
}
});
That’s all. The above HTML and CSS will work well enough, but there is a possible problem – The video autoplay may not work on all devices, depending various factors such as connection speed and security settings. There are 2 possible ways to “fix” this:
- As above, define a
poster
on the video. Show an image instead of the video. - Use a small Javascript snippet to “force start” the video after the first click. Yes, this is a common “problem” on most browsers. Videos cannot start playing until the user clicks on the page at least once.
P.S. This may not be an elegant or foolproof solution, but at least there’s something.
THE END – FULLSCREEN BACKGROUND VIDEO
That’s all for this short tutorial and sharing. Just one small bit before we end, for those who wants a full page background video:
- Just insert
<div id="vfront">
and<video id="vback">
directly into<body>
… The body is now your#vwrap
. - Remove the padding and margin on the page itself –
html, body { padding: 0; margin: 0; }
- Set the page to fill the entire window –
html, body { width: 100vw; min-height: 100vh; }
- That’s all. The rest are of the “layering” are the same.
CHEAT SHEET