htmlcsscss-grid

different grid area while using grid-template-column


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. reference

my 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>


Solution

  • 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.

    .image-grid > :first-child {
      margin-top: 10em;
    }
    
    .image-grid > :last-child {
      margin-top: 3em;
    }
    

    body {
      margin: 1em;
      background: black;
    }
    
    .image-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 1rem;
      background: aliceblue;
    }
    
    .image-grid > * {
      display: flex;
      flex-direction: column;
      gap: 1em;
    }
    
    .image-grid > :first-child {
      margin-top: 10em;
      margin-bottom: 3em;
      justify-self: end;
    }
    
    .image-grid > :last-child {
      margin-top: 3em;
    }
    
    .image-grid img {
        width: 176px;
        height: 176px;
        border-radius: 8px;
    }
    <div class="image-grid">
      <div class="column">
        <img src="https://picsum.photos/id/602/300">
        <img src="https://picsum.photos/id/603/300">
      </div>
      <div class="column">
        <img src="https://picsum.photos/id/604/300">
        <img src="https://picsum.photos/id/605/300">
      </div>
    </div>