Here is how to create Auto-Playing and Muted Background Video in HTML
Step 1: HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Auto-Playing and Muted Background Video in HTML</title>
<meta name="description" content="Learn how to add an auto-playing and muted background video in HTML. Step-by-step guide with code example and explanation.">
<meta name="keywords" content="HTML Background Video, Autoplay Video, Muted Video, Fullscreen Video, HTML5 Video Tag">
</head>
<body>
<div class="video-container">
<video autoplay muted loop playsinline>
<source src="background-video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<div class="content">
<h1>Welcome to My Website</h1>
</div>
</body>
</html>
Step 2: CSS Code
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.video-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
overflow: hidden;
}
video {
width: 100%;
height: 100%;
object-fit: cover;
}
.content {
position: relative;
z-index: 1;
color: white;
text-align: center;
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
}
h1 {
font-size: 3em;
}
Explanation
-
HTML Video Element (
<video>)autoplay: Automatically starts playing the video.muted: Required for autoplay to work in most browsers.loop: Repeats the video continuously.playsinline: Ensures the video plays inline on mobile devices.<source>: Provides the video file in MP4 format (you can add WebM for better browser compatibility).
-
CSS for Fullscreen Background Video
.video-containeris positionedfixedto cover the entire screen.object-fit: cover;makes sure the video scales properly without distortion..contentis a centered text overlay with a semi-transparent background for better readability.