简体   繁体   English

如何使弹性容器中的所有子代都具有相同的高度

[英]How to make all children in a flex-container to have the same height

I am stuck with this problem. 我陷入这个问题。

I am trying to make all the child divs within a flex container to have the same height as the tallest child div,here it is 100px and i want them to be aligned center 我正在尝试使flex容器中的所有子div具有与最高子div相同的高度,这里是100px,我希望它们对齐中心

not sure how can i do it. 不知道我该怎么办。 I tried flex:1 did not work, i tried doing align:stretch too but does not seem to work. 我尝试了flex:1没用,我也尝试过align:stretch,但是似乎没用。

can some one please help on this. 有人可以帮忙吗?

 .container { display: flex; flex-direction: row; border: 1px solid black; width: 500px; align-items: center; height:160px; } .container div:not(:first-child) { background: green; margin-top: 20px; margin-left: 20px; } .container div:first-child { height: 100px; background: red; margin-top: 20px; margin-left: 20px; text-align: center; } 
 <div class="container"> <div> item-1 </div> <div> item-2 </div> <div> item-3 </div> </div> 

To do this you just need to wrap your inner div 's in another div and set align-items: center on container element. 为此,您只需要将内部div包裹在另一个div并设置align-items: centercontainer元素为align-items: center

 .container { display: flex; border: 1px solid black; align-items: center; width: 500px; height: 150px; } div > div { display: flex; } .container div div:not(:first-child) { background: green; margin-left: 20px; } .container div div:first-child { height: 100px; background: red; margin-left: 20px; text-align: center; } 
 <div class="container"> <div> <div> item-1 </div> <div> item-2 </div> <div> item-3 </div> </div> </div> 

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

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