[英]CSS : How do I vertically-center align text in a div ? (or any other elements which is not a table)
[英]How do I vertically center text in a DIV using CSS?
我在使用CSS的DIV中垂直居中放置文本時遇到麻煩。 我在SO和其他地方都遵循了相關的答案,但似乎無法使其正常工作。 我的簡化方案是
<div class="outer">
<div class="header-title">
<span class="header-text">This is the title</span>
</div>
</div>
和
div.outer {
position: relative;
height: 200px;
border-style: solid;
border-width:1px;
}
div.header-title {
position: absolute;
font-size: 36px;
height: 100%; /* required */
color: green;
border-style: dashed;
border-width:1px;
}
span.header-text {
vertical-align: middle;
border-style: dotted;
border-width:1px;
}
但結果(在Safari中)使“標題文本” span
位於“標題標題” div
的頂部:
您的示例顯示了將div垂直居中的一種很好的解決方法:
display: table-cell;
vertical-align: middle;
因此,將其添加到您的div CSS中:
div.header-title {
position: absolute;
font-size: 36px;
height: 100%; /* required */
color: green;
display: table-cell;
vertical-align: middle;
border-style: dashed;
border-width:1px;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.