简体   繁体   English

使用flexbox垂直对齐单个子元素

[英]Aligning single child elements vertically with flexbox

I'm trying to have a variable number of columns evenly spaced, which contain a variable number of child elements vertically centered. 我正在尝试均匀分布可变数量的列,其中包含可变数量的垂直居中的子元素。 I'm almost where I want to, but if I reduce the amount of child elements to one, the vertical alignment fails (row one and five). 我几乎想要的地方,但是如果我将子元素的数量减少到1,则垂直对齐失败(第一行和第五行)。

What am I doing wrong? 我究竟做错了什么?

The Code ( http://codepen.io/anon/pen/bpvMda ): 守则( 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> 

Use these properties on your parent div of all of the centered elements. 在所有居中元素的父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 Codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM