我有两个div想要垂直居中在另一个div中。目前我的代码是:
现在我明白这里发生了什么,但我希望左侧div在容器内垂直对齐,右侧div也是如此。但它们作为一对而不是单独地垂直对齐。我尝试了各种方法,但似乎无法使其工作。我有两个div想要垂直居中在另一个div中。目前我的代码是:
现在我明白这里发生了什么,但我希望左侧div在容器内垂直对齐,右侧div也是如此。但它们作为一对而不是单独地垂直对齐。我尝试了各种方法,但似乎无法使其工作。float: left
from #left
and #right
.Instead, use display: inline-block
:
#left, #right {
display: inline-block;
vertical-align: middle;
}
display: inline-block
, you have to deal with the whitespace issue. I chose to remove the whitespace in the HTML between </div>
and <div id="right">
. See here for what happens if you don't do that. Removing the whitespace really is the easiest fix, but there are other ways.使用Flexbox解决垂直居中问题:
#container {
display: flex;
align-items: center;
}
OP的原始代码被修改了:http://jsfiddle.net/o3pmyb8c/
如果您也想水平居中,可以添加justify-content: center;
display: table-cell
的方式来看,你不关心支持 IE7? - thirtydot