idl*_*erg 3 html css css3 flexbox twitter-bootstrap-3
我正在使用通常被称为类的类将我的内容在flex容器内水平和垂直居中。它适用于所有现代浏览器,但是即使是相当新的Internet Explorer版本也给我带来麻烦。
这是我标记的相关部分,充分利用了Bootstrap的嵌入响应类来保持比率。
HTML:
<div class="container-fluid embed-responsive embed-responsive-16by9" style="background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.5) 100%), url('http://placehold.it/800X600') no-repeat;background-size:cover;background-position:center">
<div class="content-center">
<div class="container">
<div class="row">
<div class="col-xs-12 text-center">
<button id="play-video" type="button" class="btn btn-default" data-toggle="modal" data-target="#video-modal" data-youtube="scWpXEYZEGk">
Play
</button>
</div>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
.content-center {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
-ms-flex-pack: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
float: none;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这些元素不是居中,而是在页面的右侧。
这是截图供参考
该问题是由两个因素共同引起的:
这是有争议的代码:
引导样式:
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto; /* <-- source of the problem in IE */
margin-left: auto; /* <-- source of the problem in IE */
}
Run Code Online (Sandbox Code Playgroud)
作者风格:
.content-bottom {
position: absolute;
bottom: 0;
left: 0;
right: 0;
display: flex;
align-items: center;
justify-content: center; /* <-- source of the problem in IE */
float: none;
max-height: 30%;
}
Run Code Online (Sandbox Code Playgroud)
长话短说,要使内容在IE中正确居中,请使用automargin 或 justify-content: center。
由于CSS优先级的实现不正确,因此同时使用这两种方法都无法在IE中居中。
有关详细信息,请参见我的回答:Flex自动边距在IE10 / 11中不起作用
修改后的演示(justify-content禁用)