CSS row-gap Property
The CSS row-gap property specifies the spacing between adjacent rows in Grid, Flexbox, and multi-column layouts.
It is part of the CSS Box Alignment specification and works alongside column-gap and the gap shorthand property. Unlike margins, gaps create spacing between layout tracks or flex lines without adding extra space around the outside edges of the container.
row-gap Syntax
The row-gap property accepts the keyword normal or a non-negative length or percentage.
Basic Syntax
selector {
row-gap: 20px;
}
Available Values
.container {
row-gap: normal;
}
.container {
row-gap: 0;
}
.container {
row-gap: 20px;
}
.container {
row-gap: 1.5rem;
}
.container {
row-gap: 5%;
}
.container {
row-gap: calc(10px + 1vw);
}
.container {
row-gap: clamp(10px, 2vw, 30px);
}
CSS-Wide Keywords
.container {
row-gap: initial;
}
.container {
row-gap: inherit;
}
.container {
row-gap: unset;
}
.container {
row-gap: revert;
}
.container {
row-gap: revert-layer;
}
Negative lengths and percentages are not permitted. A value of 0 removes the spacing introduced by the property.
Property Values
| Value | Description |
|---|---|
normal |
Uses the layout-dependent normal gap. It generally computes to zero in Grid and Flexbox, while multi-column layout can use a nonzero normal gap. This is the initial value. |
<length> |
Specifies a non-negative gap using units such as px, em, or rem. |
<percentage> |
Specifies a gap as a percentage of the container's corresponding content-box dimension, subject to layout-specific percentage resolution rules. |
calc() |
Calculates a gap using compatible lengths, percentages, and arithmetic expressions. |
clamp() |
Constrains a responsive gap between specified minimum and maximum values. |
initial |
Resets the property to normal. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to normal because it is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | No |
| Applies to | Multi-column, Flexbox, and Grid containers |
| Computed value | Specified percentage or absolute length, or normal as defined by the layout mode |
| Animation type | Length, percentage, or calculated value |
| Percentages | Relative to the corresponding content-box dimension of the container |
Valid Values
.grid {
row-gap: 16px;
}
.grid {
row-gap: 2rem;
}
.grid {
row-gap: 10%;
}
.grid {
row-gap: calc(1rem + 5px);
}
Invalid Values
.grid {
row-gap: -10px;
}
.grid {
row-gap: auto;
}
.grid {
row-gap: 20;
}
.grid {
row-gap: 10px 20px;
}
.grid {
row-gap: space-between;
}
The property does not accept negative gaps, unitless nonzero lengths, multiple gap values, or alignment keywords.
How It Works
The row-gap property creates spacing between adjacent rows or flex lines within a supported layout container.
Basic Grid Container
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
row-gap: 20px;
}
The grid has 20 pixels of spacing between adjacent rows.
Spacing Between Rows
When a grid contains three rows, row-gap creates two gaps: one between the first and second rows and another between the second and third rows.
No Outer Gap
The property does not create additional spacing before the first row or after the last row. Use padding when space is needed around the inside edges of the container.
Effect on Layout Size
Gaps participate in layout calculations and can affect the total space required by a container. They are not simply visual offsets.
Important Distinction
The row-gap property creates spacing between layout rows or flex lines. It does not add margins to individual child elements.
Using row-gap With CSS Grid
In CSS Grid, row-gap controls the spacing between adjacent grid row tracks.
Basic Grid Example
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
row-gap: 20px;
}
Items are arranged into three columns, with 20 pixels between grid rows.
Different Row and Column Gaps
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
row-gap: 30px;
column-gap: 15px;
}
Rows are separated by 30 pixels, while columns are separated by 15 pixels.
Fixed Row Heights
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 100px;
row-gap: 20px;
}
Each automatically created row is 100 pixels tall, with 20 pixels between rows.
Grid Rows With Flexible Heights
.grid-container {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
row-gap: 1.5rem;
}
The row heights can follow their content while the spacing remains consistent.
Important Consideration
Grid gaps are placed between tracks, not between every individual grid item. Items spanning multiple rows do not receive extra internal gaps simply because they span those tracks.
Using row-gap With Flexbox
In Flexbox, the meaning of row-gap depends on the flex direction.
Wrapping Row Layout
.flex-container {
display: flex;
flex-wrap: wrap;
row-gap: 20px;
}
With the default flex-direction: row, the property creates spacing between flex lines when items wrap.
Column Flex Direction
.flex-container {
display: flex;
flex-direction: column;
row-gap: 20px;
}
In a column flex container, row-gap creates spacing between adjacent items along the vertical main axis.
Horizontal and Vertical Spacing
.flex-container {
display: flex;
flex-wrap: wrap;
row-gap: 20px;
column-gap: 12px;
}
For a wrapping row flex container, row-gap separates flex lines and column-gap separates adjacent items within each line.
Single-Line Row Flexbox
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
row-gap: 20px;
}
A single-line row flex container has no additional flex lines to separate, so the row gap does not produce visible spacing between its items.
Important Consideration
In Flexbox, the relationship between row-gap and the visible spacing depends on flex-direction and whether the container wraps.
Using row-gap With Multi-Column Layout
The row-gap property is part of the general gap system used by multi-column layout, although its effect differs from Grid and Flexbox.
Basic Multi-Column Example
.article-columns {
columns: 3;
row-gap: 20px;
}
In an ordinary multi-column layout, the content flows through columns rather than separate grid-like rows. The row-gap declaration therefore does not normally create visible spacing between text blocks.
Spacing Between Columns
.article-columns {
columns: 3;
column-gap: 30px;
}
Use column-gap to control the visible spacing between columns in a typical multi-column layout.
Important Distinction
Although the gap properties share a common specification, they do not produce identical effects across every layout model. For ordinary multi-column text, column-gap is the relevant spacing property.
row-gap Versus gap and column-gap
The row-gap, column-gap, and gap properties are related but serve different purposes.
| Property | Purpose |
|---|---|
row-gap |
Controls spacing between rows or the corresponding flex layout lines or items. |
column-gap |
Controls spacing between columns or the corresponding flex layout lines or items. |
gap |
Shorthand for setting row and column gaps together. |
Using Separate Properties
.grid {
display: grid;
row-gap: 20px;
column-gap: 10px;
}
Equivalent gap Shorthand
.grid {
display: grid;
gap: 20px 10px;
}
When two values are supplied, the first sets row-gap and the second sets column-gap.
Using One gap Value
.grid {
display: grid;
gap: 20px;
}
Both row and column gaps are set to 20 pixels.
Overriding One Gap
.grid {
display: grid;
gap: 20px;
row-gap: 35px;
}
The final row gap is 35 pixels, while the column gap remains 20 pixels.
Important Consideration
The gap shorthand resets both gap properties when declared. Declaration order matters when combining shorthand and longhand values.
Percentage and Responsive Gaps
The row-gap property supports percentages and CSS math functions, but percentage behavior requires special attention.
Percentage Row Gap
.grid {
display: grid;
height: 400px;
row-gap: 5%;
}
With a definite 400-pixel content-box height, a 5% row gap resolves to 20 pixels.
Percentage Gaps With Indefinite Sizes
When the container's corresponding size is indefinite, percentage gaps can resolve differently during intrinsic sizing and final layout. In Grid, cyclic percentage gaps resolve to zero for intrinsic size contributions but can still produce spacing during layout.
Using rem Units
.grid {
display: grid;
row-gap: 1.5rem;
}
The gap scales with the root element's font size.
Using calc()
.grid {
display: grid;
row-gap: calc(10px + 1vw);
}
This combines a fixed minimum contribution with a viewport-relative length.
Using clamp()
.grid {
display: grid;
row-gap: clamp(12px, 2vw, 32px);
}
The gap grows with viewport width but remains between 12 and 32 pixels.
Responsive Media Query
.grid {
display: grid;
row-gap: 24px;
}
@media (max-width: 600px) {
.grid {
row-gap: 12px;
}
}
The layout uses smaller gaps on narrow screens.
Important Consideration
Use lengths such as rem or responsive functions when predictable spacing is important. Percentage gaps can be less intuitive when the container's size depends on its content.
row-gap Example
The following example demonstrates a CSS Grid layout with 25 pixels between rows and 12 pixels between columns.
HTML Example
<div class="row-gap-demo">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
<div class="grid-item">Item 5</div>
<div class="grid-item">Item 6</div>
</div>
CSS Example
.row-gap-demo {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
row-gap: 25px;
column-gap: 12px;
padding: 15px;
background-color: #f1f5f9;
border: 1px solid #cbd5e1;
}
.row-gap-demo .grid-item {
padding: 18px 10px;
background-color: #2563eb;
color: #ffffff;
text-align: center;
font-weight: bold;
}
@media (max-width: 480px) {
.row-gap-demo {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
At wider widths, the six items form two rows of three columns, separated by a 25-pixel row gap. At narrow widths, the layout changes to two columns while retaining the same row spacing.
Increasing the Row Gap
.row-gap-demo {
row-gap: 40px;
}
The vertical space between rows increases to 40 pixels.
Removing the Row Gap
.row-gap-demo {
row-gap: 0;
}
The grid rows no longer have additional spacing from row-gap.
Using Equal Row and Column Gaps
.row-gap-demo {
gap: 20px;
}
The shorthand sets both row and column spacing to 20 pixels.
Accessibility Considerations
Consistent spacing can improve readability and help users distinguish groups of related content.
Maintain Clear Visual Separation
Use enough space between rows to prevent adjacent text and controls from appearing crowded.
Preserve Logical Reading Order
The row-gap property changes spacing but does not change the HTML source order or keyboard navigation sequence.
Support Zoom and Reflow
Test layouts at increased zoom levels and narrow viewport widths to ensure gaps do not create unnecessary horizontal scrolling.
Avoid Excessive Spacing
Very large gaps can make related information appear disconnected or force users to scroll unnecessarily.
Use Semantic Grouping
Spacing alone does not establish semantic relationships. Use appropriate HTML elements and headings to organize content.
Important Consideration
The row-gap property controls layout spacing only. It does not change content semantics, focus order, or assistive technology relationships.
Common Mistakes
Using row-gap Without a Supported Layout
.container {
row-gap: 20px;
}
The property does not create row spacing in an ordinary block layout. Use an appropriate layout model such as Grid or Flexbox.
Using Negative Gap Values
.grid {
display: grid;
row-gap: -10px;
}
Negative gap values are invalid. Use other layout techniques when overlapping elements is intentional.
Using an Unsupported Keyword
.grid {
row-gap: auto;
}
The property accepts normal or a non-negative length or percentage, not auto.
Confusing row-gap With Margins
Margins belong to individual elements, while row-gap creates spacing between layout rows or lines.
Expecting row-gap to Separate Items in a Single-Line Row Flexbox
A non-wrapping row flex container has only one flex line. Use column-gap to separate items along its horizontal main axis.
Assuming Percentages Always Produce Predictable Spacing
Percentage gaps can behave differently when container dimensions are indefinite or depend on their contents.
Confusing row-gap and column-gap in Column Flexbox
In a column flex container, row-gap separates items along the main axis, while column-gap separates flex lines when wrapping creates additional columns.
Overlooking the gap Shorthand
A later gap declaration can override an earlier row-gap declaration because the shorthand sets both gap properties.
Important Considerations
Keep the following points in mind when using row-gap:
- It controls spacing between rows or corresponding layout lines.
- Its initial value is
normal. - The property is not inherited.
- It applies to Grid, Flexbox, and multi-column containers.
- It accepts non-negative lengths and percentages.
- Negative gap values are invalid.
- A value of
0removes the spacing introduced by the property. - The
normalvalue generally computes to zero in Grid and Flexbox. - Grid row gaps separate adjacent row tracks.
- Grid gaps do not add space around the outside edges of the container.
- In a wrapping row flex container, row gaps separate flex lines.
- In a column flex container, row gaps separate adjacent items along the main axis.
- A single-line row flex container has no additional flex lines to separate.
- Ordinary multi-column text uses
column-gapfor spacing between columns. - The
gapshorthand sets both row and column gaps. - When
gaphas two values, the first controls row spacing. - Declaration order matters when combining shorthand and longhand gap properties.
- Gaps participate in layout calculations.
- Percentage gaps use layout-specific resolution rules.
- Indefinite container dimensions can affect percentage gap behavior.
- The
calc()function can combine compatible gap measurements. - The
clamp()function can constrain responsive gap sizes. - Media queries can adjust gaps for different viewport widths.
- Gaps do not change HTML source order.
- Use padding when spacing is needed around the container's inside edges.
- Test spacing at different viewport sizes and zoom levels.
Browser Support
The CSS row-gap property is Baseline Widely available and is supported in current versions of major browsers.
Grid gap support has been available for many years. Flexbox gap support was introduced later, so older browsers may support row-gap in Grid but not in Flexbox.
For current compatibility information, visit Can I Use? and search for row-gap.
When supporting older browsers, check compatibility for the specific layout model in which the property is used.
Specifications
The row-gap property is defined in CSS Box Alignment Module Level 3.
CSS Box Alignment Module Level 3
The specification defines the gap properties and explains their behavior in supported layout models, including Grid, Flexbox, and multi-column layout.
For additional documentation and examples, consult MDN Web Docs.
