日本語

Differences Between gap and margin for Spacing Flex Items in CSS

​​CSS​Flexbox,Playwright

This article was translated from the Japanese original with AI. 日本語の原文を読む

To space out buttons, tags, or cards laid out with display: flex, you can either set gap on the parent or put margin on the children. gap only creates space between adjacent children, so no extra space is left outside the last child. margin is applied to the outside of each child. With right or center alignment, or with wrapping via flex-wrap, the last child's margin shows up as a position shift or as a difference in the number of wrapped rows.

css
.buttons {
  display: flex;
  gap: 20px; /* Only the space between children is 20px */
}
Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page in Playwright with children 100px wide and 50px tall inside a 400px-wide parent with no padding or border, and measured each child's position with getBoundingClientRect(). The x values in the tables are distances from the parent's left edge. I did not test Firefox or Safari.

Comparing the differences in a table first

The measured results are summarized below. I explain each value in the sections that follow.

Case gap: 20px Child margin-right: 20px
Left-aligned in one row 20px between children 20px between children (looks the same)
Aligned with space-between and similar The last child lines up with the parent's right edge 20px is left to the right of the last child
Wrapping with flex-wrap: wrap Three 120px children per row Two per row
Hiding a child with display: none Space appears only between the remaining children If the last child is hidden, the margin remains on the child that becomes last
Reversed order (row-reverse) 20px between children 20px of empty space at the end, and children touch in one spot
Vertical stacking (flex-direction: column) 20px between children Top and bottom margins add together
Negative values Cannot be written (invalid) Can be written (children overlap)
Four rows of diagrams. In the first row, with space-between and gap, three light blue boxes span from the left edge to the right edge of the parent, shown by a dotted line. In the second row, with space-between and margin-right, each box has a gray diagonal-hatched margin on its right, and the third box leaves a gap equal to its margin before the parent's right edge. In the third row, with flex-wrap and gap, 120px boxes are arranged three per row over two rows. In the fourth row, with flex-wrap and margin, each box has a hatched margin on its right and bottom, and they are arranged two per row over three rows
Items spaced with gap and margin inside a 400px-wide parent (gray dotted line). The gray diagonal hatching shows the margin areas.

Left-aligned in one row looks the same

When three 100px children are placed in a row, the x positions are 0, 120, and 240 both with gap: 20px and with margin-right: 20px on every child.

The difference is on the right side of the last child. With margin-right, the last child also has a 20px margin on its outside. With left alignment, that 20px is hidden in the empty space on the parent's right. For this reason, you can write margins on every child except the last one, for example with .item:not(:last-child), to remove the last child's margin. With this approach too, the x positions were 0, 120, and 240.

gap only creates space between children from the start. The opening of the CSS Gaps specification explains that margin and padding specify space around a box, while gap specifies space between adjacent boxes.

Aligned right or centered, the last margin causes an offset

When children are aligned with justify-content on the parent, the last child's margin shows up in its position. The table compares child x positions for gap: 20px and for margin-right: 20px on every child.

justify-content gap: 20px Child margin-right: 20px
space-between 0, 150, 300 0, 140, 280
center 30, 150, 270 20, 140, 260
flex-end 60, 180, 300 40, 160, 280

With space-between and flex-end, gap lines up the right edge of the last child with the parent's right edge (400). With margins, the last child's right edge stops at 380, which is 20px short by the amount of its margin. With center, the margin version shifts the whole row 10px to the left, leaving 20px of space on the left and 40px on the right.

When space-between and gap are used together, the space between children can be wider than gap. In the space-between row of the table above, the space between children was 50px. The specification says that gap is the minimum space between children, and any free space distributed by justify-content or align-content is added to it.

When wrapping, the margin reduces how many children fit in a row

The difference is most visible with flex-wrap: wrap. I placed six 120px children in a 400px-wide parent.

css
/* Space items with gap */
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

/* Space items with margin */
.cards {
  display: flex;
  flex-wrap: wrap;
}
.cards > * {
  margin-right: 20px;
  margin-bottom: 20px;
}

With gap: 20px, three children fit in one row. Three 120px children plus two 20px gaps total exactly 400px. The space between rows is also 20px, and the parent height is 50 + 20 + 50 = 120px.

