Home > Net >  make divs overlap with Grid Layout
make divs overlap with Grid Layout

Time:02-03

The goal is that when div increases, it will overlap the next row(s) and will not push them down.
I tried using position: absolute; but it destroyed the form of the Grid Layout.
It's important to me to keep the shape of the grid, it's essential.

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}

.grid-item:hover {
  background-color: #ffcccc;
  height: 90px;
}
<div >
  <div >1</div>
  <div >2</div>
  <div >3</div>
  <div >4</div>
  <div >5</div>
  <div >6</div>
  <div >7</div>
  <div >8</div>
  <div >9</div>
</div>

CodePudding user response:

In your example, you specify the height on hover. You could also specify a negative bottom margin to compensate (and use z-index to make sure it appears on top).

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}

.grid-item:hover {
  background-color: #ffcccc;
  height: 90px;
  margin-bottom: -60px;
  z-index: 100;
}
<div >
  <div >1</div>
  <div >2</div>
  <div >3</div>
  <div >4</div>
  <div >5</div>
  <div >6</div>
  <div >7</div>
  <div >8</div>
  <div >9</div>
</div>

CodePudding user response:

I would use scale and remove the height from item.

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
  transition: all .3s ease-out 0s; /* new */
}

.grid-item:hover {
  background-color: #ffcccc;
  scale: 1.7; /* new */
}
<div >
  <div >1</div>
  <div >2</div>
  <div >3</div>
  <div >4</div>
  <div >5</div>
  <div >6</div>
  <div >7</div>
  <div >8</div>
  <div >9</div>
</div>

  • Related