CSS Flexbox Generator
Visually build Flexbox layouts. Live preview + copy CSS or Tailwind classes.
.container {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
flex-wrap: nowrap;
gap: 8px;
}flex flex-row justify-start items-stretch flex-nowrap gap-2Flexbox in 60 seconds
Add display: flex to a container and its children become flex items. Flex items are arranged along a main axis (row or column) and aligned on a cross axis. Five container properties handle most needs: flex-direction, justify-content, align-items, flex-wrap, and gap. Three item properties for fine control: flex-grow, flex-shrink, flex-basis (or the shorthand flex).
flex-direction: rows vs columns
Flexbox is one-dimensional — items go either left-to-right (row) or top-to-bottom (column). The direction determines what the “main axis” is.
- row (default): main axis is horizontal. justify-content moves items left/right.
- row-reverse: items flow right-to-left.
- column: main axis is vertical. justify-content moves items up/down.
- column-reverse: items flow bottom-to-top.
justify-content vs align-items
The most-confused pair in CSS layout. Memorize:
- justify-content: distributes items along the MAIN axis. For a row, that's horizontal.
- align-items: aligns items on the CROSS axis. For a row, that's vertical.
Common values for both:
- flex-start: pack items at the start.
- center: center items.
- flex-end: pack items at the end.
- space-between (justify-content only): equal space between items, none at ends.
- space-around (justify-content only): equal space around each item.
- space-evenly (justify-content only): truly equal space everywhere.
- stretch (align-items only, default): items stretch to fill the cross axis.
- baseline (align-items only): items align by text baseline. Useful for typography.
Common Flexbox patterns
- Center anything:
display: flex; justify-content: center; align-items: center;on the parent. - Equal-width columns:
display: flex;on parent,flex: 1;on children. - Sticky footer: parent is column flex with min-height: 100vh, content has flex: 1, footer has flex: 0.
- Navigation bar: row flex with justify-content: space-between to push logo left and nav right.
- Card grid: row flex with flex-wrap: wrap and gap. Each card has flex: 0 0 calc(33.333% - gap).
Flexbox vs CSS Grid
A common question. Quick answer:
- Flexbox = one-dimensional: a row OR a column. Best for components.
- Grid = two-dimensional: rows AND columns simultaneously. Best for full page layouts.
- Use both: Grid for the page macro layout, Flexbox for the contents of grid cells.
Gotchas: flex-shrink, min-width, content overflow
Flex items have min-width: auto by default — they refuse to shrink below their content size. This causes the classic “text overflow” bug when you add text-overflow: ellipsis to a flex child. Fix: add min-width: 0 to the flex item. Or use overflow: hidden in the right place.
Other gotchas: gap doesn't work in Safari before 14.1 (use margin), flex-wrap creates new flex lines (each line aligns independently with align-items), and flex shorthand flex: 1 means flex: 1 1 0% not flex: 1 1 auto.
For other CSS tools: CSS Gradient Generator, CSS Box Shadow Generator, and Color Palette Generator.