I’m trying to display 4 images in a seemingly irregular grid, I have added two transparent boxes with slightly smaller height than the images, one at the start of the first column and one at the end of the second column.
I will attach images of what I’m trying to achieve vs what I have done along with the code.
.image-grid {
grid-area: I;
display: grid;
grid-template-columns: repeat(2, 0.5fr);
grid-template-rows: auto auto auto;
gap: 1rem;
justify-self: start;
}
.image-grid img {
width: 176px;
height: 176px;
border-radius: 8px;
}
.transparent-box {
width: 176px;
height: 140px;
background-color: rgba(0, 0, 0, 0.00);
border-radius: 8px;
}
<div class="image-grid">
<div class="transparent-box"></div>
<img src="images/1.jpg" alt="Digital Network">
<img src="images/2.jpg" alt="Workstation">
<img src="images/3.jpg" alt="Person in Digital Environment">
<img src="images/4.jpg" alt="Circuit Pattern">
<div class="transparent-box"></div>
</div>
3
Answers
You can use a 2 column grid as you are doing but with these alterations:
remove the redundant transparent elements, instead position the first image in column 2;
even numbered images will automatically go into column 1; move them up by the size of the gap (1rem in this case) using a transform as that does not affect the layout of the grid, being only a visual change.
Use a grid with a single row. Each grid cell then contains a vertical flexbox, with top padding set differently for each column to achieve that random look.
Is that what you are trying to achieve?