Search code examples
htmlcsscss-grid

How to offset a div in a css grid


It's possible to "offset" a div in a css grid as in the image ?image


Solution

  • Consider negative margin:

    .container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(3, 1fr);
      width: 500px;
      height: 200px;
      border: 1px solid;
    }
    
    .box {
      grid-column: 2/3;
      margin-left: -20px;
      margin-right: -80px;
      background: red;
    }
    .box-alt {
      grid-column: 2/3;
      grid-row:2;
      background: blue;
    }
    <div class="container">
      <div class="box"></div>
      <div class="box-alt"></div>
    </div>