How to add a background video without YouTube
A YouTube iframe is not always the right choice. For example, you want a video on the home page of the website, and YouTube ads or its player are not what you want there. The way is the <video> element.
Today it can be done with HTML and CSS only, without any library or JavaScript (as of September 2026). It takes three steps:
1. Get the video
Keep the file as small as possible. On a large monitor a 20-second video can easily have 20 MB. On a computer with fast internet 20 MB is not a problem, but on slower Wi-Fi or mobile data it is, so it is worth preparing a smaller version and considering whether the video makes sense on a phone at all.
2. The HTML part
<div class="home_video_block"> <video autoplay muted loop playsinline poster="img/img-bg.jpg" id="bgvid"> <source src="video/home-video.mp4" type="video/mp4"> </video> </div>
The poster attribute holds an image that is shown while the video is not loaded. The muted and playsinline attributes are required – browsers start automatically only a video without sound, and iPhone plays a video without playsinline only in full screen.
3. The CSS part
The object-fit: cover property makes the video fill the whole block (and cuts off the rest) and the svh unit sets the height of the block to the height of the browser window:
.home_video_block {
position: relative;
height: 100svh; /* height of the browser window */
overflow: hidden;
}
video#bgvid {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
}