How to Create a Video Background With CSS

Creating a video background with CSS involves using the background-image property and setting it to the URL of the video file. You will also need to use the background-size property to set the size of the video and the background-position property to set the position of the video on the page.

Here is an example of how to create a video background with CSS:


<div class="video-background">
<div class="video-foreground">
<!-- Your content here -->


.video-background {
/* Set the video background */
background-image: url(path/to/video.mp4);
background-size: cover;
background-position: center center;
/* Hide the video */
opacity: 0;
position: absolute;
z-index: –100;

/* Fullscreen */
width: 100%;
height: 100vh;

.video-foreground {
/* Add some content */
position: relative;
z-index: 1;

Make sure the video is in a format that is compatible with most browsers. The most popular and widely supported video format is mp4.

You can also use the video HTML tag to insert the video in your html and use CSS to position it and adjust its size and properties.

Note that, this method might affect the page performance, specially if the video is too big or if the internet connection is slow. An alternative is to use a video background as an animation using CSS or javascript libraries.