简体   繁体   English

如何使用 HTML/CSS 和 Bootstrap 4 使视频适合 web 页面的背景?

[英]How do I fit a video to the background of a web page using HTML/CSS and Bootstrap 4?

I'm trying to get the video to fit the screen.我正在尝试让视频适合屏幕。 Currently it is massive and I can't figure out how to make the scroll bars disappear.目前它很大,我不知道如何使滚动条消失。 Object-fit doesn't look like it's working as it is supposed to. Object-fit 看起来不像它应该的那样工作。 Also, when I make the screen smaller obviously I want the video to scale with it.此外,当我明显缩小屏幕时,我希望视频随之缩放。

HTML HTML

<div class="video-background">
    <video autoplay muted loop>
        <source src="img/cocktail_pour.mp4" type="video/mp4">
    </video>
</div>

CSS CSS

body {
    margin:0;
    padding:0;
}
.video-background {
    width:100%;
    height:100vh;
    overflow:hidden;
    display:flex;
    justify-content:center;
    align-items:center;
}

.video-background video{
    position:absolute;
    top:0;
    left:0;
    object-fit:cover;
}

This works across most browsers:这适用于大多数浏览器:

HTML: HTML:

<div class="video-background">
  <video autoplay muted loop class="video">
    <source src="https://www.w3schools.com/howto/rain.mp4" type="video/mp4">
    Your browser does not support HTML5 video.
  </video>
</div>

<div class="content">
  <!-- CONTENT GOES HERE -->
</div>

CSS: CSS:

.video { /* This is to make the video appear behind everything else */
  position: fixed;
  z-index: -1000; /* Z-Index is -1000 just to be safe */
  right: 0;
  bottom: 0;
  min-width: 100%; 
  min-height: 100%;
}

.content { /* This is for the content */
  position: fixed;
  z-index: 1; /* This can be set to any number greater than -1000. */
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  color: #f1f1f1;
  width: 100%;
  padding: 20px;
}

Bootstrap 5 (update 2019)引导程序 5(2019 年更新)

Use helper classes to eliminate the need for a lot of extra CSS...使用帮助类来消除对大量额外 CSS 的需求...

<header>
    <div class="position-absolute top-50 w-100">
        <h1 class="text-white text-center">Overlay Text</h1>
    </div>
    <video autoplay loop id="video-background" class="position-absolute end-0 bottom-0 h-auto w-auto">
        <source src="https://www.w3schools.com/howto/rain.mp4" type="video/mp4"> Your brower is old
    </video>
</header>

Bootstrap 5 full screen video Bootstrap 5 全屏视频


Bootstrap 4 (original answer) Bootstrap 4(原始答案)

You can use absolute position and then overlay whatever you want on the video so it remains as a background...您可以使用绝对 position ,然后在视频上覆盖您想要的任何内容,使其保留为背景......

<header>
    <div class="overlay">
        Text
    </div>
    <video id="video-background">
        ...
    </video>
</header>

#video-background {
  position: absolute;
  right: 0; 
  bottom: 0;
  min-width: 100%; 
  min-height: 100%;
  width: auto; 
  height: auto;
  z-index: -100;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color:rgba(0,0,0,0.5);
}

Demo: https://codeply.com/p/aUjjU2uIXH演示: https://codeply.com/p/aUjjU2uIXH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM