CSS gaps and gap decoration property overview
The CSS gaps module defines the properties that let you control the space between rows and columns in CSS grid, flexbox, and multi-column layouts. The module also defines properties for drawing decorative lines, called rules or gap decorations, within those gaps.
This guide introduces gap decorations and the properties that control their appearance.
Introducing gap rules
The CSS gap properties enable drawing lines in the center of each gutter (or gap) in multi-column, grid, and flexbox layouts. While margin and padding specify visual spacing around individual boxes, the properties in the CSS gaps module let you specify spacing between adjacent boxes within a given layout. You can add rules to every gap or a subset of gaps, control their widths, colors, and insets, and animate those features.
Originally, column gaps and their decorations were limited to CSS multi-column layout. You could set the width, color, and style of column rules, but all rules shared the same appearance and spanned the full block dimension.
Grid layout has always supported gaps between rows and columns, but it originally didn't support decorations in those gaps. Before rules expanded to all gap-aware layouts, painting lines in the gaps between rows and columns in grid and flexbox layouts required hacks such as background images, borders on all items, and even absolutely positioned overlays.
The CSS gaps module lets you define column and row gaps for all gap-aware layout containers and draw rules in the middle of both horizontal and vertical gaps. These rules don't affect the box model.
This example demonstrates the basic gap decoration features, with lime and darkviolet rules in the gaps between grid rows and columns, respectively.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 60px;
padding: 1rem;
gap: 1.5rem;
column-rule: thick solid darkviolet;
row-rule: thick solid lime;
}
Defining the size of gaps
The gap shorthand and its constituent properties (column-gap and row-gap) let you specify the spacing between adjacent columns and rows within a given layout context. This spacing creates the gaps in which rules can be drawn.
column-gap: A<line-width>or<length-percentage>: This can be one of the keywordsthin,medium, orthick, or a positive<length>or<percentage>value, defining the width of the gap between columns. The default is0in flexbox and grid, and1emin multi-column layout.row-gap: The same vocabulary ascolumn-gap, defining the width of the gap between rows.gap: Taking one or two values, sets both therow-gapandcolumn-gapvalues at once, in that order.
While these properties may seem intuitive at first, the default size and how percentage values for gap values are handled differ by layout type and whether the container has a fixed size. See the Defining CSS gaps guide to learn how to size gaps in grid, flexbox, and multi-column layouts, including how percentage values are resolved.
Defining the appearance of rules
The column-rule-* and row-rule-* properties control the width, color, and style of rules in column and row gaps, respectively. The rule-* shorthand properties apply the same values to both column and row rules.
All the properties in this section accept a comma-separated list of values, allowing for varying gap decorations within a container. You can use the repeat() function within the list to repeat values a specified number of times or as many times as needed to style all the rules.
The column-rule-* properties include:
column-rule: Shorthand defining the<line-width>,<line-style>, and<color>of the column rules.column-rule-color: Defines the colors of the column rules. The default iscurrentcolor.column-rule-style: Defines the line styles of the column rules. It can besolid,dashed,dotted,double,groove,ridge,inset,outset,none, orhidden.column-rule-width: Defines the widths of the column rules. It can bethin,medium, orthick, or a positive<length>value.
The following row-rule-* properties follow the same syntax as their column-rule-* counterparts:
row-rule: Shorthand defining the<line-width>,<line-style>, and<color>of the row decoration line.row-rule-color: The colors of the rules drawn in row gaps. The default iscurrentcolor.row-rule-style: The line styles of the rules drawn in the row gaps.row-rule-width: The widths of the rules drawn in the row gaps.
The following rule-* shorthand properties take the same values as their column-* property components, applying the same list of values to both axes:
rule: Setscolumn-ruleandrow-ruleto the same value.rule-color: Sets both thecolumn-rule-colorandrow-rule-colorto the same value.rule-style: Sets both thecolumn-rule-styleandrow-rule-styleto the same value.rule-width: Sets both thecolumn-rule-widthandrow-rule-widthto the same value.
See Defining CSS gap rules to learn more about defining rule colors, widths, styles, and using the repeat() function.
Splitting rules into segments
In grid and multi-column layouts, rules stretch across the container's entire block and inline dimensions by default. The *-rule-break properties enable splitting lines that span across multiple rows or columns into multiple segments. You can set the behavior for breaking rule lines at every intersection using the following properties:
column-rule-break: The keywordsnone,normal, orintersection, specifying whether the column rules break across row gaps or run continuously.row-rule-break: The keywordsnone,normal, orintersection, specifying whether the row rules break across column gaps or run continuously.rule-break: Setscolumn-rule-breakandrow-rule-breakto the same value.
Expanding on the first example, here we've split the row rules into separate segments at column gaps while keeping the column rules continuous:
.grid {
column-rule-break: none;
row-rule-break: intersection;
}
By default, when the lines are set to break, internal segments end at the edge of the gap without crossing into it, while segments that abut the container's edge are drawn up to the edge of the container's content box.
Controlling the length of rule segments
The *-rule-inset-* properties let you change the lengths of rule segments by shortening or lengthening them. They adjust the segment ends relative to where each segment would normally start or end.
By default, continuous rules are drawn from one end of the container to the other. The rules end at the container edge where no other rule segments are present; these rules have two cap segment endpoints. The start is the cap-start edge, and the end is the cap-end edge.
When a rule breaks into segments, the edge type depends on whether it meets other rule segments at that edge. Column rules intersect row rules at interior junctions. When a rule segment starts or ends at an interior junction where other rule segments are present, those edges are junction-start and junction-end edges, respectively.
The *-rule-inset-junction-* properties define the endpoints at gap junctions where other segments are present. The *-rule-inset-cap-* properties, on the other hand, adjust endpoints where no other rule segments are present. These include endpoints at the container's inline and block edges and at intersections where the rule-visibility-items property prevents other segments from being painted.
In the following example, the column rules are continuous but shortened by 10px at both the top and bottom using the column-rule-inset-cap shorthand property. The row rules are split into segments at the column gap edges using the row-rule-break property. We've set row-rule-inset-cap to -20px to extend their cap ends outward by 20px. We've also set row-rule-inset-junction to 10px to shorten the segments by 10px at their junction ends. Alternatively, we could have set the row-rule-inset shorthand to -20px / 10px, which defines the cap and junction insets in that order.
.grid {
row-rule-break: intersection;
column-rule-inset-cap: 10px;
row-rule-inset-cap: -20px;
row-rule-inset-junction: 10px;
}
Column rule inset properties
The column-rule-inset-* properties enable insetting column segment cap and junction edges, controlling the segment starts, ends, or both.
Column rule inset cap properties
The column-rule-inset-cap-* properties specify the space between a segment endpoint and the container edge or a gap junction where no other gap decoration segments exist. Positive values make the segment shorter, while negative values make the segments longer. The properties accept <inset-value> values: either a <length-percentage> or the overlap-join keyword. A <length-percentage> defines the space between the top or bottom of the column segment and the adjacent row gap or container edge. The overlap-join keyword resolves to 0 for cap edges. If the segment ends at the container's edge, percentage values resolve to 0. Otherwise, percentages are relative to the row gap size, plus any spacing added by the align-content property.
-
column-rule-inset-cap-end: Defines the space between the end, or top, of the column rule segment and, depending on whether the segment starts at the top of the container or at an interior junction, either the start edge of the container or the bottom edge of the row gutter, respectively. -
column-rule-inset-cap-start: Defines the space between the start, or top, of the column rule segment and, depending on whether the segment starts at the top of the container or at an interior junction, either the start edge of the container or the bottom edge of the row gutter, respectively. -
column-rule-inset-capshorthand: Specifies both thecolumn-rule-inset-cap-startandcolumn-rule-inset-cap-endas one or two<inset-value>values. If you specify only one value, both properties are set to that value. If you specify two values, the first setscolumn-rule-inset-cap-startand the second setscolumn-rule-inset-cap-end.
Column rule inset junction properties
The column-rule-inset-junction-* properties specify the space between the segment and the edge of the row gutter at junctions with other rule segments. Positive values make the segment smaller, while negative values and the overlap-join keyword value make the segments longer. Percentage values are relative to the row gap size, plus spacing added by the align-content property.
-
column-rule-inset-junction-end: Defines the space between the end, or bottom, of the column rule segment and the top edge of the row gutter below the segment. -
column-rule-inset-junction-start: Defines the space between the start, or top, of the column rule segment and the bottom edge of the row gutter above the segment. -
column-rule-inset-junctionshorthand: Specifies both thecolumn-rule-inset-junction-startandcolumn-rule-inset-junction-endproperties as one or two values. If you specify only one value, both properties are set to that value. If you specify two values, the first setscolumn-rule-inset-junction-startand the second setscolumn-rule-inset-junction-end.
Column cap and junction shorthand properties
You can use the column-rule-inset-* properties to set cap and junction insets to the same value, while the column-rule-inset shorthand can be used to set the edges to independent values:
-
column-rule-inset-end: The single<inset-value>sets both thecolumn-rule-inset-cap-endandcolumn-rule-inset-junction-endto the same value. -
column-rule-inset-start: The single<inset-value>sets both thecolumn-rule-inset-cap-startandcolumn-rule-inset-junction-startto the same value. -
column-rule-insetshorthand: Specifies thecolumn-rule-inset-cap-start,column-rule-inset-cap-end,column-rule-inset-junction-start, andcolumn-rule-inset-junction-endproperties as one to four<inset-value>values. If you set the cap insets to different values from the junction insets, separate them with a slash (/). This property offers more control than thecolumn-rule-inset-endandcolumn-rule-inset-startshorthands, letting you set cap and junction edges to different values.
Row inset properties
The row-rule-inset-* properties let you change the lengths of row segments by shortening or lengthening them. They adjust the row segment ends relative to where each row segment would normally start or end. Whether the left or right is the start edge depends on the writing mode: in left-to-right languages, like English, the start is the left edge, and the end is the right edge.
Row rule inset cap properties
As with the column-rule-inset-cap-* properties, the row-rule-inset-cap-* properties specify the space between the segment cap edge and the container or gap junction where no other segments exist. The <length-percentage> values define the space between the left or right of the segment and the right or left of the column gutter at interior junctions where no other gap decoration segment exists, or the left or right edge of the container. Percentages are relative to the size of the abutting column gap, plus any additional spacing added by justify-content property. If the end of the segment is the edge of the container, percentages resolve to 0. The overlap-join value also resolves to 0.
-
row-rule-inset-cap-end: Defines the space between the end of the row segment and the edge of the container or the edge of a column gap where no other gap decoration segment exists; whether this is the left or the right edge of the segment depends on the writing mode. -
row-rule-inset-cap-start: The same asrow-rule-inset-cap-end, but for the start edge instead of the end edge of the segment. -
row-rule-inset-capshorthand: One or two<inset-value>values setting therow-rule-inset-cap-startand therow-rule-inset-cap-endvalues. If you specify only one value, both properties are set to that value. If you specify two values,row-rule-inset-cap-startis set to the first value androw-rule-inset-cap-endis set to the second value.
Row rule inset junction properties
The row-rule-inset-junction-* properties specify the space between the segment and the edge of the column gutter at junctions with other rule segments. Positive values make the segments smaller, while negative values and the overlap-join keyword value make the segments longer. Percentage values are relative to the column gap size, plus spacing added by the justify-content property.
-
row-rule-inset-junction-end: Defines the space between the end of the segment and the start of the column gap. Whether theendis the left or the right side of the segment depends on the writing mode. -
row-rule-inset-junction-start: Defines the space between the start of the segment and the end of the column gap. -
row-rule-inset-junctionshorthand: One or two<inset-value>values setting therow-rule-inset-junction-startand therow-rule-inset-junction-endvalues. If you specify only one value, both properties are set to that value. If you specify two values, the first specifiesrow-rule-inset-junction-startand the second specifiesrow-rule-inset-junction-end.
Row cap and junction shorthand properties
You can use the row-rule-inset-* properties to set cap and junction insets to the same value, while the row-rule-inset shorthand can be used to set row segment edges to independent values:
-
row-rule-inset-end: Sets both therow-rule-inset-cap-endandrow-rule-inset-junction-endto the same<inset-value>value. -
row-rule-inset-start: Sets both therow-rule-inset-cap-startandrow-rule-inset-junction-startto the same<inset-value>value. -
row-rule-insetshorthand: Specifies therow-rule-inset-cap-start,row-rule-inset-cap-end,row-rule-inset-junction-start, androw-rule-inset-junction-endproperties as one to four<inset-value>values. If you set the cap insets to different values from the junction insets, separate them with a slash (/). This property offers more control than therow-rule-inset-endandrow-rule-inset-startshorthands, letting you set row cap and junction start and end edges to different values.
Column and row shorthands
When setting both column and row insets to the same values, you can use a rule-inset-* shorthand, including:
-
rule-inset-cap: Setscolumn-rule-inset-capandrow-rule-inset-capto the same value. -
rule-inset-junction: Setscolumn-rule-inset-junctionandrow-rule-inset-junctionto the same value. -
rule-inset-end: Setscolumn-rule-inset-endandrow-rule-inset-endto the same value. -
rule-inset-start: Setscolumn-rule-inset-startandrow-rule-inset-startto the same value. -
rule-inset: Setscolumn-rule-insetandrow-rule-insetto the same values. Specifies therule-inset-cap-start,rule-inset-cap-end,rule-inset-junction-start, andrule-inset-junction-endproperties as one to four<inset-value>values. If you set the cap insets to different values from the junction insets, separate them with a slash (/).
See Defining rule insets to learn more about defining CSS gap rule insets, including breaking rules into segments and controlling insets at cap and junction edges.
Controlling rule visibility
You can define whether rule segments are painted in portions of gaps adjacent to empty areas.
In this example, note how no rule segments are painted alongside the empty grid areas.
.grid {
rule-visibility-items: between;
}
The following properties control whether rule segments are painted next to empty areas:
column-rule-visibility-items: Specifies where segments of column rules that are set to break should be painted: in all column gaps, only where at least one column area adjacent to the gap has an item, or only where both column areas adjacent to the gap have an item. Valid values include theall,around,between, andnormalkeywords.row-rule-visibility-items: The same ascolumn-rule-visibility-items, but for row rule segments.rule-visibility-items: Sets thecolumn-rule-visibility-itemsandrow-rule-visibility-itemsproperties to the same value.
Controlling rule overlap
If row and column rules are continuous, they overlap where they cross, at the junction intersections. Rules split into segments also overlap when their junction insets are set to overlap-join. Negative insets may also cause rule segments to overlap, depending on the gap size, the rule size, and the inset value. You can control whether row rules are painted over column rules, or vice versa, using the rule-overlap property.
Expanding on the first example on this page, we've set the column rules to be painted over the row rules when they intersect.
.grid {
rule-overlap: column-over-row;
}
rule-overlap: Sets the paint order where row rules and column rules overlap as either the keywordrow-over-columnorcolumn-over-row.