Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink flexbox container to content size [duplicate]

Tags:

html

css

flexbox

If you are using a Flexbox layout to layout some items in a row, the Flexbox container takes up the full width of whatever container it is in. How do you set the Flexbox container to only take up the width of it's child elements?

Take a look at the following example:

https://jsfiddle.net/5fr2ay9q/

In this example the size of the flexbox container extends beyond the child element's width. The typical way to fix something like this is display: inline but obviously that won't work because then it's not a flexbox container anymore.

Is it possible to do this?

.container {
  display: flex;
  flex-direction: row;
  outline: 1px solid red;
}

p {
  outline: 1px solid blue;
}
Can you make the flex container not 100% width but rather the width of the content itself?

<div class='container'>
  <img src='https://placehold.it/300x300'/>
  <p>
    This is some content hello world testing 123.
  </p>
</div>
like image 232
Jake Wilson Avatar asked Jul 11 '17 19:07

Jake Wilson


1 Answers

You can use display: inline-flex; (see Designating a Flexible Box):

.container {
  display: inline-flex;
  flex-direction: row;
  outline: 1px solid red;
}

p {
  outline: 1px solid blue;
}
Can you make the flex container not 100% width but rather the width of the content itself?

<div class='container'>
  <img src='https://placehold.it/300x300'/>
  <p>
    This is some content hello world testing 123.
  </p>
</div>
like image 117
2 revs Avatar answered Sep 21 '22 00:09

2 revs