I'm trying to allow an image to take the height and width of its parent. Its parent is a grid item(div1) that has a height 100% of the container.
What I've expected and wanted:
- div1 will take the height of the parent(container) means div1 height should be 500px and a width of 50% of the container.
- The image should take the width and height of its parent(div1). Image should have a height of 500px and width 100% of its parent.
What the result shows:
- image took a height of 530px and the div1 took a height of 530px too.
I'm confused with this behavior. What I might be doing wrong?
My code:
.container {
height: 500px;
max-height: 500px;
display: grid;
grid-template-columns: 50% 50%;
}
.container .div1 {
height: 100%;
}
.container .div1 img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="container">
<div class="div1">
<img src="https://via.placeholder.com/800" />
</div>
<div>
<h1>heading</h1>
</div>
</div>