You can use display: flex
Basically, this allows you to tell child elements how much height/width it can take up.
In the snippet below, we have 3 children. We can assign a flex
to each child.
The flex for each child means how much of its parent should that particular child take up. In my example they all have a flex of 1
meaning that they each take up 1/3 of the parent's height.
Child1 - 1
Child2 - 1
Child3 - 1
However if we had something like this:
Child1 - 1
Child2 - 2
Child3 - 3
Then we have something different. In this case, Child1 is taking up 1/6 of the parent's height, Child2 is taking up 2/6 (1/3) of the parent's height, and Child3 is taking up 3/6 (1/2) of the parent's height. This is basically how flex
works.
Lastly, flex has a default direction, meaning that it automatically will fill the width (rows) based on the flex values, not the height (columns). So to tell flex to display in columns you need to use flex-direction: column;
to order/"squish" by height.
.parent {
width: 200px;
height: 180px;
display: flex;
flex-direction: column;
}
.child1 {
background: red;
width: 100%;
flex: 1; /* says to take up 1/3 of the parents height (so 60px) */
}
.child2 {
background: green;
width: 100%;
flex: 1; /* says to take up 1/3 of the parents height (so 60px) */
}
.child3 {
background: blue;
width: 100%;
flex: 1; /* says to take up 1/3 of the parents height (so 60px) */
}
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
<div class="child3"></div>
</div>