I would like to automatically adjust the height of my image according to the height of my text.
I tried using height: auto
and width: 100%
on my image (which seemed to make the most sense to me) but it doesn't work.
CodePen Example: https://codepen.io/JeremyWeb/pen/wvJPboW
* {
padding: 0;
margin: 0;
}
.container {
display: flex;
align-items: flex-start;
border: solid 4px red;
padding: 5px;
}
.text {
width: 50%;
border: solid 4px black;
padding: 5px;
}
.img-container {
width: 50%;
height: auto;
max-height: 100%;
box-sizing: border-box;
border: solid 4px black;
padding: 5px;
}
.img {
height: auto;
width: 100%;
object-fit: cover;
}
<div class="container">
<p class="text">Simultaneously we had a problem with prisoner drunkenness that we couldn’t figure out. I mean , the guards searched cells multiple times to no avail, that we couldn’t figure out.</p>
<div class="img-container">
<img class="img" src="https://images.unsplash.com/photo-1621166856483-1725976c4e21?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max"/>
</div>
</div>
In HTML/CSS vanilla if possible. Thank you.
Based on Haworth's method, another way would be to use a background image on an empty div and using the min-height
property.
.container {
display: flex;
}
.text {
width: 50%;
}
.img-container {
width: 50%;
min-height: 100%;
background-image: url("https://images.unsplash.com/photo-1621166856483-1725976c4e21?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max");
background-size: cover;
background-position: center;
}
<div class="container">
<p class="text">Simultaneously we had a problem with prisoner drunkenness that we couldn’t figure out. I mean , the guards searched cells multiple times to no avail, that we couldn’t figure out.</p>
<div class="img-container"></div>
</div>