Add space before and after first and last grid items

前端 未结 2 806
攒了一身酷
攒了一身酷 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;
    }
    <div id='c'>
      <div class='i'>1</div>
      <div class='i'>2</div>
      <div class='i'>3</div>
      <div class='i'>4</div>
      <div class='i'>5</div>
      <div class='i'>6</div>
      <div class='i'>7</div>
      <div class='i'>8</div>
      <div class='i'>9</div>
      <div class='i'>10</div>
      <div class='i'>11</div>
      <div class='i'>12</div>
      <div class='i'>13</div>
      <div class='i'>14</div>
      <div class='i'>15</div>
      <div class='i'>16</div>
      <div class='i'>17</div>
      <div class='i'>18</div>
      <div class='i'>19</div>
      <div class='i'>20</div>
      <div class='i'>21</div>
      <div class='i'>22</div>
      <div class='i'>23</div>
      <div class='i'>24</div>
    </div>

    0 讨论(0)
  • 2021-01-21 14:57

    This is another one of those instances where flexbox may provide a simpler, easier and more effective solution that grid.

    :root {
      --gap: 25px;
    }
    
    #c {
      display: flex;
      overflow-x: auto;
      height: 50px;
      border: solid red 1px;
    }
    
    .i {
      height: 25px;
      flex: 0 0 calc(calc(100% - calc(var(--gap) * 2)) / 1.5); /* fg, fs, fb */
    }
    
    #c::before {
      content: '';
      flex: 0 0 var(--gap);
    }
    
    .i {
      margin-right: 20px;
    }
    
    #c::after {
      content: '';
      flex: 0 0 calc(var(--gap) - 20px); /* gap less margin */
    }
    
    .i:nth-child(odd)  { background: skyblue; }
    .i:nth-child(even) { background: pink; }
    body               { margin: 0; }
    *                  { box-sizing: border-box; }
    <div id='c'>
      <div class='i'>1</div>
      <div class='i'>2</div>
      <div class='i'>3</div>
      <div class='i'>4</div>
      <div class='i'>5</div>
      <div class='i'>6</div>
      <div class='i'>7</div>
      <div class='i'>8</div>
      <div class='i'>9</div>
      <div class='i'>10</div>
      <div class='i'>11</div>
      <div class='i'>12</div>
      <div class='i'>13</div>
      <div class='i'>14</div>
      <div class='i'>15</div>
      <div class='i'>16</div>
      <div class='i'>17</div>
      <div class='i'>18</div>
      <div class='i'>19</div>
      <div class='i'>20</div>
      <div class='i'>21</div>
      <div class='i'>22</div>
      <div class='i'>23</div>
      <div class='i'>24</div>
    </div>

    You may also want to consider a transparent border for start- and end-side spacing. Last margin / padding collapsing in flexbox / grid layout

    0 讨论(0)
提交回复
热议问题