CSS Grid: Create a Column Gap Using grid-column-gap And Row Gap using grid-row-gap And Use grid-gap For Row And Coulmn Gap
Sometimes you want a gap in between the columns. To add a gap between the columns, use the
html code
grid-column-gap property like this: in the container divgrid-column-gap: ;
1
2
3
4
5
html code
<style> .d1{background:LightSkyBlue;} .d2{background:LightSalmon;} .d3{background:PaleTurquoise;} .d4{background:LightPink;} .d5{background:PaleGreen;} .container { font-size: 40px; min-height: 300px; width: 100%; background: LightGray; display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-column-gap: 20px; } </style> <div class="container"> <div class="d1">1</div> <div class="d2">2</div> <div class="d3">3</div> <div class="d4">4</div> <div class="d5">5</div> </div>
You can add a gap in between the rows of a grid using
grid-row-gap in the same way that you added a gap in between columns above
1
2
3
4
5
<style> .d1{background:LightSkyBlue;} .d2{background:LightSalmon;} .d3{background:PaleTurquoise;} .d4{background:LightPink;} .d5{background:PaleGreen;} .container { font-size: 40px; min-height: 300px; width: 100%; background: LightGray; display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-row-gap :5px; } </style> <div class="container"> <div class="d1">1</div> <div class="d2">2</div> <div class="d3">3</div> <div class="d4">4</div> <div class="d5">5</div> </div>
grid-gap is a shorthand property for grid-row-gap and grid-column-gap that's more convenient to use.If
grid-gap
has one value, it will a create a gap between all rows and columns.
However, if there are two values, it will use the first one to set the
gap between the rows and the second value for the columns.
Here I used
grid-gap to introduce a 10px gap between the rows and 20px gap between the columns.
1
2
3
4
5
<style> .d1{background:LightSkyBlue;} .d2{background:LightSalmon;} .d3{background:PaleTurquoise;} .d4{background:LightPink;} .d5{background:PaleGreen;} .container { font-size: 40px; min-height: 300px; width: 100%; background: LightGray; display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-gap:10px 20px; } </style> <div class="container"> <div class="d1">1</div> <div class="d2">2</div> <div class="d3">3</div> <div class="d4">4</div> <div class="d5">5</div> </div>
Comments
Post a Comment