简体   繁体   English

将弹性项目与弹性端对齐

[英]Aligning flex items to the flex-end

Hei,

I'm creating grid layout and when trying to align items on flex-end or start, height/width becomes zero. 我正在创建网格布局,当尝试在flex-end或start上对齐项目时,高度/宽度变为零。 What I'm wondering is, is it possible to align items in flex container to the top/bottom/center WITHOUT specifying height/width? 我想知道的是,是否可以将flex容器中的项目与顶部/底部/中心对齐,而无需指定高度/宽度? In my case I didn't specify any height and it doesn't work. 在我的情况下,我没有指定任何高度,这是行不通的。 If I specify it, it works. 如果我指定它,它将起作用。 Line that doesn't work is commented in first example. 第一个示例中注释了行不通的行。

Here is code: 这是代码:

 .flex-container { display: flex; background: silver; width: 400px; height: 200px; //align-items: flex-start; } .flex-item { background: blue; flex: 1 0 10px; margin: 5px; } .flex-container2 { display: flex; background: silver; width: 400px; height: 200px; align-items: flex-end; } .flex-item2 { background: blue; height: 50px; flex: 1 0 10px; margin: 5px; } 
 <div class="flex-container"> <div class="flex-item"></div> <div class="flex-item"></div> <div class="flex-item"></div> <div class="flex-item"></div> </div> <br> <div class="flex-container2"> <div class="flex-item2"></div> <div class="flex-item2"></div> <div class="flex-item2"></div> <div class="flex-item2"></div> </div> 

As the default value for align-items is stretch , hence they fill their parents height, and you change it to flex-start / flex-end , the row flex items becomes 0 in height, and will need content (or a height/min-height) for you to actually see them. 由于align-items的默认值为stretch ,因此它们填充了其父代的高度,您将其更改为flex-start / flex-end ,行flex项的高度变为0,并且需要内容(或高度/分钟-height),以便您实际看到它们。

 .flex-container { display: flex; background: silver; width: 400px; height: 200px; align-items: flex-start; } .flex-item { background: blue; min-height: 20px; flex: 1 0 10px; margin: 5px; } .flex-item:nth-child(even) { align-self: flex-end; } .flex-container2 { display: flex; background: silver; width: 400px; height: 200px; align-items: flex-end; } .flex-item2 { background: blue; height: 50px; flex: 1 0 10px; margin: 5px; } 
 <div class="flex-container"> <div class="flex-item">They need content or a height/min-height</div> <div class="flex-item">They need content or a height/min-height</div> <div class="flex-item"></div> <div class="flex-item"></div> </div> <br> <div class="flex-container2"> <div class="flex-item2"></div> <div class="flex-item2"></div> <div class="flex-item2"></div> <div class="flex-item2"></div> </div> 

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

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