Make a wrapper-element and use grid-template-rows: 1fr 1fr
and grid-template-columns: 1fr 1fr 1fr
, which is a 3-column and 2-row layout. Optionally you could just set their value to auto
, as you are already using span
in the grid to make the layout (read further for clarification).
.wrapper {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
Make sure each img
is wrapped into a container for better layout control. Not necessary, but preferable in my opinion. Add flex
to the img
-container, and then apply object-fit: cover
, so the image won't stretch. This will lead to a bit of cropping of the image, but it won't be stretched out - also preferable in my opinion, but not necessary.
Also, make sure to apply max-width: 100%
and height: auto
on the img
-element, so the image is automatically responsive and doesn't overflow from the container.
.item {
border: 1px solid black;
display: flex;
}
.item img {
object-fit: cover;
max-width: 100%;
height: auto;
}
For the element that needs a height of 2 rows, you can use grid-span
.
To set the element as the last column (column 3), use grid-column: 3/3
. Use grid-row: 1 / span 2
to make the item go into row 1 of the layout and span out to the second row.
.item.large {
grid-column: 3 / 3;
grid-row: 1 / span 2;
}
To make the whole thing responsive, just modify the values on the .item.large
-element to auto, while setting your preferred amount of columns and rows inside a media-query
:
@media only screen and (max-width: 768px) {
.wrapper {
display: grid;
grid-template-rows: auto;
grid-template-columns: 1fr;
}
.item.large {
grid-column: auto;
grid-row: auto;
}
}
.wrapper {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
.item {
border: 1px solid black;
display: flex;
}
.item img {
object-fit: cover;
max-width: 100%;
height: auto;
}
.item.large {
grid-column: 3 / 3;
grid-row: 1 / span 2;
}
@media only screen and (max-width: 768px) {
.wrapper {
display: grid;
grid-template-rows: auto;
grid-template-columns: 1fr;
}
.item.large {
grid-column: auto;
grid-row: auto;
}
.item img {
width: 100%;
}
}
<div class="wrapper">
<div class="item"><img src="https://www.fillmurray.com/640/360"></div>
<div class="item"><img src="https://www.fillmurray.com/640/360"></div>
<div class="item large"><img src="https://www.fillmurray.com/640/360"></div>
<div class="item"><img src="https://www.fillmurray.com/640/360"></div>
<div class="item"><img src="https://www.fillmurray.com/640/360"></div>
</div>