How to create flexible child length to wrap content

What I basically want is to make each child the height of the element in order to wrap its content.

Here is my code:

<style>

.parent{
        display: flex;
    }

.child{

    padding: 5px;
    margin: 10px;
    background: green;
    height: auto;
}   

</style>

<div class="parent">

    <div class="child">child1</div>
    <div class="child">child2</div>
    <div class="child">child3</div>
    <div class="child" style="height:50px">child1</div>

</div>

      

Output:
enter image description here

Expected Result:
enter image description here

+3


source to share


1 answer


You just need to set align-items: flex-start

to the parent, because that's the default stretch

.



.parent {
  display: flex;
  align-items: flex-start;
}

.child {
  padding: 5px;
  margin: 10px;
  background: green;
  height: auto;
}
      

<div class="parent">
  <div class="child">child1</div>
  <div class="child">child2</div>
  <div class="child">child3</div>
  <div class="child" style="height:50px">child1</div>
</div>
      

Run code


+7


source







All Articles