CSS Flexbox Generator

Lay out a flex container with real items to watch, and copy only the properties that differ from the browser's defaults.

Enable JavaScript to change the layout; the CSS below is for the defaults.

Direction

Row lays the items along the inline axis, which follows the text direction.

Along the main axis, whichever axis the direction made that.

Across the main axis. stretch is the default and the reason items match heights.

Wrap

nowrap is the default, and the usual reason items squash instead of moving to a new line.

Only does anything once the lines wrap, so it is left out of the CSS until they do.

Gap replaced the margin tricks. It applies between items, never outside them.

Left empty it matches the gap above. A number here writes the two values separately.

How much of the leftover space each item takes. 0 means none.

1 is the default, and why items shrink below their content width.

The starting size before growing or shrinking. auto means the content's own size.

Item heights

Uneven heights are what make align-items visible at all.

1
2
3
4
flex.css
.flex-container {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 16px;
}

Lay out a flex container against real items, and copy only the properties that are actually doing something.

How to use

  1. Set the direction first. It decides which axis is the main one, and every other property is described in terms of that.
  2. Set justify content for the main axis and align items for the cross axis. The uneven item heights in the preview are what make align items visible.
  3. Turn wrapping on if the items should move to a new line rather than squash. Align content appears in the CSS only once they can.
  4. Use gap rather than margins. It applies between items and never outside them, which is what the old negative-margin tricks were working around.
  5. Copy the CSS. Anything left at the browser’s own default is left out, because writing flex-wrap: nowrap teaches that you need it.

Example

.flex-container {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: 16px;
}

Four declarations, three of them doing work. flex-direction: row is absent because that is already what flex does.

Pitfalls

  • nowrap is the default. Items squashing below their content width instead of moving to a new line is almost always this, plus flex-shrink: 1, which is also a default.
  • flex: 1 is not flex-grow: 1. The shorthand sets basis to 0%, so items ignore their content width; flex: 1 1 auto keeps it. That difference is the single most common flexbox surprise.
  • A flex item cannot shrink below its content’s minimum size unless you say so. min-width: 0 on the item is the fix for a long word or a pre block blowing out a row.
  • align-content does nothing on a single line. If it appears to be ignored, wrapping is off.
  • align-items: stretch is why two cards in a row match heights, and why an image in a flex row can look distorted. align-items: flex-start stops both.
  • gap on flex is supported in every current browser, but Safari only added it in 14.1. If you support older Safari, the fallback is margins on the items and a negative margin on the container.
  • Percentage flex-basis resolves against the container’s main size, which is undefined in some height: auto columns. It silently behaves like auto there.
  • Flexbox is one-dimensional. If you are fighting to align things in rows and columns, that is grid’s job.

Compatibility

Flexbox is supported unprefixed by every browser in use, since 2015. gap in flex containers needs Chrome 84, Firefox 63 or Safari 14.1; everything else here is older than that. The generated CSS uses no prefixes, because the prefixed syntax targets a version of the specification that no shipping browser uses any more. The tool runs entirely in your browser.

Frequently asked questions

Why are my items not wrapping?
flex-wrap: nowrap is the default. Set it to wrap.
What is the difference between justify and align?
Justify works along the main axis, align across it. Change the direction to column and the two swap places, which is the part that catches everyone.
Should I use flexbox or grid?
Flexbox for one direction: a row of buttons, a nav bar, a card footer. Grid when both rows and columns matter.
Why is one item wider than the rest?
Either its content is wider and nothing is shrinking it, or flex-grow is on and it has more space to take.
How do I make equal width columns?
flex: 1 1 0% on every item, which ignores content width. With auto instead of 0% the widths follow the content.
Weekly drops

New tools, when there are new tools

One email when something worth using ships. No schedule to fill, so no filler.

Your address goes nowhere else, and one click unsubscribes.