响应式网站的视频高度和宽度

时间:2015-01-29 00:18:26

标签: html5 video mobile responsive-design

我在响应式网站上使用视频HTML5标记。我将高度和宽度设置为100%并且它工作正常,除了在移动版本中它正在破坏布局。

网址:omob-2.myshopify.com

<div style="height: 100%; width: 100%;"> 
<video width="100%" height="100%" autoplay>
<source src="intro_12_07_14.mp4" type="video/mp4">
</video> 
</div>

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

在不支持视频标记的设备上,您会显示图片。这里有一个答案How can I display an image if browser does not support HTML5's <video> tag

编辑:设置css样式的宽度和高度,而不是视频标记。仅设置宽度,以保持尺寸比例,就像这样。

video {
    width: 100%;
    height: auto   !important;
}

答案 1 :(得分:0)

使用 CSS3 transform translate(-50%, -50%)将视频置于页面中央:

Html代码

      <div class="video-container">
        <video autoplay loop="true" width="1280" height="720">
         <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
       </video>
     </div>

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%;
      -webkit-transform: translate(-50%, -50%);
              transform: translate(-50%, -50%);
    }
    body {
      background: #000;
      color: #fff;
      font-family: 'Oxygen', sans-serif;
    }

请参见demo

答案 2 :(得分:0)

您可以同时使用 max-width 属性或 object-fit 属性来实现此目的。请参阅参考资料:Using the object-fit propertyUsing the max-width property with fill-available

/* Using fill-available on the max-width property */

/* A box you would like to place the video in*/
.wrapper {
   width: 600px
   height: 300px;
}

/* The video */
.wrapper_video > video {
   width: 100%;
   max-width: -webkit-fill-available;
   max-width: fill-available;
}


/* The object-fit property defines how an element responds to the height 
and width of its content box.*/

/* A box you would like to place the video in*/
.wrapper {
   width: 600px
   height: 300px;
}

/* The video */
.wrapper_video > video {
   width: 100%;
   height: 100%;
   object-fit: cover;
}
相关问题