问题描述
我用这种方式:
.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%);
}
问题是画中画,不到位
解决方法
在父 div 中居中 <video>
元素的一种方法是使用 flexbox:
.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>
要使自动边距起作用,中心类必须有一个宽度:
.center {
margin: 0 auto;
width: 400px;
}
然后,我将中心类应用到视频本身,而不是容器:
<video class='center'>
</video>