text-align: center;
box-sizing: border-box;
color: #fff;
float: left;
width: 50%;
height: 65vh;
padding-top: 14%
這是我的CSS。我想要的是文字正好在中間,但它不起作用,因爲我的身高取決於屏幕高度。我該如何解決這個問題?垂直高度的CSS文本
text-align: center;
box-sizing: border-box;
color: #fff;
float: left;
width: 50%;
height: 65vh;
padding-top: 14%
這是我的CSS。我想要的是文字正好在中間,但它不起作用,因爲我的身高取決於屏幕高度。我該如何解決這個問題?垂直高度的CSS文本
您可以使用Flexbox佈局。
爲Flexbox的很好的指導:https://css-tricks.com/snippets/css/a-guide-to-flexbox/
顯示:彎曲組佈局了flexbox佈局
證明內容:中心集文字水平中心
對齊項:中心將文字設置爲垂直中心
.container {
width: 150px;
height: 150px;
border: 3px solid black;
display: flex;
justify-content: center;
align-items: center;
}
<div class="container">
<p class="text">Some Text</p>
</div>
你可以使用display:table-cell
.container {
width: 150px;
height: 65vh;
border: 3px solid black;
display: table-cell;
text-align: center;
vertical-align: middle;
}
<div class="container">
<p class="text">Some Text</p>
</div>
設置line-height
是一樣的height
值(65vh
):
只適用於不包裝的一行文字 - https://jsfiddle.net/bq7kLf99/1/ –
玩弄線高度。 – Lowkase
'line-height:100px'(或你的身高值),並移除'padding-top','heigh'屬性。 – gmunkhbaatarmn
但如果我做100px作爲行高,它不會響應? – Cooln