gpt4 book ai didi

css - 未指定父级高度时垂直居中文本

转载 作者:太空宇宙 更新时间:2023-11-04 02:04:13 25 4
gpt4 key购买 nike

对于固定高度的容器,我会简单地使 line-height 属性与高度匹配。不指定容器高度怎么办?

最佳答案

有两种方法。这是表格方法。另一种方法是使父容器相对,并使用 top:50%; 使 p 标签成为绝对。如果您不支持 ie8 或更低版本,您可以使用 flexbox。这是学习 flexbox 的重要资源:http://flexboxfroggy.com/

.container{
width: 300px;
height: 400px;
}

.content{
width: 100%;
height: 70%;
display: table;
}

p{
display: block;
display: table-cell;
vertical-align: middle;
}
<div class="container">
<div class="content">
<p>
Center text
</p>
</div>
</div>

关于css - 未指定父级高度时垂直居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41156140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com