1

Given the following code:

.outer-container {
  display: flex;
  justify-content: space-evenly;
}

.left-container {
  height: 300px;
}

.right-container {
  flex: 0 1 auto;
}
<div class='outer-container'>
  <div class='left-container'>Lefto</div>
  <div class='right-container'>Righto</div>
</div>

The right container will grow to be 300px high, despite the flex-grow property being set to 0. How do I prevent this from happening? I.E., I want the right container to only be as tall as its content.

Codepen: https://codepen.io/MaxMillington2/pen/PxOwxo

ksav
  • 20,015
  • 6
  • 46
  • 66
Max Millington
  • 4,378
  • 4
  • 27
  • 34

1 Answers1

1

Add align-self: flex-start on the .right-container

.outer-container {
  display: flex;
  justify-content: space-evenly;
}

.left-container {
  height: 300px;
  border: 1px solid blue;
}

.right-container {
  align-self: flex-start;
  border: 1px solid red;
}
<div class='outer-container'>
  <div class='left-container'>Lefto</div>
  <div class='right-container'>Righto</div>
</div>

Note: flex-grow, flex-shrink and flex-basis are for controlling how the space is filled along the main axis. In this case, the main axis is left-right because the flex-direction is set to row by default.

The align-items property will align the items on the cross axis. The initial value for this property is stretch and this is why flex items stretch to the height of the tallest one by default.

MDN - Concepts of Flexbox

That is why you need to override either align-items on your .outer-container or align-self to the flex children.

ksav
  • 20,015
  • 6
  • 46
  • 66