我有一个绝对定位的div。
我正在尝试将其与flex垂直对齐:
.container{
position: absolute;
display: flex;
align-items: center;
justify-content: center;
....
HTML:
<div class="container">
<div>
<p>hello</p>
<p>to</p>
<p>you</p>
...
这可能吗?我还能如何垂直对齐.container
?请注意,我不想使用拉伸方法。
position: absolute;
top: 0;
bottom: 0;
left:0;
right:0;
margin: auto;
容器也可以是任何高度。
ps请没有桌子。
要使.container
元素与flex居中,您需要使父对象成为flex容器。
将justify-content
和align-items
属性宣布Flex容器,而是可以应用到子元素(弯曲项)。
但是,由于.container
绝对定位,因此flex无法正常工作:
或者,您可以尝试以下操作:
html { height: 100%; }
body {
height: 100%;
position: relative;
}
.container {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
有关此居中方法的说明,请参见以下文章:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句