If .right
item stretch height to any size, how to make .item
elements inside .left
to fill vertical space equally?
From:
To:
.container {
display: flex;
background: #ccc;
}
.left {
flex: 2;
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: stretch;
height: 100%;
}
.right {
flex: 1;
border: 1px solid #000;
}
.item {
border: 1px solid #000;
flex: 1 0 100%;
align-self: stretch;
}
<div class="container">
<div class="left">
<div class="item">
Item 1
</div>
<div class="item">
Item 2
</div>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet.
</div>
</div>
https://jsfiddle.net/d7o58kaj/1/
An initial setting of a flex container is align-content: stretch
.
That means that flex lines will equally share all available space in the container along the cross axis. (A similar effect to flex: 1
on all items on the main axis.)
However, when you define a height for a flex item when the cross axis is vertical, or width, when the cross axis is horizontal, you override the align-content
default.
In your row-direction container, the cross axis is vertical. So if you remove height: 100%
, that allows align-content: stretch
to work.
Learn more about flex alignment on the cross axis here:
Learn more about flex alignment on the main axis here:
.container {
display: flex;
background: #ccc;
}
.left {
flex: 2;
display: flex;
flex-wrap: wrap; /* <--- this brings align-content into play */
/* flex-direction: row; <--- default setting; can be omitted */
/* align-items: stretch; <--- default setting; can be omitted */
/* height: 100%; */
}
.right {
flex: 1;
border: 1px solid #000;
}
.item {
border: 1px solid #000;
flex: 1 0 100%;
align-self: stretch;
}
<div class="container">
<div class="left">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet.
</div>
</div>
Removed height: 100%
from .left
.container {
display: flex;
background: #ccc;
}
.left {
flex: 2;
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: stretch;
}
.right {
flex: 1;
border: 1px solid #000;
}
.item {
border: 1px solid #000;
flex: 1 0 100%;
align-self: stretch;
}
<div class="container">
<div class="left">
<div class="item">
Item 1
</div>
<div class="item">
Item 2
</div>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet.
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With