csscss-grid

How to forward scrollbar from to column in css grid


I have a css grid that is filled line by line, if the content of the first cell of any row goes beyond the container, it is necessary for a scroll to appear common to the entire first column

P.S. I can’t rewrite the grid using grid-template-rows and fill it by columns

My sample code

<!DOCTYPE html>
<html lang="ru">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .container {
            display: grid;
            grid-template-columns: minmax(0, 1fr) 50px 50px 50px;
            gap: 10px;
        }
        .item {
            overflow: auto;
            text-wrap: nowrap;
        }
    </style>
  </head>
  <body>
    <div class="container">
        <div class="item">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
        </div>
        <div class="item">
            item 2
        </div>
        <div class="item">
            item 3
        </div>
        <div class="item">
            item 4
        </div>
        <div class="item">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
        </div>
        <div class="item">
            item 6
        </div>
        <div class="item">
            item 7
        </div>
        <div class="item">
            item 8
        </div>
    </div>
  </body>
</html>

From this code I received: (https://i.sstatic.net/65qeY8uB.png) But I need a scrollbar common to all cells in first column instead only on first cell

P.S. Sorry for gramma mistakes, English is not my first language


Solution

  • You can use sticky positioning to achieve this. You’ll need to make all grid items sticky other than those in the the first column. And you can’t use gaps between them, because the scrolling content will appear through those gaps and it will look messy. So you need to use padding instead. I have increased the size of columns 3 and 4 to 60px allow for 5px of padding in the grid items, and column 2 to 65px to allow for 10px of left padding. To make the sticky positioning work, items in column 4 must be styled with right: 0, items in column 3 with right: 60px and items in column 2 with right: 120px.

    .container {
      display: grid;
      grid-template-columns: max-content 65px 60px 60px;
      overflow: auto;
      padding-bottom: 1em;
    }
    
    .item {
      text-wrap: nowrap;
      padding: 5px;
    }
    
    /* selects grid items except 1, 5, 9, 13 ... so all items NOT in the first column */
    .item:not(:nth-child(4n+1)) {
      background: white;
      color: red;
      position: sticky;
    }
    
    /* selects grid items 2, 6, 10, 14 ... so items in the second column */
    .item:nth-child(4n+2) {
      padding-left: 10px;
      right: 120px;
    }
    
    /* selects grid items 3, 7, 11, 15 ... so items in the third column */
    .item:nth-child(4n+3) {
      right: 60px;
    }
    
    /* selects grid items 4, 8, 12, 16 ... so items in the fourth column */
    .item:nth-child(4n+4) {
      right: 0;
    }
    <div class="container">
      <div class="item">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </div>
      <div class="item">
        item 2
      </div>
      <div class="item">
        item 3
      </div>
      <div class="item">
        item 4
      </div>
      <div class="item">
        Cras volutpat velit non mi sagittis condimentum. Cras tempor aliquet turpis sed pretium. Nunc aliquet sodales turpis quis ultrices. Duis auctor accumsan enim, quis maximus ex malesuada a. Donec a felis ut erat tempus euismod non vel neque. Proin lectus massa, sagittis at imperdiet nec, consequat ut neque. Sed vel placerat neque, vel varius urna. Vivamus interdum euismod urna a accumsan. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
      </div>
      <div class="item">
        item 6
      </div>
      <div class="item">
        item 7
      </div>
      <div class="item">
        item 8
      </div>
    </div>