With following HTML
<body>
<div class="wrapper">
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
<div class="grid-item"><img src="https://images.unsplash.com/photo-1713877561507-881bf3b1c310?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" alt=""></div>
</div>
</body>
And CSS
.wrapper {
width: 90%;
margin: 200px auto;
border:solid 3px palegreen;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);
}
.grid-item img {
width: 100%;
height: 100%;
object-fit: cover;
}
How does .grid-item
s know what height to take ?
I thought .grid-item
s just won’t take any height since I didn’t specify it but now i am confused
2
Answers
By default the css grid layouts takes the height of the things that are inside it, in your paradigm it contains images so it will take the height of the images.
To be more precise The css rule
.grid-item img { height: 100%; }
tells the images to take up 100% of the height of their parent grid items, which ensures that the images fill the entire space within each grid item without distortion.Furthermore answering your second question in the comments on why Original photo has resolution of 6000×4000 pixels while computed size of each
.grid-item
is 1633.19×1092.75 pixels. If computed size was 1633.19×4000. It depends on how browsers handle images withobject-fit: cover;
. This property ensures that the image covers the entire container while maintaining its aspect ratio so it has to do some correcting before rendering it to you like that, but, even without the css ruleobject-fit: cover
I am not really sure how each browser reacts to resizing each image dynamically in order to keep the aspect ration, it’s out of the scope of my knowledge.I hope I helped you a little, if you need any more explanation please tell me.
To achieve an automatic grid item height in CSS, you can use the grid-auto-rows property along with grid-template-columns for defining the grid structure.