Add space before and after first and last grid items

前端 未结 2 811
攒了一身酷
攒了一身酷 2021-01-21 14:34

My question is identical to this question, but the given solution does not work.

Here is a codepen of what I\'m working with.

I\'ve tried two different approache

2条回答
  •  故里飘歌
    2021-01-21 14:53

    One idea to change the width of before is to define a column template like below that will force the width of the first element only then the other will follow the grid-auto-columns. Basically we define an explicit grid with 1 column then the browser will add more column as needed to create the implicit grid:

    :root {
      --gap: 25px;
    }
    
    body {
      width: 100vw;
      overflow-x: hidden;
      margin: 0
    }
    
    #c {
      width: 100%;
      height: 50px;
      overflow-x: auto;
      display: grid;
      grid-gap: 20px;
      grid-template-columns:1px;
      grid-auto-flow: column;
      grid-auto-columns: calc(calc(100% - calc(var(--gap) * 2)) / 1.5);
      border: solid red 1px;
    }
    
    /* second approach */
    #c::before {
      content: '';
    }
    
    #c::after {
      content: '';
      width: 1px; /* works out to about 25px or var(--gap) */
    }
    
    .i {
      /*width: 100%; not needed*/
      height: 25px;
      /*display: inline-block; not needed*/
    }
    
    .i:nth-child(odd) {
      background: skyblue;
    }
    
    .i:nth-child(even) {
      background: pink;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

提交回复
热议问题