Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Adding a video as a background in html/css

Edrol97

Silver Coder
I want to add a video as a specific page background in html/css. How would I do it so the video loops ad infintum?

Thanks,

Eliot
 
Hey Eliot,

Yeah, you can do that easy enough — just use the &lt;video&gt; tag in HTML, chuck in your autoplay, muted, and loop attributes, and you’re good to go. That’ll make it play automatically and loop forever without sound.

Just make sure the video’s either covering the whole page via CSS or inside a div set to position: absolute or fixed, depending on how you want it layered. Also, don’t forget playsinline for mobile if you want it to behave.

If you need it to sit behind content, you’ll just set the z-index stuff right and maybe throw object-fit: cover on it so it doesn’t go all weird with scaling.


Let me know if you want a snippet
 
Hey Eliot,

Yeah, you can do that easy enough — just use the &lt;video&gt; tag in HTML, chuck in your autoplay, muted, and loop attributes, and you’re good to go. That’ll make it play automatically and loop forever without sound.

Just make sure the video’s either covering the whole page via CSS or inside a div set to position: absolute or fixed, depending on how you want it layered. Also, don’t forget playsinline for mobile if you want it to behave.

If you need it to sit behind content, you’ll just set the z-index stuff right and maybe throw object-fit: cover on it so it doesn’t go all weird with scaling.


Let me know if you want a snippet
A snippet would be helpful. is &lt;video&gt; the same as <video> as a tag?

Thanks,

Eliot
 
A snippet would be helpful. is &lt;video&gt; the same as <video> as a tag?

Thanks,

Eliot

If you use &lt;video&gt; it would display it as text whereas <video> would actually load the video. You would only use the first one during like a tutorial or something.

Something like this should work for example:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Example Code</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="video-background">
    <video autoplay muted loop playsinline>
      <source src="background.mp4" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>

  <div class="content">
    <h1>Welcome to My Website</h1>
    <p>This content appears over the video background.</p>
  </div>
</body>
</html>
CSS:
body, html {
 code here
}

.video-background {
  position: fixed;
  top: 0;
  left: 0;
  min-width: 100%;
  min-height: 100%;
  z-index: -1;
  overflow: hidden;
}

.video-background video {
  min-width: 100%;
  min-height: 100%;
  object-fit: cover;
}

.content {
  code here
}
 
Back
Top Bottom