Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox: Fill remaining space but still wrap

Tags:

css

flexbox

Is there any way to tell flexbox items to fill the remaining space but wrap as soon as they are getting smaller than their content.

Some of the items have a fixed percentage width though.

HTML

<div class="grid">
  <div class="grid__item">column auto</div>
  <div class="grid__item one-third">column 33.333%</div>
  <div class="grid__item one-third">column 33.333%</div>
  <div class="grid__item">column auto</div>      
</div>

CSS

.grid {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  background: gray;
}

.grid__item {
  background: red;
  flex: 1;
}

.one-third {
  flex-basis: 33.333%;
  background: green;
}

I want the fixed width items (green) to always have a width of 33.333%. The other items (red) should fill the remaining space. If the screen is getting smaller I want the items to wrap if there is not enough space to show their content.

I thought I could simply use flex: 1 and flex-wrap: wrap on the container to do something like this, but as you might see in the pen it doesn't wrap at all. If I remove flex: 1 in .grid__item they kind of wrap, but not all the way down to very small screen sizes.

like image 487
Daniel Avatar asked Aug 06 '15 14:08

Daniel


People also ask

How do you make a flex item fill the remaining space?

You can use the flex-grow property to force an item to fill the remaining space on the main axis of a flex container. The item will expand as much as possible and occupy the free area.

How do you make a flex Div take only the required space?

You might be looking for the display: inline-flex property. It treats its children just like the display: flex property, except it itself does not automatically grow. Only works if there's no wrapping... I wanted wrap and parent div taking the smaller space possible.

Why is flexbox not wrapping?

If you are using flexbox and want the content to wrap, you must specify flex-wrap: wrap . By default flex items don't wrap. To have the images be three-across, you should specify flex-basis: 33.33333% .


2 Answers

You can certainly allow for wrapping but I'm not entirely sure the effect is what you are after. If you have an image of how you expect this to look after the wrap we can enhance from there.

Codepen Demo

* {
  box-sizing: border-box;
}
.grid {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  background: gray;
}
.grid__item {
  background: red;
  flex: 1 0 auto;
  padding: 1em;
}
.one-third {
  flex: 0 0 33.333%;
  background: green;
  border: 1px solid lightgreen;
}
<div class="grid">
  <div class="grid__item">Lorem ipsum dolor sit amet</div>
  <div class="grid__item one-third">column 33.333%</div>
  <div class="grid__item one-third">column 33.333%</div>
  <div class="grid__item">Lorem ipsum dolor sit amet,</div>
</div>
like image 112
Paulie_D Avatar answered Oct 25 '22 01:10

Paulie_D


setting flex:1 it will shrink indefinitely because it means flex-grow:1 ;flex-shrink:1

try setting flex: 1 0 auto;

Pen

like image 34
maioman Avatar answered Oct 25 '22 03:10

maioman