Html 使视频 100% 适合任何屏幕分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36949747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:12:04  来源:igfitidea点击:

Make video fit 100% with any screen resolution

cssasp.net-mvchtmltwitter-bootstrap

提问by Qwerty

I have a video with the following properties, Frame width: 1920 and Frame Height: 1080. I need its width and height to be 100% thus filling up the whole screen. And it needs to be responsive too. So far, I have this code :

我有一个具有以下属性的视频,帧宽度:1920 和帧高度:1080。我需要它的宽度和高度为 100%,从而填满整个屏幕。它也需要响应。到目前为止,我有这个代码:

<video class="hidden-xs hidden-sm hidden-md hidden-custom videosize embed-responsive-item" autoplay="autoplay" loop="loop">
    <source src="~/Videos/myvideo.mp4" type="video/mp4" />
</video>

css:

css:

   .videosize {
    position:absolute;
    z-index:-1;
    top:0;
    left:0;
    width:100%; 
    height:100vh;
}

With the code above it fits perfectly with a 1680 x 1050 screen resolution, however with other resolution, it takes up 100% of the height then the width adjusts leaving white spaces on both sides.

使用上面的代码,它非常适合 1680 x 1050 的屏幕分辨率,但是在其他分辨率下,它会占据高度的 100%,然后调整宽度,在两侧留下空白。

Any idea ? Thanks.

任何的想法 ?谢谢。

回答by raumus

Found a good solution here: http://codepen.io/shshaw/pen/OVGWLG

在这里找到了一个很好的解决方案:http: //codepen.io/shshaw/pen/OVGWLG

So your CSS would be:

所以你的 CSS 将是:

.video-container {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
  height: 100%; 
  overflow: hidden;
}
.video-container video {
  /* Make video to at least 100% wide and tall */
  min-width: 100%; 
  min-height: 100%; 

  /* Setting width & height to auto prevents the browser from stretching or squishing the video */
  width: auto;
  height: auto;

  /* Center the video */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
}

HTML:

HTML:

<div class="video-container">
  <video>
    <source src="~/Videos/myvideo.mp4" type="video/mp4" />
  </video>
</div>

回答by David

You can now use the object-fit property. This property has been designed especially to manage responsive size for <img>and <video>elements. It is now supported by all modern browsers.

您现在可以使用 object-fit 属性。此属性专为管理<img><video>元素的响应大小而设计。现在所有现代浏览器都支持它。

.videosize {
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    width: 100%; 
    height: 100%;
    object-fit: cover;
}

回答by Michael Staples

I Set the height of the video Tag and it solved the problem:

我设置了视频标签的高度,它解决了问题:

<video height="600" autoplay="true" loop="true" muted="true" plays-inline="" style="position: absolute; right: 0; top: 0; min-width:100%; z-index: -100; object-fit: cover;">
      <source src="assets/vid/grapes.mp4" type="video/mp4"> </video>

回答by Abhishek Meharia

.video-container {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
        object-fit: fill;
    }

    .video-container video {
        min-width: 100%;
        min-height: 100%;
        width: auto;
        height: auto;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }

This worked for me.

这对我有用。

回答by mlegg

Can you use an iframe?

你可以使用 iframe 吗?

/* Flexible iFrame */
 
.flexible-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 30px;
    height: 0;
    overflow: hidden;
}
 
.flexible-container iframe,   
.flexible-container object,  
.flexible-container embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
<!-- Responsive iFrame -->
<div class="flexible-container">
<iframe src="URL" frameborder="0" style="border:0"></iframe>
</div>