簡體   English   中英

將多行彈性項目垂直居中

[英]Vertically center multiple lines of flex items

我正在嘗試垂直對齊包裝紙的子DIV,但不幸的是沒有。 要求是具有兩個子DIV垂直和水平居中的屏幕包裝紙的全寬和高度。 (div的高度由jQuery處理以使窗口高)

注意:我不想讓子DIV width:100%

這是我嘗試過的:

 .wrapper { width: 100%; height:100%; min-height:600px; /*just for example*/ display: flex; justify-content: center; align-items: center; flex-wrap: wrap; background:#e1e1e1; } .row1 { background: #cdcdcd; } .row2 { background: #404040; color:#fff; } 
 <div class="wrapper"> <div class="row1">This is row one</div> <div class="row2">This is row two</div> </div> 

flex容器的初始設置為flex-direction: row 這意味着容器的子代(“柔性物品”)將連續排成一行。

要覆蓋此默認設置,您可以添加flex-wrap: wrap到容器中,並將項目的width: 100% ,這將強制每行一個項目。 但是您要說的是這種情況下這種方法不是一種選擇。

因此,另一個選擇是將容器切換為flex-direction: column

 .wrapper { width: 100%; height:100%; min-height:600px; /*just for example*/ display: flex; flex-direction: column; /* NEW */ justify-content: center; align-items: center; flex-wrap: wrap; background:#e1e1e1; } .row1 { background: #cdcdcd; } .row2 { background: #404040; color:#fff; } 
 <div class="wrapper"> <div class="row1">This is row one</div> <div class="row2">This is row two</div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM