revision:
- defines the size of the gap between the rows and between the columns in flexbox, grid or multi-column layout. It is a shorthand for the following properties: "row-gap", "column-gap".
Note: The gap property was formerly known as grid-gap.
The grid-* properties were added early when CSS Grid's specifications were being drafted, and later deprecated when gap became more generalized.
Browsers still support these deprecated grid-* properties and merely treat them as if the "grid-prefix" is not present.
Hence, grid-gap is the same as gap, grid-column-gap is the same as "column-gap" and grid-row-gap is the same as "row-gap".
Property values:
row-gap : sets the size of the gap between the rows in a grid or flexbox layout
column-gap : sets the size of the gap between the columns in a grid, flexbox or multi-column layout
initial : sets this property to its default value
inherit : inherits this property from its parent element.
row-gap introduces space between row tracks;
column-gap introduces space between column tracks.
Things do get a little more complicated when "writing-mode" is used : the default writing mode on the web is horizontal, from left to right, but there are vertical writing modes as well, and when that happens, columns become horizontal and rows are vertical. Always pay attention to writing-mode as it can make it less intuitive than it usually is.
example: gap property
Codes:
<style> .container {display: grid; margin-left: 1vw; grid-template-columns: 10vw 10vw 10vw; grid-template-rows: 10vw 10vw; background: green;text-align-last: center; gap: 2vw;} .grid-item{background:yellow; border:0.3vw solid blue;} </style> <div class="container"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> </div>
By default, items within the flex container are stacked from left to right on the same horizontal line. In this case, column-gap is applied between items and row-gap does nothing. That's because there is only one line (or row).
Switching the flex-direction of the container to column, the items are stacked vertically, from top to bottom. Column-gap does not work in the "column direction", and the row-gap needs to be used to.
As an alternative, we can use the "gap shorthand" with one value, which would apply the same gap in both directions and, therefore, work in both cases.
example: flexbox gaps
code:
<style> .container_a{margin-left: 1vw; display:flex; flex-direction: row; column-gap:1vw;} .container_b{margin-left: 1vw; display:flex; flex-direction: row; gap:1vw;} .flex-item{background:orange; border:0.1vw solid blue;} .flex-item_b{background:green; border:0.1vw solid blue;} </style> <div class="container_a"> <div class="flex-item">flex-item 1</div> <div class="flex-item">flex-item 2</div> <div class="flex-item">flex-item 3</div> <div class="flex-item">flex-item 4</div> <div class="flex-item">flex-item 5</div> <div class="flex-item">flex-item 6</div> </div><br> <div class="container_b"> <div class="flex-item_b">flex-item 1</div> <div class="flex-item_b">flex-item 2</div> <div class="flex-item_b">flex-item 3</div> <div class="flex-item_b">flex-item 4</div> <div class="flex-item_b">flex-item 5</div> <div class="flex-item_b">flex-item 6</div> </div>
To summarize, "colum-gap" always works vertically (assuming the default writing-mode), and "row-gap" always works horizontally. This does not depend on the direction of the flex container.
In this case, the column-gap is still applied vertically between items, and row-gap is applied horizontally between the two flex lines.
example: wrap
code:
<div> <style> .container_c {margin-left: 1vw; display: flex; flex-wrap: wrap; column-gap: 4vw; row-gap: 1vw; justify-content: center; border:0.3vw dashed orange;} .flex-item_c{background:lightgreen; border:0.1vw solid blue;width: 10vw; height: 5vw;} </style> <div class="container_c"> <div class="flex-item_c">flex-item 1</div> <div class="flex-item_c">flex-item 2</div> <div class="flex-item_c">flex-item 3</div> <div class="flex-item_c">flex-item 4</div> <div class="flex-item_c">flex-item 5</div> </div> </div>
There's one interesting difference between this and grid. The column gaps don't necessarily align across flex lines. That's because of "justify-content: center", which centers items within their flex lines.
This way, we can see that each flex line is a separate layout, where gaps apply independently of other lines.
There are three notable differences: 1/ row-gap has no effect, 2/ column-gap has a non-0 default value, and 3/ gaps can be styled.
row-gap has no effect: in multi-column layouts, there aren't any rows to separate. That means only column-gap is relevant (as is the gap shorthand).
unlike in grid and Flexbox, column-gap has a default value of 1em in multi-column layouts (as opposed to 0). So, even when no gap is specified at all, the columns of content are still visually separated. The default gap can, of course, be overridden but it's a good default to have.
We can style the empty gap between columns in a multi-column layout. We use the "column-rule property", which works like "border".
example: mult-column gap
code:
<style> .container_d {margin-left: 4vw;column-count: 3; padding: 1em; column-gap: 1vw; column-rule: 0.4vw solid deepskyblue; padding:1.2vw; } </style> <div class="container_d"> What is Lorem Ipsum? .. . . </div>
Styling gap in Flexbox and CSS Grid would be really useful, but it isn't supported anywhere today. The good news is that it could be in the near future.
In the meantime, there are ways around this.
One is to give the grid container a background color, then a different color for the items, and finally a gap to make the container color show through.
While this works, it means we're unable to use gaps to introduce space between items.
The gap here acts as a "border width instead". So, to visually separate the items out a bit more, we need to use padding or margin on the items, which isn't as great.
example: styling the gap
code:
<style> grid_a { margin-left:7vw; background: red; border: 0.2vw solid deepskyblue; display: grid; gap: 0.5vw; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(2, auto);} .item {background: lightgreen; padding: 0.5vw; line-height: 2;} .item.long {grid-column: span 2;} </style> <div class="grid_a"> <div class="item">Lorem ipsum dolor. . . . </div> <div class="item">Proin quis nulla enim. ... </div> <div class="item long">Vestibulum luctus convallis mattis..... </div> <div class="item"> <img src="../../pics/car2.jpg" width="200">....... </div> <div class="item long">Morbi et orci consectetur .... </div> <div class="item">Aliquam in sem sagittis, .... </div> </div>
Another workaround uses"margin" and/or "padding" to add visual space between elements of a layout.
The "gap property" is just one part of the empty space created between boxes in a layout container.
"margin", "padding', and "alignment", all may increase the empty space on top of what gap already defines.
example: use margin/padding
code:
<div> <style> .container_e {margin-left:7vw; display: flex; gap: 4vw; width:85vw; justify-content: space-around; border: 0.3vw dashed orange;} .flex-item_e{background:lightgreen;padding:1vw; margin: 0 2vw;} </style> <div class="container_d"> <div class="flex-item_e">item 1</div> <div class="flex-item_e">item 2</div> <div class="flex-item_e">item 3</div> <div class="flex-item_e">item 4</div> <div class="flex-item_e">item 5</div> </div> </div>
There are four different types of empty space between two consecutive flex items:
Between two consecutive items, the gap defines the minimum space between these items. There can be more, like in this case, but there can never be less space.
Margin pushes items even further apart, but unlike gap, it adds space at both sides of all items.
Padding provides some space inside each item.
Finally, and only because there is enough remaining space, content distribution kicks in and distributes the items evenly within the flex line, according to the "space-around" value.