Overlaying a transparent background on an embedded video

。_饼干妹妹 提交于 2019-12-08 18:52:15

问题


I've a video which I've embedded in a fullwidth div panel. I'm trying to overlay a gradient on top of it, but I can't make it work. I've tried adjusting z-index, wrapping the video in another div, and - as below - adding an overlay class, but I must be missing something obvious.

Whatever I try, the video jumps back on top of the other panels (which end up falling behind it).

Would be so grateful for your help!

                <div class="videoContainer hide-for-small-only">
                    <div class="overlay"></div>
                  <video autoplay loop muted>
                        <source src="<?=URL?>public/videos/vid.mp4" type="video/mp4">
                        <source src="<?=URL?>public/videos/vid.webm" type="video/webm">
                  </video>
                </div>

My css is:

.videoContainer 
{

  position: absolute;
  width: 100%;
  height: 100%;
  //padding: 20px;
  border-radius: 5px;

  background-attachment: scroll;
  overflow: hidden;
}

.videoContainer video 
{
    min-width: 100%;
    min-height: 100%;
}
.videoContainer overlay {
    background: black;
    opacity: 0.5;
    position: absolute;
    z-index: 1;
    text-align: center;
    margin: 0%;
  }

回答1:


Here is a fiddle

I used green overlay for the demo.

CSS

.videoContainer {
    position: relative;
    width: 100%;
    height: 100%;
    //padding: 20px;
    border-radius: 5px;
    background-attachment: scroll;
    overflow: hidden;
}
.videoContainer video {
    min-width: 100%;
    min-height: 100%;
    position: relative;
    z-index: 1;
}
.videoContainer .overlay {
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 2;
    background: green;
    opacity: 0.5;
}



回答2:


I came across this very same issue and eventually managed to get an aesthetically similar effect simply setting the video with opacity:0.5 over a colored background.

.vid-bg {
  opacity: 0.5; /* ! */
  position: fixed;
  right: 0;
  bottom: 0;
  min-width: 100%; 
  min-height: 100%
}
<body style="background-color: green">
    <video playsinline="playsinline" autoplay="autoplay" muted="muted" loop="loop" class="vid-bg">
    <source src="http://inserthtml.com/demos/javascript/background-videos/flowers.mp4" type="video/mp4">
  </video>
</body>


来源:https://stackoverflow.com/questions/31428660/overlaying-a-transparent-background-on-an-embedded-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!