5

I'm trying to make width of each item based on the content but what I have so far set the width all the way to available spaces. flex: 0 0 auto does not seem to do the trick. What am I doing wrong?

Goal is have a width based on the content size.

[Hello]
[Hello world]

Currently

[Hello                   ]
[Hello world             ]

https://jsfiddle.net/v6cgLjbd/8/

<span class='box'>
  <span class='item'>Hello</span>
  <span class='item'>Hello World</span>
  <span class='item'>Hello lOOOOONG text</span>
</span>

.box {
  height: 200px;
  width: auto;
  border: 1px solid red;
  display: flex;
  flex-direction: column;
}

.item {
  background-color: gray;
  flex: 0 0 auto;
  width: auto;
}
Asons
  • 84,923
  • 12
  • 110
  • 165
Meow
  • 18,371
  • 52
  • 136
  • 180

1 Answers1

5

You need to add align-items: flex-start on flex-container. When you use flex-direction: column on parent element, with flex property on child elements you control height not width.

.box {
  height: 200px;
  border: 1px solid red;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.item {
  background-color: gray;
}
<span class='box'>
  <span class='item'>Hello</span>
  <span class='item'>Hello World</span>
  <span class='item'>Hello lOOOOONG text</span>
</span>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176