Flexbox Generator
Build a CSS flexbox layout visually: set direction, wrap, justify-content, align-items, gap and each item’s grow, shrink and basis. Copy the CSS.
Container
Spacing along the main axis.
Placement across the cross axis.
Only matters when wrapped items fill two or more lines.
Items
Preview items have different content heights so you can see align-items. The preview height is a viewing aid and is not part of the CSS. Item 1 has the thicker outline.
CSS
.container {
display: flex;
gap: 12px;
}What Flexbox Generator Does
This flexbox generator sets up a flex container and up to 12 items, then writes the CSS: display: flex, plus whichever of flex-direction, flex-wrap, justify-content, align-items, align-content and gap differ from their defaults, and per-item flex, align-self and order. Properties already at their default value are left out unless you tick the option to include them.
The flex shorthand is written in its shortest equal form. An item with grow 1, shrink 1 and a 0% basis becomes flex: 1. Grow 1, shrink 1 and auto becomes flex: auto, and 0 0 auto becomes flex: none. When every item has the same settings they are written once on .item instead of one rule per item.
How to Use Flexbox Generator
- Pick flex-direction, flex-wrap, justify-content, align-items and align-content for the container
- Set the row and column gap and the number of items, from 1 to 12
- Choose an item under Edit item and set its flex-grow, flex-shrink, flex-basis, align-self and order
- Watch the preview update; the item you are editing has the thicker outline
- Press Copy to take the CSS, or tick the option to include properties at their default value
Formula Used by Flexbox Generator
How flex-grow shares leftover space
Final size = basis + free space × (grow ÷ sum of grow)
- basis
- The item’s flex-basis, in px
- free space
- Container size − sum of the bases − gaps, when positive
- grow
- The item’s flex-grow value
Worked example
A 600 px row with three items at basis 100 px and grow 1, 2 and 1, no gap.
- Free space: 600 − 3 × 100 = 300 px
- Shares: 300 × 1/4 = 75, 300 × 2/4 = 150, 300 × 1/4 = 75
- Widths: 100 + 75, 100 + 150, 100 + 75
Result: Items of 175, 250 and 175 px.
How flex-shrink removes overflow
Reduction = overflow × (shrink × basis) ÷ sum of (shrink × basis)
- overflow
- Sum of the bases − container size, when positive
- shrink × basis
- The scaled flex shrink factor from the CSS Flexbox spec
Worked example
A 400 px row with three items at basis 200 px and shrink 1, 1 and 2, no gap, and nothing that stops an item from shrinking.
- Overflow: 3 × 200 − 400 = 200 px
- Scaled factors: 200, 200 and 400, sum 800
- Reductions: 200 × 200/800 = 50, 50 and 200 × 400/800 = 100
- Widths: 200 − 50, 200 − 50, 200 − 100
Result: Items of 150, 150 and 100 px.
flex shorthand equivalents
The forms this generator writes, with the three values they stand for.
| You write | It means (grow shrink basis) |
|---|---|
| flex: none | 0 0 auto |
| flex: auto | 1 1 auto |
| flex: initial | 0 1 auto (the default) |
| flex: 2 | 2 1 0% |
| flex: 10em | 1 1 10em |
| flex: 1 30px | 1 1 30px |
| flex: 2 2 10% | 2 2 10% |
Source: MDN — flex
Where justify-content puts three 100 px items in a 600 px row
Left edge of each item, in px from the start of the row. Free space is 300 px; no gap.
| justify-content | Item 1 | Item 2 | Item 3 |
|---|---|---|---|
| flex-start | 0 | 100 | 200 |
| center | 150 | 250 | 350 |
| flex-end | 300 | 400 | 500 |
| space-between | 0 | 250 | 500 |
| space-around | 50 | 250 | 450 |
| space-evenly | 75 | 250 | 425 |
Source: MDN — justify-content
How to Read Your Result
Main axis and cross axis
flex-direction sets the main axis: horizontal for row, vertical for column. justify-content works along that axis and align-items works across it. Switch from row to column and the same two settings swap which way they act. That swap is the usual reason a centering rule appears to stop working.
flex: 1 against flex: auto
flex: 1 starts every item from a 0% basis, so the free space is the whole container and the items end up equal width whatever their content. flex: auto starts from each item’s own size, so longer content keeps a longer item and only the leftover is shared. Pick flex: 1 for equal columns and flex: auto when content should still count.
align-content only counts with wrapping
MDN says align-content has no effect on single-line flex containers. Set flex-wrap to wrap, make the container taller than the lines, and it spreads the lines across the cross axis. With nowrap there is only one line, so align-items does the placing.
Items refusing to shrink
By default a flex item will not shrink below its min-content size, which MDN notes can be changed with min-width or min-height. Long words, wide images and fixed-width children are the usual cause of a row that overflows even though flex-shrink is 1.
Limitations & Accuracy Notes
- The size examples ignore min-content limits. A real browser freezes an item that hits its minimum or maximum size and shares the rest among the others, so a row with stubborn content can differ from the arithmetic above.
- When the flex-grow values add up to less than 1, the spec hands out only that fraction of the free space, so a single item with grow 0.25 takes a quarter of the free space, not all of it.
- The generator does not write min-width, max-width, flex-flow, inline-flex or nested containers, and the CSS has no width or height for the container. The preview height is only there so vertical alignment is visible.
- The preview items have different content heights on purpose, so align-items has something to show. Your own items will size to your own content.
- The CSS Flexbox spec says order and the reverse directions affect only the visual rendering, leaving speech and keyboard navigation in source order. Keep that in mind before relying on them.
Frequently Asked Questions
What is the difference between justify-content and align-items?
justify-content spreads items along the main axis, and align-items places them across the cross axis. With flex-direction: row the main axis is horizontal; with column the two swap. Change the direction in this generator and watch the same settings move to the other axis in the preview.
What does flex: 1 mean?
MDN documents a single unitless number as shorthand for that number, 1 and 0%. So flex: 1 means flex-grow 1, flex-shrink 1 and flex-basis 0%. Items with it start from a zero basis and split the free space equally, whatever their content width. This tool writes flex: 1 when your settings match that.
What do flex-grow, flex-shrink and flex-basis do?
flex-basis is the starting size along the main axis. flex-grow shares out any positive leftover space, and flex-shrink shares out the shortfall when items do not fit. The initial values are 0 1 auto, so items do not grow, can shrink, and start at their normal size.
How do I center something with flexbox?
Put display: flex, justify-content: center and align-items: center on the container. Horizontal centering works at once. For vertical centering the container needs more height than the item, so give it a height or min-height. Pick center in both lists here to see the result.
What is the difference between flex: auto and flex: none?
MDN lists flex: auto as 1 1 auto and flex: none as 0 0 auto. An auto item grows and shrinks from its normal size. A none item is fully inflexible and keeps its width or height. Set grow and shrink on an item here and the tool writes whichever keyword matches.
When does align-content have an effect?
MDN says align-content has no effect on single-line flex containers, meaning those with flex-wrap: nowrap. It spaces whole lines of wrapped items across the cross axis, and it only shows when the container has extra cross-axis room. Set wrap and a preview height taller than the lines to try it.
Why will my flex item not shrink below its content?
By default a flex item will not shrink below its min-content size, per MDN. To let it get smaller, set min-width or min-height on the item. The CSS from this generator does not add that, so write min-width: 0 on the item yourself when long text or images are forcing the row wider.
References & Further Reading
- MDN — flex — Shorthand expansion, keyword values, min-content behavior
- MDN — justify-content — space-between, space-around and space-evenly definitions
- MDN — align-content — No effect on single-line flex containers; initial value normal
- MDN — gap — Row gap first, then column gap; applies to flex and grid containers
- W3C — CSS Flexible Box Layout Module Level 1 — Resolving flexible lengths: scaled flex shrink factor, frozen items, flex factors below 1