简体   繁体   English

是否可以使“ flex-flow:column” flexbox的所有子级与最高的子级具有相同的高度?

[英]Is it possible to make all the children of a `flex-flow: column` flexbox the same height as the tallest child?

If I have a flexbox that is styled with flex-flow: column , and that container has five child div with variable heights, is it possible to make all the children be the same height as the tallest? 如果我有一个使用flex-flow: column样式设置的flexbox,并且该容器有五个高度可变的子级div ,是否可以使所有子级的高度与最高子级的高度相同?

I'm not necessarily looking to fill the container with them (ie have them fill any excess space), just all be the same height. 我并不一定要用它们填充容器(即,让它们填充任何多余的空间),而只是高度都相同。

In the snippet below, the five child div all have different content, which causes them to be different heights; 在下面的代码段中,五个子div内容都不同,这导致它们的高度不同; is it possible to make all five be as tall as the uppermost div , which has the most content? 是否可以使所有五个都与内容最多的最上面的div一样高?

 html { font-family: Arial, sans-serif; } *, *:before, *:after { box-sizing: border-box; border: none; margin: 0; padding: 0; } body { background: #333; color: #333; } .container { background: firebrick; display: flex; flex-flow: column; height: 50rem; margin: 0 auto; padding: 1rem; width: 30rem; } .panel { background: white; padding: 1rem; } .panel:nth-child(even) { background: lightgrey; } 
 <div class="container"> <div class="panel">Lorem ipsum dolor sit amet consectetur adipisicing elit. Magnam pariatur commodi ipsum ex quam eius voluptatibus fugit vero, consectetur explicabo cum magni fugiat natus.</div> <div class="panel">Lorem, ipsum dolor sit amet consectetur adipisicing elit.</div> <div class="panel">this is a test</div> <div class="panel">Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, commodi a!</div> <div class="panel">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Saepe, nihil pariatur enim, porro temporibus harum, quo omnis beatae eaque soluta totam ipsa?</div> </div> 

The simple answer is "no". 简单回答是不”。 Flexbox distributes space among flex items based on the size of the container, not the size of a particular sibling. Flexbox根据容器的大小而不是特定同级的大小在弹性项目之间分配空间。 You'll need to use a script. 您将需要使用脚本。

If you don't mind them stretching to fit container height you can set children flex-basis to 20%: 如果您不介意它们会拉伸以适合容器的高度,则可以将子级flex-basis设置为20%:

 html { font-family: Arial, sans-serif; } *, *:before, *:after { box-sizing: border-box; border: none; margin: 0; padding: 0; } body { background: #333; color: #333; } .container { background: firebrick; display: flex; flex-flow: column; height: 50rem; margin: 0 auto; padding: 1rem; width: 30rem; } .panel { background: white; padding: 1rem; flex-basis: 20%; } .panel:nth-child(even) { background: lightgrey; } 
 <div class="container"> <div class="panel">Lorem ipsum dolor sit amet consectetur adipisicing elit. Magnam pariatur commodi ipsum ex quam eius voluptatibus fugit vero, consectetur explicabo cum magni fugiat natus.</div> <div class="panel">Lorem, ipsum dolor sit amet consectetur adipisicing elit.</div> <div class="panel">this is a test</div> <div class="panel">Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, commodi a!</div> <div class="panel">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Saepe, nihil pariatur enim, porro temporibus harum, quo omnis beatae eaque soluta totam ipsa?</div> </div> 

You can do this with flex-basis , with the catch that you have to know how many children there will be. 您可以使用flex-basis来做到这一点,并且必须知道将要有多少个孩子。 You can also use flex-grow , but that only distributes the extra space available, so the tallest elements will still be taller than the shorter ones: 您还可以使用flex-grow ,但是只会分配可用的额外空间,因此最高的元素仍然比较短的元素高:

 html { font-family: Arial, sans-serif; } *, *:before, *:after { box-sizing: border-box; border: none; margin: 0; padding: 0; } body { background: #333; color: #333; } .container { background: firebrick; display: flex; flex-flow: column; height: 50rem; margin: 0 auto; padding: 1rem; width: 30rem; } .panel { background: white; padding: 1rem; flex-basis: 20%; } .panel:nth-child(even) { background: lightgrey; } 
 <div class="container"> <div class="panel">Lorem ipsum dolor sit amet consectetur adipisicing elit. Magnam pariatur commodi ipsum ex quam eius voluptatibus fugit vero, consectetur explicabo cum magni fugiat natus.</div> <div class="panel">Lorem, ipsum dolor sit amet consectetur adipisicing elit.</div> <div class="panel">this is a test</div> <div class="panel">Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, commodi a!</div> <div class="panel">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Saepe, nihil pariatur enim, porro temporibus harum, quo omnis beatae eaque soluta totam ipsa?</div> </div> 

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

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