Spacing between columns and rows in CSS grids
The gap
property allows you to simultaneously set the distance between columns and rows in a grid.
You can pass either one value or two separated by a space. If one value is passed, it specifies the distance between columns and rows. If two values are passed, the first specifies the distance between rows, and the second - between columns.
Example
Let's set the same distance between columns and rows:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
#parent {
display: grid;
gap: 10px;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
padding: 10px;
border: 2px solid #696989;
width: 600px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Let's set different distances between columns and rows:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
#parent {
display: grid;
gap: 20px 10px;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
padding: 10px;
border: 2px solid #696989;
width: 600px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Practical tasks
Create a table with a 10px
spacing between columns and rows.
Create a table where the spacing between columns and rows is 10px
and 5%
respectively.