gap

Sets the spacing between child elements in flex or grid containers. Only works with df and dg, serving as a modern replacement for margin-based spacing.

Class List

ClassCSSDescription
gap4pxgap: 4pxMinimum gap. Closely spaced icons, inline elements
gap8pxgap: 8pxNarrow gap. Button groups, tag lists
gap12pxgap: 12pxNormal gap. Form fields, list items
gap16pxgap: 16pxStandard gap. General content spacing
gap2remgap: 2rem (20px)rem start. Card lists, inside sections
gap2-4remgap: 2.4rem (24px)Generous gap. Card grids
gap3-2remgap: 3.2rem (32px)Wide gap. Section separation
gap4remgap: 4rem (40px)Large gap. Large layouts
rg8pxrow-gap: 8pxRow (vertical) gap 8px only
rg16pxrow-gap: 16pxRow (vertical) gap 16px only
rg2remrow-gap: 2rem (20px)Row (vertical) gap 20px only
cg8pxcolumn-gap: 8pxColumn (horizontal) gap 8px only
cg16pxcolumn-gap: 16pxColumn (horizontal) gap 16px only
cg2remcolumn-gap: 2rem (20px)Column (horizontal) gap 20px only

Visual Comparison

Compares how various gap values affect spacing between elements in a flex container.

4px — gap4px

ABCD

8px — gap8px

ABCD

12px — gap12px

ABCD

16px — gap16px

ABCD

20px (2rem) — gap2rem

ABCD

32px (3.2rem) — gap3-2rem

ABCD

gap vs margin Comparison

gap only adds spacing between items, while margin adds spacing on all sides.

gap — df gap16px

ABC

16px gap only between items. No gap outside first/last items

margin — mr16px

ABC

Unnecessary 16px gap after the last item

Why gap is better

gap adds spacing only between items, eliminating the extra margin problem on first/last items. Works correctly even with wrapping.

row-gap & column-gap

Row (vertical) and column (horizontal) spacing can be specified separately. rg is row-gap, cg is column-gap.

<!-- Separate row-gap and column-gap -->
<div class="dg gtcr3-1fr rg2-4rem cg8px">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

<!-- row-gap only (no horizontal gap) -->
<div class="df fdc rg16px">
  <div>Row 1</div>
  <div>Row 2</div>
  <div>Row 3</div>
</div>

<!-- column-gap only (no vertical gap) -->
<div class="df cg2rem">
  <div>Col 1</div>
  <div>Col 2</div>
  <div>Col 3</div>
</div>

rg2-4rem cg8px — row 24px, column 8px

123456

Row gap (vertical) is wide, column gap (horizontal) is narrow

ClassCSSdirection
rg8pxrow-gap: 8pxRow (vertical) gap
rg16pxrow-gap: 16pxRow (vertical) gap
rg2remrow-gap: 2remRow (vertical) gap — 20px
rg2-4remrow-gap: 2.4remRow (vertical) gap — 24px
cg8pxcolumn-gap: 8pxColumn (horizontal) gap
cg16pxcolumn-gap: 16pxColumn (horizontal) gap
cg2remcolumn-gap: 2remColumn (horizontal) gap — 20px
cg2-4remcolumn-gap: 2.4remColumn (horizontal) gap — 24px

Unit Examples

Use px for under 20px and rem for 20px and above. Based on 1rem = 10px.

ClassCSSActual size
gap4pxgap: 4px4px
gap8pxgap: 8px8px
gap12pxgap: 12px12px
gap16pxgap: 16px16px
gap2remgap: 2rem20px
gap2-4remgap: 2.4rem24px
gap3-2remgap: 3.2rem32px
gap4remgap: 4rem40px
gap4-8remgap: 4.8rem48px

Common Patterns

Commonly used gap combination patterns in practice.

PatternClassUsage
Card griddg gtcrfit-minmax28rem-1fr gap2remResponsive card list
Button groupdf gap8pxGap between buttons
Form fieldsdf fdc gap16pxVertical form layout
Navigationdf aic gap2-4remHeader menu gap
Tag listdf fww gap8pxWrapping tags
Section gapdf fdc gap4remBetween large content blocks
<!-- Card grid -->
<div class="dg gtcrfit-minmax28rem-1fr gap2rem">
  <div class="bg18181B p2rem br8px">Card 1</div>
  <div class="bg18181B p2rem br8px">Card 2</div>
  <div class="bg18181B p2rem br8px">Card 3</div>
</div>

<!-- Button group -->
<div class="df gap8px">
  <button class="bg6366F1 cFFFFFF py8px px16px br8px bn cp">Save</button>
  <button class="bg27272A cFFFFFF py8px px16px br8px bn cp">Cancel</button>
</div>

<!-- Form fields -->
<form class="df fdc gap16px">
  <input type="text" placeholder="Name" />
  <input type="email" placeholder="Email" />
  <textarea placeholder="Message"></textarea>
  <button class="bg6366F1 cFFFFFF py8px px16px br8px bn cp">Submit</button>
</form>

<!-- Tag list (wrapping allowed) -->
<div class="df fww gap8px">
  <span class="bg27272A cFAFAFA py4px px12px br4px fs14px">HTML</span>
  <span class="bg27272A cFAFAFA py4px px12px br4px fs14px">CSS</span>
  <span class="bg27272A cFAFAFA py4px px12px br4px fs14px">JavaScript</span>
  <span class="bg27272A cFAFAFA py4px px12px br4px fs14px">Vue</span>
  <span class="bg27272A cFAFAFA py4px px12px br4px fs14px">Nuxt</span>
</div>

Tips & Notes

gap is flex/grid only

gap only works in df(display: flex) or dg(display: grid) containers. Does not apply to block or inline elements.

Use gap instead of margin

gap is cleaner than margin for inter-item spacing. No extra margin issues on first/last items, and works correctly with wrapping.

Use multiples of 4

Use multiples of 4 for spacing values: 4px, 8px, 12px, 16px, 2rem(20px), 2-4rem(24px), etc. A consistent spacing system improves design quality.

Fine control with row-gap/column-gap

To set different row and column spacing, specify rg(row-gap) and cg(column-gap) individually. gap applies both values equally.

Shorthand decomposition: gap is internally decomposed into row-gap + column-gap. When using gap2rem cg1rem, only column-gap is precisely overridden to 1rem. 2-value usage is also decomposed: gap2rem-4remrow-gap: 2rem; column-gap: 4rem, allowing precise overrides with directional classes.