Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flex container expand and overflow its container

Tags:

html

css

flexbox

How do I make a flex parent with a nowrap flex-wrap expand to fit its content even if that means overflowing whatever is wrapping the parent?

Basically, the content has a min-width, I want the flex parent not to shrink more than the space all the flex items need.

Here is a JSFiddle https://jsfiddle.net/lazamar/odat477r/

.wrapper {
  background-color: yellowgreen;
  display: block;
  padding: 10px;
  max-width: 180px;
}

.parent {
  display: flex;
  flex-flow: row nowrap;
  background-color: yellow;
}

.child {
  display: block;
  background-color: orange;
  margin: 10px;
  min-width: 50px;
}
<div class="wrapper">
  <div class="parent">
    <div class="child">Content</div>
    <div class="child">Content</div>
    <div class="child">Content</div>
    <div class="child">Content</div>
    <div class="child">Content</div>
  </div>
</div>
like image 878
Marcelo Lazaroni Avatar asked Apr 25 '16 12:04

Marcelo Lazaroni


People also ask

Why are my flex items overflowing?

The initial value of the flex-wrap property is nowrap . This means that if you have a set of flex items that are too wide for their container, they will overflow it.

How do you make your flex-grow?

The flex-grow property specifies how much the item will grow relative to the rest of the flexible items inside the same container. Note: If the element is not a flexible item, the flex-grow property has no effect.


1 Answers

The answer is what t1m0n said. Use display: inline-flex instead of display: flex for the parent.

.wrapper {
  background-color: yellowgreen;
  display: block;
  padding: 10px;
  max-width: 180px;
}
.parent {
  display: inline-flex; /* -- only change --*/
  flex-flow: row nowrap;
  background-color: yellow;
}
.child {
  display: block;
  background-color: orange;
  margin: 10px;
  min-width: 50px;
}
<div class="wrapper">
  <div class="parent">
    <div class="child">Content</div>
    <div class="child">Content</div>
    <div class="child">Content</div>
    <div class="child">Content</div>
    <div class="child">Content</div>
  </div>
</div>
like image 126
Marcelo Lazaroni Avatar answered Nov 15 '22 03:11

Marcelo Lazaroni