I have a flexbox of two divs: the left one is a container for some text, and the right one is a container for an image. I am using a flexbox because I want the text div to take up 2/3 of the width, and the image to take up 1/3rd, regardless of the parent width.
<div id="parent">
<div id="txtContainer">
<p>This is some text! It will have some height.</p>
<p>I'm adding multiple lines to show that.</p>
<p>I hope you're having a wonderful day, whoever you are :)</p>
</div>
<div id="imgContainer">
<img id="img" src="https://cdn.pixabay.com/photo/2021/02/17/23/02/tiger-6025561_960_720.jpg">
</div>
</div>
#parent {
display: flex;
width: 100%;
}
#txtContainer {
background-color: lime;
flex-basis: 0;
flex-grow: 2;
}
#imgContainer {
background-color: red;
flex-basis: 0;
flex-grow: 1;
}
#img {
object-fit: contain;
width: 100%;
height: 100%;
}
I would like for both the text container and the overall parent to have a height that will fit the text exactly, without any extra space at the bottom. This means the height of everything, including the image container, would be determined by the height of the text. I would like for the image to fill as much of its container as possible without extending past the height of the text.
Visually, instead of this:
I want this:
Here's a demo of how it works now: https://jsbin.com/hoqufutuya/1/edit?html,css,output
I have tried setting height: 100%
on everything involved. I've also tried some different position
s and margin
s, all to no avail. Unfortunately I'm not very good with CSS, so I'm stuck. I was not able to find help on google or SO.
Thank you for your time.