Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make flex child height to wrap content [duplicate]

Tags:

html

css

flexbox

What I basically want is to make each child element's height 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 output:
enter image description here

like image 936
dsharew Avatar asked Jul 25 '17 12:07

dsharew


1 Answers

You just need to set align-items: flex-start on parent element because default value is 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>
like image 123
Nenad Vracar Avatar answered Oct 29 '22 01:10

Nenad Vracar