OiO.lk Blog HTML Is there a way to get a css grid to auto add a col when image reaches max-width?
HTML

Is there a way to get a css grid to auto add a col when image reaches max-width?


I have a grid. Each cell will be an image with a max-width (used simple colored div in ex). Is there a way to have the grid automatically add another column when the images reach their max-width instead of just increasing the gap between them. (same for removing a column when shrink to min-width). Here is the sample code

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <style>
      .pet-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 10px;
        width: 100vw;
      }

      .pet {
        aspect-ratio: 1/1;
        background-color: cadetblue;
        max-width: 205px;
      }
    </style>
  </head>

  <body>
    <header></header>

    <main>
      <div class="pet-grid">
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
        <div class="pet"></div>
      </div>
    </main>

    <footer></footer>
  </body>
</html>



You need to sign in to view this answers

Exit mobile version