I have a CSS grid layout that currently looks like this,
.image__gallery-grid {
max-height: none;
grid-template-columns: repeat(4, minmax(0, 1fr));
grid-template-rows: repeat(2, minmax(0, 1fr));
gap: 2.5rem;
display:grid;
}
.image__gallery-grid .image:nth-child(1) {
grid-column: span 1 / span 1;
grid-row: span 2 / span 2;
}
.image__gallery-grid .image:nth-child(2) {
grid-column-start: 2;
grid-row: span 1 / span 1;
}
.image__gallery-grid .image:nth-child(3) {
grid-column-start: 2;
grid-row: span 1 / span 1;
grid-row-start: 2;
}
.image__gallery-grid .image:nth-child(4) {
grid-column: span 1 / span 1;
grid-column-start: 3;
grid-row: span 2 / span 2;
}
.image__gallery-grid .image:nth-child(5) {
grid-column-start: 4;
grid-row: span 1 / span 1;
}
.image__gallery-grid .image:nth-child(6) {
grid-column-start: 4;
grid-row: span 1 / span 1;
grid-row-start: 2;
}
img {
max-width:100%;
height: auto;
}
<div class="image__gallery-grid">
<div class="image"><img src="https://placehold.co/900x900"></div>
<div class="image"><img src="https://placehold.co/900x900"></div>
<div class="image"><img src="https://placehold.co/900x900"></div>
<div class="image"><img src="https://placehold.co/900x900"></div>
<div class="image"><img src="https://placehold.co/900x900"></div>
<div class="image"><img src="https://placehold.co/900x900"></div>
</div>
The first image in the grid should span 2 rows, as should the 4th image. I can get the layout as I want, the first .image
and 4th .image
both span 2 rows, but the image just matches the width, is there a way to get the image to fill the available space? I’ve tried object-fit:cover; object-position: center
2
Answers
Try adding the following css lines after what you already have:
to go along with my comment for demonstration
snippet to run: