brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: gap in a CSS flexbox replaces margins between items

Css
~/tips/tip-css-flexbox-gap-spacing

.toolbar {

Spacing goes only between items, with no margin on the first or last one.

Before gap worked in flexbox, spacing buttons meant a margin that had to be removed from the last one, plus a negative margin trick for wrapped rows. Now one property handles both directions.

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
}

The first value is the gap between rows, the second the gap between columns. With one value both are the same. The space only goes between items, never on the outside edge, so the toolbar lines up with the rest of the page without any correction.

When items wrap to a second line, the row gap keeps them apart vertically. With margins you would get double spacing or a margin on the last row that pushed the next element down.

All current browsers support it; Safari was last, in version 14.1. The odd thing is detection: @supports (gap: 1rem) returns true in browsers that only knew grid gap, so it cannot tell you whether flex gap works. In practice the old Safari versions without it are rare now, so I’d just use it.

Further reading: MDN: gap

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } @font-face { All Css tips (66) All topics

Link