Search code examples
htmlcsshtml5-videogradient

How do I put text on a gradient overtop a background video in css?


I have a video that plays as the user opens the page so the video is on the background and text can be displayed on top.

This all works right now but I would like to add a gradient over the video to make the text at the bottom visible at all times, just like you do with images.

However, I cannot seem to find a solution that worked for me. I'm sorry if the code is a bit unprofessional, I have just started my first year as a student.

.container-video {
  position: relative;
  /* background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.01),
    rgba(0, 0, 0, 0.7)
  );
  z-index: 99; */
}

#background-video {
  /* video itself */
  width: 100vw;
  /* viewport width */
  height: 50vh;
  /* viewport height */
  object-fit: cover;
  z-index: 0;
}

.overlay {
  /* text on top of the video */
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  margin-top: 280px;
}
<header>
  <div class="container-video">
    <video id="background-video" autoplay loop muted poster="images/Placeholder_videoPic.png">
      <source src="videos/Placeholder_Video_trim.mp4" type="video/mp4" />
    </video>
  </div>
  <div class="overlay">
    <h1>Meteor dodge</h1>
    <p>Available on PS5, PS5, Xbox & PC</p>
    <p>$14.99</p>
    <a href="inlogForm.html">Kopen</a>
  </div>
</header>


Solution

  • This is the CSS that ended up working for me:

    .container-video {
      position: relative;
      width: 100%;
    }
    
    #background-video {
      width: 100vw;
      height: 50vh;
      object-fit: cover;
      z-index: 0;
      position: relative;
    }
    
    .overlay {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1;
      margin-top: 280px;
      background-image: linear-gradient(
        to bottom,
        rgba(0, 0, 0, 0.1),
        rgba(0, 0, 0, 1)
      );
      width: 100%;
      padding-bottom: 30px;
    }