Using a video as a background

柔情痞子 提交于 2020-07-21 03:03:15

问题


I want to know which is a good way to use a video as a background, like is used for example in Square Cash: https://square.com/cash


回答1:


<div id="video-container">
<video autoplay loop poster="http://media.w3.org/2010/05/sintel/poster.png" muted="muted">
                    <source id="mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4">
                    <source id="webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" type="video/webm">
                    <source id="ogv" src="http://media.w3.org/2010/05/sintel/trailer.ogv" type="video/ogg">
                    Your browser does not support the video tag. I suggest you upgrade your browser.
                </video>
</div><!-- end video-container -->




#video-container {
    top:0%;
    left:0%;
    height:100%;
    width:100%;
    overflow: hidden;
        position: absolute;
}
video {
    position:absolute;
    z-index:0;
        width: 100%;
}



回答2:


Added object-fit: cover; for video to act like background-size: cover;

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.videobg {
  height: 100vh;
  overflow: hidden;
  padding: 5vh;
  position: relative; /* requires for to position video properly */
}

video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  object-fit: cover; /* combined with 'absolute', works like background-size, but for DOM elements */
}
<div class="videobg">
  <video playsinline webkit-playsinline autoplay loop muted>
  <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
  <img src="https://dummyimage.com/200x200/cc0000/ccc.jpg">
</div>


来源:https://stackoverflow.com/questions/19477890/using-a-video-as-a-background

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