3 回答

TA貢獻1770條經驗 獲得超3個贊
柔性箱方式
垂直對準
注:.jumbotron
vertical-center
<div class="jumbotron vertical-center"> <!-- ^--- Added class --> <div class="container"> ... </div></div>
.vertical-center { min-height: 100%; /* Fallback for browsers do NOT support vh unit */ min-height: 100vh; /* These two lines are counted as one :-) */ display: flex; align-items: center;}
重要
A 百分比
值.的值 height
或 min-height
屬性相對于 height
因此,應該指定 height
顯式的父母。 供應商前綴/舊的Flexbox語法由于簡潔而在已發布的代碼段中省略,但在線示例中存在。 在一些舊的web瀏覽器中,比如Firefox 9 (其中我測試過) 撓性容器- .vertical-center
在這種情況下-將不會占用父級內部可用的空間,因此我們需要指定 width
財產,如: width: 100%
.另外,在上面提到的一些web瀏覽器中,FLEX項目- .container
在這種情況下-可能不會出現在中心水平。似乎申請的左/右 margin
的 auto
對彈性物品沒有任何影響。 因此,我們需要通過 box-pack / justify-content
.
舊式web瀏覽器的傳統方式
vertical-align: middle
中間
將方框的垂直中點與父框的基線加上父框x高度的一半對齊。
.vertical-center
height
height
.container
聚在一起
.vertical-center
::before
::after
display
.container
inline-block
.
vertical-align: middle;
<div class="jumbotron vertical-center"> <div class="container"> ... </div></div>
.vertical-center { height:100%; width:100%; text-align: center; /* align the inline(-block) elements horizontally */ font: 0/0 a; /* remove the gap between inline(-block) elements */}.vertical-center:before { /* create a full-height inline block pseudo=element */ content: " "; display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ height: 100%;}.vertical-center > .container { max-width: 100%; display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ /* reset the font property */ font: 16px/1 "Helvetica Neue", Helvetica, Arial, sans-serif;}
auto
0
display
none
@media (max-width: 768px) { .vertical-center:before { height: auto; /* Or */ display: none; }}
footer
/header
relative
, absolute
z-index

TA貢獻1831條經驗 獲得超4個贊
添加Bootstrap.css,然后將其添加到CSS中
html, body{height:100%; margin:0;padding:0}
.container-fluid{
height:100%;
display:table;
width: 100%;
padding: 0;
}
.row-fluid {height: 100%; display:table-cell; vertical-align: middle;}
.centering {
float:none;
margin:0 auto;
}
Now call in your page
<div class="container-fluid">
<div class="row-fluid">
<div class="centering text-center">
Am in the Center Now :-)
</div>
</div>
</div>
- 3 回答
- 0 關注
- 627 瀏覽
相關問題推薦
添加回答
舉報