Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set flexbox children to have different heights to use up available space

Tags:

css

flexbox

Using a two-column flexbox layout, how can different-sized children be made to fill all available space, instead of all children having the height of the tallest child of the row?

I set up a demo on jsbin that illustrates the problem. I'd like for all the children to be the size of their wrapped contents.

#container {

 width: 800px;
  display: flex;
  flex-wrap: wrap;
}

.cell {
  width: 300px;
  flex; 1 auto;
}


<div id="container">

<div class="cell">
Cells with arbitrarily long content.</div>

<div class="cell">
</div>

<div class="cell">
</div>

<div class="cell">
</div>

<div class="cell">
</div>

</div>

</body>
</html>
like image 972
verysuperfresh Avatar asked Jan 07 '14 16:01

verysuperfresh


People also ask

Can you set height of flexbox?

It can be changed by using the flex-direction property. To use flexbox, we have to set display: flex or inline-flex to flex-container. By default, the height and width of a flex-container are set to auto. But we can define a fixed amount to them.

How do you make flexbox children 100% height of their parents using CSS?

Getting the child of a flex-item to fill height 100%Set position: absolute; on the child. You can then set width/height as required (100% in my sample).

How do I make remaining space on my flexbox?

Use the flex-grow property to make a flex item consume free space on the main axis. This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

How do you set the height of a flex item?

Instead of max-height: 250px you should use flex: 0 0 250px and with flex-direction: column it will set height of element to 250px.


1 Answers

This is how Flexbox rows are expected to behave. Flexbox is not meant to recreate Masonry with pure CSS: items in one row cannot occupy space allocated for a preceding/following row (same goes for columns if you're using column orientation). You can use align-items to prevent them from stretching, but that's about it:

http://cssdeck.com/labs/9s9rhrhl

#container {   width: 800px;   display: flex;   flex-wrap: wrap;   align-items: flex-start; }  .cell {   width: 300px;   flex: 1 auto;   padding: 10px;   border: 1px solid red; } 

Otherwise, you should be using the column orientation or the multi-column module (see this SO answer: https://stackoverflow.com/a/20862961/1652962)

like image 188
cimmanon Avatar answered Oct 18 '22 20:10

cimmanon