【问题标题】:Centering <video> issue居中 <video> 问题
【发布时间】:2023-12-22 13:33:01
【问题描述】:

我是这样用的:

.centr {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

网站是这样的:

问题是画中画,不到位

【问题讨论】:

  • (相关的)HTML 是什么样的?
  • 我们不需要标题中的标签;我们在标签区域中有它们。
  • 转换不需要前缀。每个浏览器都支持它。然而,它是使用所有选项(弹性、自动边距、网格......)居中的最糟糕的方法

标签: html css video centering


【解决方案1】:

在父 div 中居中 &lt;video&gt; 元素的一种方法是使用弹性框:

.container {
  /* just styling, nothing important */
  background-color: gray;
  width: 500px;
  height: 200px;

  /* note these lines below */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

video {
  width: 300px;
  height: 150px;
  background-color: #000;
}
<div class="container">
  <video></video>
</div>

【讨论】:

    【解决方案2】:

    要使自动边距起作用,中心类必须有一个宽度:

    .center { 
    margin: 0 auto;
     width: 400px;
     }
    

    然后,我会将中心类应用于视频本身,而不是容器:

    <video class='center'>
    
    </video>
    
    

    【讨论】: