The gap property adds consistent space between items in grid and flex layouts without margin on every child.
01
row-gap
Vertical space.
02
column-gap
Horizontal space.
03
Grid
Grid layouts.
04
Flexbox
Flex layouts.
05
px / rem
Length units.
06
Shorthand
One property.
Fundamentals
Introduction
The gap property in CSS is a shorthand for setting the row-gap and column-gap properties in grid and flexbox layouts. It defines the space between rows and columns, making it easier to control the layout of your content with consistent spacing.
Definition and Usage
Before gap, developers often used margins on each child to create space between grid or flex items. That approach is harder to maintain and can cause unwanted outer spacing. gap applies spacing only between items, keeping the container edges clean.
💡
Beginner Tip
Put gap on the parent container (display: grid or display: flex), not on the individual items. One value like gap: 1rem sets the same space in both directions.
Foundation
📝 Syntax
The syntax for the gap property allows you to specify the space for rows and columns in a grid or flex container:
syntax.css
container{gap:row-gapcolumn-gap;}
row-gap is the space between rows.
column-gap is the space between columns.
If only one value is provided, it applies to both rows and columns.
1rem gap grows proportionally if the user increases their browser default font size, keeping layout spacing accessible.
A11y
♿ Accessibility
Use sufficient gap between interactive elements so touch targets are easy to tap on mobile.
Prefer rem for gap so spacing scales when users change root font size.
Do not rely on gap alone for semantic grouping; use headings and landmarks for structure.
Test wrapped flex layouts to ensure gap spacing remains readable at narrow widths.
Keep contrast on items inside gapped layouts; spacing does not replace color contrast requirements.
🧠 How gap Works
1
You set gap on the container
Apply gap to a grid or flex parent with one or two length values.
Declaration
2
Browser reserves track space
The layout engine inserts fixed gutters between rows and columns or between flex items.
Layout
3
Items sit in remaining space
Children fill the tracks or flex lines without needing individual margins for spacing.
Placement
=
▦
Even, maintainable spacing
Items are separated cleanly with no extra outer margin on the first or last child.
Compatibility
🖥 Browser Compatibility
The gap property is widely supported in modern browsers, including recent versions of Chrome, Firefox, Safari, Edge, and Opera. Grid gap has longer support; flexbox gap was added in later browser versions.
✓ Baseline · Modern browsers
gap in grid and flex
Grid gap is universal in modern engines. Flex gap is supported in current Chrome, Firefox, Safari, and Edge.
96%Modern browser support
Google Chrome57+ grid · 84+ flex
Supported
Mozilla Firefox52+ grid · 63+ flex
Supported
Apple Safari10.1+ grid · 14.1+ flex
Supported
Microsoft Edge16+ grid · 84+ flex
Supported
Opera44+ grid · 70+ flex
Supported
gap property96% supported
Bottom line: Safe for modern projects. Grid gap is nearly universal; flex gap works in all current major browsers.
Wrap Up
🎉 Conclusion
The gap property is an essential tool for web developers working with grid and flexbox layouts. By using gap, you can easily control the spacing between rows and columns, creating a clean and well-organized design.
Experiment with different values to see how this property can improve the layout of your web projects. Start with a single value like gap: 1rem, then try separate row and column gaps when you need finer control.
The gap property sets the space between rows and columns in a grid or flex container. It is a shorthand for row-gap and column-gap.
The default value is normal, which means no extra gap is added between items (0 in practice).
gap adds space only between items inside a grid or flex container. margin adds space around an individual element and can affect layout outside the container.
Yes. Modern browsers support gap in flex containers as well as grid containers, making it a convenient way to space flex items without margin hacks.
gap is shorthand that can set both axes at once. row-gap controls vertical spacing between rows; column-gap controls horizontal spacing between columns.