简体   繁体   English

我不明白为什么我的高度为 auto 的 div 占据了其父级的全高

[英]I don't understand why my div with height auto is taking the full height of its parent

I have a div.grid that contains multiple div.column inside using flexbox.我有一个包含多个一div.grid div.column内使用Flexbox的。 When a div.column increases in height due to having more content inside, all other div.column also increase in height.div.column由于里面有更多内容而增加高度时,所有其他div.column的高度也会增加。

It is to my understaind that this shouldn't help as their height is set to auto.据我所知,这应该无济于事,因为它们的高度设置为自动。

Also, my grid is using the flex display, with flex-wrap: wrap .此外,我的网格使用 flex 显示,带有flex-wrap: wrap I think is what is causing it, but I haven't found any explanation to why it is changing the children's height.我认为这是造成它的原因,但我还没有找到任何解释为什么它会改变孩子们的身高。

I have tried changing the following properties, in different combinations and manners, of parent and children: position, display, height.我曾尝试以不同的组合和方式更改父和子的以下属性:位置、显示、高度。

These are the computed styles only, not my actual code.这些只是计算样式,而不是我的实际代码。

 div.grid { display: flex; flex-wrap: wrap; width: 960px; } div.column { display: block; flex-basis: 37.5%; flex-grow: 0; flex-shrink: 0; /* height is actually set to auto, but it is computed like this */ height: 132px; }
 <div class="grid"> <div class="column">something</div> <div class="column">something<br>else<br>here</div> <div class="column">something</div> </div>

I expected the children to not fill the full height of the parent when the children don't all have the same amount of content.当孩子们没有相同数量的内容时,我希望孩子们不会填满父母的全部高度。

在此处输入图片说明

As LGSon has asnwered in the comments:正如 LGSon 在评论中所回答的那样:

Using flex: display automatically defaults align-items to stretch making the height of the children stretch to fill the height of the wrapper.使用flex: display自动默认align-items stretch使子align-items的高度拉伸以填充包装器的高度。 It is also not shown in the computed properties, which is why it didn't cross my mind to check it.它也没有显示在计算属性中,这就是为什么我没有想到要检查它。

Therefore, the solution was to add align-items: flex-start to .grid .因此,解决方案是将align-items: flex-start.grid

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

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