0

I'm trying to create grid which has collapsible items inside. When I shrink one item then we see whitespace between this item and item on the bottom of it. I need bottom item to go up when top item shrinks.

const gridItems = document.querySelectorAll('.grid-item');

gridItems.forEach(item => {
  item.addEventListener('click', () => {
    item.classList.toggle('small');
  });
});
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 10px;
}

.grid-item {
  background-color: #3498db;
  color: white;
  text-align: center;
  padding: 20px;
  font-size: 1.5rem;
  height: 150px;
  transition: all 0.3s ease;
  /* Smooth transition animation */
}

.grid-item.small {
  height: 50px;
}

.grid-item:nth-child(2n) {
  background-color: #e74c3c;
}

.grid-item:nth-child(3n) {
  background-color: #2ecc71;
}

.grid-item:nth-child(4n) {
  background-color: #f39c12;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>
  <div class="grid-container">
    <div class="grid-item">1</div>
    <div class="grid-item">2</div>
    <div class="grid-item">3</div>
    <div class="grid-item">4</div>
    <div class="grid-item">5</div>
    <div class="grid-item">6</div>
    <div class="grid-item">7</div>
    <div class="grid-item">8</div>
  </div>
</body>

</html>

So to sum up, I need grid row to be same height as its content to avoid empty spaces when i shrink grid item.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Shotiko Topchishvili
  • 2,663
  • 10
  • 20

0 Answers0