With margins, only two children fit in one row, and the layout takes three rows. When deciding where to wrap, the browser counts each child's margins, so the third child's margin-right brings the total to 420px, which exceeds 400px (see the CSS Flexbox specification's line-breaking algorithm). Because the children in the last row also get margin-bottom, the parent height becomes 210px, and there is 20px of empty space at the bottom.

Even when :not(:last-child) removes the last child's margin, the layout still fits only two per row. The child at the right end of a wrapped row is not the "last child," so it keeps its margin.

Negative margins to cancel the outer margins

One way to fit three children per row with margins is to give each child margin: 0 10px and give the parent margin: 0 -10px to cancel the outer margins. When I tested this in a 400px box, three children fit in one row.

However, the flex container became 420px wide and extended 10px beyond the outer box on each side (the parent's left edge was 10px to the left of the outer box). The children themselves stayed within the 400px outer box. With gap, you do not need this kind of workaround.

How empty space appears when children are hidden

The difference also appears when some children are not displayed. Here, four 80px children are placed in a row.

With a parent that has gap: 20px, when the third child has display: none, the remaining three children are at x positions 0, 100, and 200. The gap for the hidden child does not remain, so the space closes up.

With margins, combining them with :not(:last-child) can cause problems. If the fourth (last) child is hidden with display: none, the third child is not the last child in the HTML, so its margin-right: 20px stays. With justify-content: flex-end for right alignment, the third child's right edge is at 380, which is 20px away from the parent's right edge. With gap, hiding the fourth child in the same way leaves the third child's right edge at 400.

On the other hand, gap creates space whenever a box exists. When the third child has visibility: hidden, the invisible child's position and the gap on both sides remain, and the x positions are 0, 100, 200, and 300. When the third child is an empty element with width 0, the space between the second and fourth children is 40px. The specification's section on percentage gaps also says that gap is intended to behave as if an empty child of that size were inserted.

Reversing the order does not swap the margin's direction

Even with flex-direction: row-reverse, which puts items from right to left, margin-right stays on the visual right side. When I placed three children with margin-right: 20px on .item:not(:last-child), the first child, which is now on the far right, ended at 380, leaving 20px before the parent's right edge. The gap between the first and second children was 20px. However, there was no gap between the second child (160 to 260) and the third child (60 to 160); they touched. This is because the second child's margin is on its visual right side, which is toward the first child.

With gap: 20px, the first child's right edge was at 400, aligned with the parent's right edge. Because gap is placed between children regardless of direction, reversing the order does not require any changes to the CSS.

Stacking vertically, top and bottom margins add together

When I stacked children vertically with flex-direction: column and gave each child margin: 20px 0 (20px above and below), the space between children was 40px. The y positions were 20 for child 1, 110 for child 2, and 200 for child 3. As stated in the CSS Flexbox specification, margins between flex items do not collapse. The bottom margin and the top margin are added together.

When the same children were placed in a parent with display: block, the top and bottom margins collapsed, and the space between children was 20px. The parent height was 190px. Because the first child's margin-top is not inside the parent, the y positions started at 0 and were 0, 70, and 140. The reason the spacing doubles when you change a block vertical layout to flex is this margin behavior. With gap: 20px, the space between children in a vertical flex layout was 20px, and the y positions were 0, 70, and 140.

Using gap and margin together, and negative and percentage values

gap and margin are added together. With gap: 20px on the parent and margin-right: 10px on the children, the x positions were 0, 130, and 260, so the space between children was 30px.

You cannot write a negative value for gap. The specification also says negative values are invalid. When I wrote gap: -10px, the computed value stayed at the initial value normal, and the children were placed at 0, 100, and 200 with no space between them. If you want children to overlap slightly, give them margin-right: -10px. In that case, the x positions were 0, 90, and 180, so each child overlapped the next by 10px.

You can also use percentages for gap. As described on MDN's gap page, a percentage is a ratio of the parent's content box size. With gap: 5% on a 400px-wide parent, the space between children was 20px.

When I stacked children vertically with row-gap: 10%, the space between them was 0 if the parent's height was not set (y positions were 0, 50, and 100). When I set height: 300px on the parent, the space between children became 30px (y positions were 0, 80, and 160). The specification's percentage section states that when a flex container's size depends on its contents, percentages are treated as 0. If you want to set vertical spacing as a percentage, define the parent's height explicitly.

​Posted by 86