Collapsing borders using CSS Grid

前端 未结 3 1226
谎友^
谎友^ 2020-12-29 03:04

I\'m having fun getting my head around the new CSS Grid spec, but I\'m running into trouble with borders.

Is it possible to collapse borders in a CSS Grid,

相关标签:
3条回答
  • 2020-12-29 03:43

    Consider controlling all sizing and spacing at the grid container level, not at the grid item level. Remove the borders and sizing applied to the items.

    .container {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); /* 1 */ /* 2 */
      grid-auto-rows: 100px; /* 3 */
      grid-gap: 5px; /* 4 */
      padding: 5px;
      background-color: tomato;
    }
    
    .block {
      background-color: lightgrey;
    }
    
    /* for demo only */
    .block:nth-child(-n + 2) {
      visibility: hidden;
    }
    <div class='container'>
      <div class='block'>0</div>
      <div class='block'>0</div>
      <div class='block'>1</div>
      <div class='block'>2</div>
      <div class='block'>3</div>
      <div class='block'>4</div>
      <div class='block'>5</div>
      <div class='block'>6</div>
      <div class='block'>7</div>
      <div class='block'>8</div>
      <div class='block'>9</div>
      <div class='block'>10</div>
      <div class='block'>11</div>
      <div class='block'>12</div>
      <div class='block'>13</div>
      <div class='block'>14</div>
      <div class='block'>15</div>
      <div class='block'>16</div>
      <div class='block'>17</div>
      <div class='block'>18</div>
      <div class='block'>19</div>
      <div class='block'>20</div>
      <div class='block'>21</div>
      <div class='block'>22</div>
      <div class='block'>23</div>
      <div class='block'>24</div>
      <div class='block'>25</div>
      <div class='block'>26</div>
      <div class='block'>27</div>
      <div class='block'>28</div>
      <div class='block'>29</div>
      <div class='block'>30</div>
      <div class='block'>31</div>  
    </div>

    jsFiddle demo

    Notes:

    1. auto-fit: Fill in as many columns as can fit on the row. Overflow columns will wrap.
    2. minmax(): Each column will be a minimum width of 120px and maximum width of whatever free space is available. The fr unit is comparable to flex layout's flex-grow property.
    3. grid-auto-rows: Automatically created rows (implicit rows) will be 100px in height.
    4. grid-gap: 5px gutters all around. Shorthand for grid-column-gap and grid-row-gap.
    0 讨论(0)
  • 2020-12-29 03:51

    You may use grid-gap and box-shadow:

    .container {
      display: grid;
      grid-template-columns: 100px 100px;
      box-sizing: border-box;
      grid-gap:10px;
    }
    
    .block {
      width: 100px;
      height: 100px;
      background-color: lightgrey;
     box-shadow:0 0 0 10px palegreen;
    }
    
    .first {
      grid-column: 2 / span 1;
    }
    <div class='container'>
      <div class='block first'>1</div>
      <div class='block'>2</div>
      <div class='block'>3</div>
    </div>

    Or combine row and columns template setting:

    .container {
      display: grid;
      grid-template-columns: 110px 110px;
      grid-template-rows:110px;
      box-sizing: border-box;
      
    }
    
    .block {
      width: 100px;
      height: 100px;
      background-color: lightgrey;
     border:solid 10px palegreen;
    }
    
    .first {
      grid-column: 2 / span 1;
    }
    <div class='container'>
      <div class='block first'>1</div>
      <div class='block'>2</div>
      <div class='block'>3</div>
    </div>

    Note that columns and rows of 120px will show both sides borders when box is set to 100px...

    If fr value is used for columns, then do not set width on boxes (rows would follow same restriction).

    .container {
      display: grid;
      grid-template-columns: repeat(7, 1fr);
      grid-template-rows: 110px;
      /*whatever else */
      box-sizing: border-box;
    }
    
    .block {
      margin: 0 -10px 0 0;/* fixed width value missing */
      height: 100px;
      background-color: lightgrey;
      border: solid 10px palegreen;
    }
    
    .first {
      grid-column: 2 / span 1;
    }
    <div class='container'>
      <div class='block first'>1</div>
      <div class='block'>2</div>
      <div class='block'>3</div>
      <div class='block'>4</div>
      <div class='block'>5</div>
      <div class='block'>6</div>
      <div class='block'>7</div>
    </div>

    0 讨论(0)
  • 2020-12-29 03:53

    Another approach you could take if you were ok with the gap border color being the same as the day cells that don't fall on the current month is to wrap a div around the entire grid container and set its background-color to the color you want your borders to be and give it 1px of padding with a grid-gap of 1px. With this approach you're able to achieve a uniformly bordered grid without the complexity of using box-shadow, which feels like a hack to me.

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