簡體   English   中英

使用flexbox垂直對齊單個子元素

[英]Aligning single child elements vertically with flexbox

我正在嘗試均勻分布可變數量的列,其中包含可變數量的垂直居中的子元素。 我幾乎想要的地方,但是如果我將子元素的數量減少到1,則垂直對齊失敗(第一行和第五行)。

我究竟做錯了什么?

守則( http://codepen.io/anon/pen/bpvMda ):

 .myView { margin: auto; display: flex; flex-direction: row; justify-content: space-around; height: 500px; width: 500px; } .wpType { flex: 0 1 auto; display: flex; flex-flow: row wrap; align-items: space-around; align-content: center; padding: 0; margin: 0; list-style: none; border: 1px solid silver; } .wpType:nth-child(even){ background: blue; } .wpType:nth-child(odd){ background: red; } .wp { flex: 0 1 auto; padding: 5px; width: 100px; height: 100px; margin: 10px; background: white; line-height: 100px; color: white; font-weight: bold; font-size: 2em; text-align: center; } 
 <div class="myView"> <div class="wpType"> <div class="wp"></div> </div> <div class="wpType"> <div class="wp"></div> <div class="wp"></div> <div class="wp"></div> </div> <div class="wpType"> <div class="wp"></div> <div class="wp"></div> <div class="wp"></div> </div> <div class="wpType"><div class="wp"></div> <div class="wp"></div></div> <div class="wpType"><div class="wp"></div></div> </div> 

在所有居中元素的父div上使用這些屬性。

display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;

Codepen

暫無
暫無

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

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