Search code examples
cssgrid-layoutcss-grid

Overflow Absolute element is not visible inside grid item


I have 1 x 6 grid. I want to show the resize handle when hovering grid item. For that, i have 2 rounded absolutely positioned div inside the grid item. I placed in the middle of the edge on both sides of an item. I want those 2 handles to be placed half inside and a half outside of the gird item edge. Here is the code snippet. I need that overflow: auto for grid item to prevent grid item to extend to accommodate its content.

Thanks in advance.

.wrapper{
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-auto-rows: 150px;
  grid-gap: 10px;
}

.item{
  position: relative;
  background: lightyellow;
  border: 2px dashed transparent;
  overflow: auto;
}

.item:hover{
  border-color: #333;
  padding: 10px;
}

.round{
  position: absolute;
  height: 15px;
  width: 15px;
  border-radius: 50px;
  border: 1px solid #333;
  top: calc(50% - 7.5px);
  background: lightgreen;
}

.round:nth-child(1) {
    left: -7.5px !important;
}

.round:nth-child(2){
  right: -7.5px;
}
<div class="wrapper">
  <div class="item span2">
    <div class="round"></div>
    <div class="round"></div>
    <p>This is the text of the box. This box will grow to fit the content if i remove the overflow. But i dont want this behaviour. So i am setting overflow to auto. But i want that two rounded green box to fit on half inside and half outside of box. Now it is another half is not visible. </p>
  </div>
  <div class="item"></div>
  <div class="item"></div>
</div>


Solution

  • You can use fixed position with a null transform trick:

    .wrapper{
      display: grid;
      grid-template-columns: repeat(6, 1fr);
      grid-auto-rows: 150px;
      grid-gap: 10px;
      transform:translate(0,0);
    }
    
    .item{
      position: relative;
      background: lightyellow;
      border: 2px dashed transparent;
      overflow: auto;
    }
    
    .item:hover{
      border-color: #333;
      padding: 10px;
    }
    
    .round{
      position: fixed;
      height: 15px;
      width: 15px;
      border-radius: 50px;
      border: 1px solid #333;
      top: calc(50% - 7.5px);
      background: lightgreen;
    }
    
    .round:nth-child(1) {
        left: -7.5px !important;
    }
    
    .round:nth-child(2){
      left: calc((100% / 6) - 17.5px);
    }
    <div class="wrapper">
      <div class="item span2">
        <div class="round"></div>
        <div class="round"></div>
        <p>This is the text of the box. This box will grow to fit the content if i remove the overflow. But i dont want this behaviour. So i am setting overflow to auto. But i want that two rounded green box to fit on half inside and half outside of box. Now it is another half is not visible. </p>
      </div>
      <div class="item"></div>
      <div class="item"></div>
    </div>