[英]Vertically centering a text in a 100% height
I have a div with a height of 100%, hence it takes all full window height, and obviously as you scroll down it scrolls to the next divs.我有一个高度为 100% 的 div,因此它占用了所有完整的窗口高度,很明显,当您向下滚动时,它会滚动到下一个 div。
The problem is that text is stuck on top and I want it to be centered, until a user decides to scroll down.问题是文本卡在顶部,我希望它居中,直到用户决定向下滚动。
Below is the html下面是html
<div id="header1">
<h1>Welcome to test</h1>
<p></p>
</div>
Below is the css:下面是css:
#header1 {
width:100%;
background: #6C7E82;
top: 50%;
min-height: 100%;
}
Use Flexbox.使用弹性盒。 http://jsfiddle.net/xpjkbhvm/
http://jsfiddle.net/xpjkbhvm/
#header1 {
width:100%;
min-height: 100%;
display: box; // IE10 fallback
display: flex;
align-items: center;
justify-content: center;
}
It's not supported by IE 8-9 but otherwise it's the most reliable method. IE 8-9 不支持它,但它是最可靠的方法。
You can try the below code:你可以试试下面的代码:
#header1 {
width:100%;
background: #6C7E82;
height: 100%;
overflow:hidden;
display:table;
}
#header1 h1{
display: table-cell;
vertical-align: middle;
}
If you don't wanna use "display:table" you can use below code as well:如果您不想使用“display:table”,您也可以使用以下代码:
#header1:before{
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
#header1 {
width:100%;
background: #6C7E82;
}
#header1 h1{
display: inline-block;
vertical-align: middle;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.