Collapsing borders using CSS Grid
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>
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 */
}
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:
-
auto-fit
: Fill in as many columns as can fit on the row. Overflow columns will wrap. -
minmax()
: Each column will be a minimum width of 120px and maximum width of whatever free space is available. Thefr
unit is comparable to flex layout'sflex-grow
property. -
grid-auto-rows
: Automatically created rows (implicit rows) will be 100px in height. -
grid-gap
: 5px gutters all around. Shorthand forgrid-column-gap
andgrid-row-gap
.