首页 > 解决方案 > 使用 flex 作为显示,并有一个视频作为孩子不尊重我的填充设置

问题描述

我想在 HTML 页面上有:

这是我所拥有的:

body { margin: 0; }

#main {
  position: absolute;
  left:0px;
  top:0px;
  
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  background:red;
}

#header {
  background:yellow;
}

#content {
  background:gray;
  flex:1;
}

table {
  width: 100%;
  height: 100%;
  background: blue;
  color: white;
}

video {
  width: 100%;
  height: 100%;
}
<html>

<body>

<div id="main">

<div id="header">
Header
</div>
<div id="content">


<table>
<tr>
  <td>
    <video controls>
      <source src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4" type="video/mp4">
    </video>
  </td>
</tr>
</table>

</div>


</div>

</body>
</html>

现在,问题出在视频标签上(如果您运行此代码段,将会出现滚动条——我不想要它们)。

如果我删除视频标签,一切都会按预期工作。但是,如果有 video 标签,它会破坏 'flex' 选项:它会放大 td,因此表格也会变大,然后内容也会变大 - 因此启用滚动条并且页面将不适合屏幕。

也许在加载媒体时调整了视频的大小,那时所有的 css 操作都完成了。

但是,即使在播放期间,有什么方法可以使视频的大小与其父级(td)的 100% 宽度和高度完全相同?

标签: htmlcsshtml5-video

解决方案


你可以制作视频position:absolute

body {
  margin: 0;
}

#main {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  background: red;
}

#header {
  background: yellow;
}

#content {
  background: gray;
  flex: 1;
  min-height: 0;
}

table {
  width: 100%;
  height: 100%;
  background: blue;
  color: white;
}

td {
  position: relative;
}

video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
<div id="main">

    <div id="header">
      Header
    </div>
    <div id="content">


      <table>
        <tr>
          <td>
            <video controls>
      <source src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4" type="video/mp4">
    </video>
          </td>
        </tr>
      </table>

    </div>


  </div>


推荐阅读