【问题标题】:Make video fit 100% with any screen resolution使视频适合任何屏幕分辨率 100%
【发布时间】:2016-08-25 06:55:18
【问题描述】:

我有一个具有以下属性的视频,帧宽度: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:

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

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

有什么想法吗?谢谢。

【问题讨论】:

标签: css asp.net-mvc html twitter-bootstrap


【解决方案1】:

使用object-fit:cover;是我的解决方案。

从 2021 年起,所有主流浏览器都支持它。 object-fit support

CSS 示例:

.video-wrapper {
    overflow: hidden;
    display: flex;
    align-items: center; 
    justify-content: center;
}

.video-wrapper video {
    object-fit: cover;
}

【讨论】:

    【解决方案2】:

    效果惊人:https://css-tricks.com/fluid-width-video/

    video {
        /* override other styles to make responsive */
        width: 100% !important;
        height: auto !important;
    }
    

    【讨论】:

      【解决方案3】:
      .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%);
          }
      

      这对我有用。

      【讨论】:

        【解决方案4】:

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

        <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>

        【讨论】:

        • 这个高度是干什么用的?为什么是600?此外,plays-inline 应该是 playinline
        【解决方案5】:

        您现在可以使用 object-fit 属性。此属性专门设计用于管理 &lt;img&gt;&lt;video&gt; 元素的响应大小。现在所有现代浏览器都支持它。

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

        【讨论】:

        • 你能发布一个完整的例子吗?
        • 目前,@raumus 解决方案肯定比我的object-fit 解决方案更好,因为 Microsoft Edge (v16.16299) 尚不支持它... :-(
        • Ofc 即使有了新的浏览器,微软也落后了。否则对象拟合真的很好!!!
        • 在我的情况下,我希望视频能够整齐地放入可用空间中,即只放大直到它触及垂直或水平边缘。我发现 object-fit: contain 做得很好。
        【解决方案6】:

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

        所以你的 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:

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

        【讨论】:

        • 知道我们是否可以为这样的嵌入式 Youtube iframe 实现相同的目标?
        • ` /* 将视频设为至少 100% 宽和高 */ min-width: 100%;最小高度:100%; ` 对我有用
        • 这不适用于垂直视频,例如 1080x1920 分辨率。视频看起来略微放大,因此顶部和底部的部分被切断。我自己还在寻找解决方案,如果找到了会返回这里。
        • @StefanMidjich - 就是这样。
        【解决方案7】:

        你可以使用 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>

        【讨论】:

        • 哦,请不要。这已经过时了,即使是 2016 年的答案
        猜你喜欢
        • 2023-03-18
        • 1970-01-01
        • 2011-07-01
        • 2011-09-04
        • 2017-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多