How do I set a flex-container to be the width of its flex-items?

CssFlexbox

Css Problem Overview


I have a flex container with justify-content: flex-start. There ends up being overflow on the right side due to the flex items taking less space than the size of the container.

Aside from setting an explicit width on the flex-container, is there a way of just having the equivalent of width: auto to remove the overflow?

Css Solutions


Solution 1 - Css

If you mean you want the container to be the width of the items, rather than the items expanding to the container width...

You may be able to get that by changing display:flex to display:inline-flex

Solution 2 - Css

  1. width: min-content (CSS3 "intrinsic" as opposed to "extrinsic")

  2. display: inline-flex if you don't care whether it's inline

If you use width: min-content on a paragraph, the longest word determines the width.

.flex-container {
  display: flex;
}

.flex-inline-container {
  display: inline-flex;
}

.width-min-content {
  width: min-content;
}

.row-direction {
  flex-direction: row;
  border: 0.0625rem solid #3cf;
 }
.col-direction {
  flex-direction: column;
  border: 0.0625rem solid magenta;
}

flex
<div class='flex-container row-direction'>
  <input />
  <input type='datetime-local'>
</div>
<div class='flex-container col-direction'>
  <input />
  <input type='datetime-local'>
</div>
<br>
flex width: min-content
<div class='flex-container width-min-content row-direction'>
  <input />
  <input type='datetime-local'>
</div>
<div class='flex-container width-min-content col-direction'>
  <input />
  <input type='datetime-local'>
</div>
<br>
inline-flex
<div class='flex-inline-container row-direction'>
  <input />
  <input type='datetime-local'>
</div>

<div class='flex-inline-container col-direction'>
  <input />
  <input type='datetime-local'>
</div>

Solution 3 - Css

This one worked for me on the same inline-flex (or flex) element:

   width: fit-content;

Solution 4 - Css

As suggested by Mahks, you can make the flex container inline. But if laying it inline is not suitable for your layout, an alternative is to float the flex container (not the flex items).

Floats will shrink-wrap to fit their contents, and this is no different for a block-level flex container. An inline-level flex container behaves similarly to an inline-block box when laid out in its parent inline formatting context, which means it will also shrink to fit its contents by default.

Solution 5 - Css

not sure about your question, but:

DEMO: http://jsfiddle.net/jn45P/

you just need to enable the flexibility on the flex items, using flex-grow:1; to fill up the space

<div class="o">
    <div>a</div><div>a</div><div>a</div><div>a</div><div>a</div>
</div>
    
<div class="o flex">
    <div>a</div><div>a</div><div>a</div><div>a</div><div>a</div>
</div>

div.o
{
    border:2px red solid;
    padding:2px;
    width:500px;
    flex-direction:row;
    display:flex;
    justify-content:flex-start;
}

div.o > div
    {border:2px red solid;margin:2px;}

div.o.flex > div
    {flex:1 1 auto;} /* enable flexibility on the flex-items */

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBOverflowView Question on Stackoverflow
Solution 1 - CssMahksView Answer on Stackoverflow
Solution 2 - CssneaumusicView Answer on Stackoverflow
Solution 3 - CssCorey BoView Answer on Stackoverflow
Solution 4 - CssBoltClockView Answer on Stackoverflow
Solution 5 - Cssuser652649View Answer on Stackoverflow