Is there a way to ensure that the height of my images always matches their width?
I need the height of the images to dynamically adjust and match the width, even when the window is resized.
For example, if the image width is 500px, the height should also be 500px. And if the width changes to 600px after resizing, the height should adapt accordingly.
One important constraint: Do not set a fixed width for the grid or images.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
}
img {
width: 100%;
object-fit: cover;
}
<div class="grid">
<img src="https://vietadang.github.io/the-stars/dist/images/menu/dessert-1.jpg"/>
<img src="https://vietadang.github.io/the-stars/dist/images/menu/dessert-2.jpg"/>
<img src="https://vietadang.github.io/the-stars/dist/images/menu/dessert-3.jpg"/>
<img src="https://vietadang.github.io/the-stars/dist/images/menu/dessert-4.jpg"/>
</div>