Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline flex container width not growing

Tags:

html

css

flexbox

Consider the following layout:

<div class="div">
  <span class="span1">test</span>
  <span class="span2">test test test test test</span>
</div>

and the css:

.div{
   display:inline-flex;
   background-color:lightgray;
}
.span1{
   flex:0 0 100px;
}
.span2{
   white-space:nowrap;
}

Why isn't the div stretched wide enough to cover the two spans? This happens in FF and Chrome. In IE 11/Edge it works (as I would expect it to work) Here's the fiddle https://jsfiddle.net/p18h0jxt/

PS: It works everywhere if I used the following style:

.span1{
   flex:0 0 auto;
   width:100px;
}

Thanks.

like image 440
Dr Sitecore Avatar asked Oct 23 '25 22:10

Dr Sitecore


1 Answers

From this SO answer:

Bug affecting all major browsers, except IE 11 & Edge:

Just as you said - apparently flex-basis is not respected in a nested flex container.

So your 100px flex-basis from flex: 0 0 100px; can't work properly (except ironically in IE 11 & Edge).

The workaround (also mentioned here) is to use width instead of flex-basis like so:

.div {
  display: inline-flex;
  background-color: lightgray;
}

.span1 {
  width: 100px;
}

.span2 {
  white-space: nowrap;
}
<div class="div">
  <span class="span1">test</span>
  <span class="span2">test test test test test</span>
</div>

You could use flex instead of inline-flex, but then your div will be rendered like a block element i.e. it will take up the full width that's available rather than being confined to your content.

I assume you are using inline-flex so that the background remains confined to the content.

like image 110
Dan Kreiger Avatar answered Oct 26 '25 14:10

Dan Kreiger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!