Collapsing borders using CSS Grid

21,742

Solution 1

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>

Solution 2

I just found a simple way to achieve this, using css outline instead of border.

The outline property draws a line outside the element, so, having 1px gap collapses both lines.

.container {
  display: grid;
  grid-template-columns: 100px 100px 100px;
  gap: 1px; /* you can use gap instead of grid-gap */
}

.block {
  width: 100px;
  height: 100px;
  background-color: lightgrey;
  outline: 1px solid darkgreen; /* Use outline instead of border */
}

.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>

As TylerH commented, outline does not take up space and can overlap, that is why you need to use the gap for it, if you want a 5px line, you should write 5px for both properties, the outline and the gap.

.container {
  display: grid;
  grid-template-columns: 100px 100px 100px;
  gap: 5px;
}

.block {
  width: 100px;
  height: 100px;
  background-color: lightgrey;
  outline: 5px solid darkgreen; /* The same width as the gap */
}

Solution 3

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.
Share:
21,742
Admin
Author by

Admin

Updated on July 23, 2022

Comments

  • Admin
    Admin almost 2 years

    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, or is there any way to style the gutter?

    As you can see in the snippet below, the 10px borders stack (20px total) in-between blocks.

    I understand this issue isn't unique to CSS Grids, but I'm hoping it'll allow for new solutions for creating a uniform 10px border between all boxes and on the outer edges.

    My actual use-case is a calendar I'm making to practice working with Grids and React components. You can see the issue I'm running into here:

    CSS Grid Calendar.

    Since every month is different, I'll have a lot of different edge-cases to consider.

    .container {
      display: grid;
      grid-template-columns: 120px 120px;
      box-sizing: border-box;
    }
    
    .block {
      width: 100px;
      height: 100px;
      background-color: lightgrey;
      border: 10px solid 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>

  • Admin
    Admin about 7 years
    Awesome! I'll be trying both of those for sure.
  • Admin
    Admin about 7 years
    Nice! I'm hoping to have some more control over what happens with the unfilled space. I'll definitely keep this in mind for other applications.
  • Admin
    Admin about 7 years
    Do you think these approaches could still work if I've defined my grid template with frs?
  • Admin
    Admin about 7 years
    I'm referring to the green background-color showing in the upper left and lower right.
  • G-Cyrillus
    G-Cyrillus about 7 years
    @TimFoley If you do, do not set width on children, only col number in the template edit added a snippet example
  • G-Cyrillus
    G-Cyrillus about 7 years
    @TimFoley an exemple with 2 grids side by side and 1fr for rows and columns : codepen.io/gc-nomade/pen/QvpBaO
  • Admin
    Admin about 7 years
    Just tested it out, and I think you've solved it! Thanks a ton
  • Håken Lid
    Håken Lid about 6 years
    Isn't that the same as @Michael_B's answer? Except that he uses 5px instead of 1px borders.
  • TylerH
    TylerH over 3 years
    While outline draws a line outside the element, it's important to note it does not take up space, so it doesn't cause changes to the layout. This also allows a weird 'feature' where you can have two elements side by side and their outlines can appear to 'overlap'.
  • vanowm
    vanowm almost 3 years
    I found this answer to be the simplest and most universal solution. Thank you.
  • Kip
    Kip over 2 years
    i thought this was the solution for me until i learned there's no outline-top, outline-left, etc :(