Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsing borders using CSS Grid

Tags:

html

css

css-grid

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>
like image 795
Tim Foley Avatar asked Apr 28 '17 18:04

Tim Foley


People also ask

What is the use of border-collapse in CSS?

The border-collapse property in CSS is used to set the borders of the cell present inside the table and tells whether these cells will share a common border or not. Syntax: border-collapse: separate|collapse|initial|inherit; Default Value : Its default value is separate.

Which CSS property can be used for collapsing the border between table cells?

Choose the CSS property that can be used for collapsing the borders between table cells? Explanation: The border-collapse property sets whether the table borders are collapsed into a single border or detached as in standard HTML.

Which attribute is used to collapse cell borders?

The border-collapse CSS property sets whether cells inside a <table> have shared or separate borders.


2 Answers

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>
like image 181
G-Cyrillus Avatar answered Oct 04 '22 02:10

G-Cyrillus


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 */ } 
like image 43
stramin Avatar answered Oct 04 '22 02:10

stramin