Free CSS Flexbox Generator
The CSS Flexbox Generator lets you build a flex layout visually and copy the generated CSS. Adjust direction, wrapping, alignment and gaps, and watch the preview update instantly.
The grid equivalent is the CSS Grid Generator, and the sizing within either comes from the Aspect Ratio Calculator and PX to REM / EM Converter. For the visual properties on the items, the CSS Box Shadow Generator is the companion.
How to use the Flexbox Generator
- 1 Choose the flex direction and whether items should wrap.
- 2 Set the main-axis and cross-axis alignment.
- 3 Adjust the gap between items.
- 4 Copy the generated declarations, or inspect the live preview to check the behaviour at different widths.
The properties that matter
Most flexbox layouts need only four declarations: direction, wrapping, and the two alignment properties. The remaining properties exist for the cases where that is not enough, and reaching for them first is usually a sign the container should be a grid instead.
The behaviour worth internalising is that flexbox distributes free space. If items overflow, there is no free space to distribute and the alignment properties appear to do nothing. Setting a gap and allowing wrapping while the container can grow is what makes the distribution meaningful.
- Direction determines which axis each alignment property affects.
- `flex: 1` makes an item grow to fill available space.
- The default minimum size is the content size, not zero.
- Gap replaces margin hacks between items.
Frequently asked questions
What is the difference between `justify-content` and `align-items`?
`justify-content` distributes items along the main axis, which is horizontal in a row and vertical in a column. `align-items` positions them along the cross axis. Because the axes swap with the direction, the same property has a different visual effect in a row and a column — the single most confusing thing about flexbox.
Why does my flex item refuse to shrink?
Because flex items have a minimum size that defaults to their content size rather than zero. A long unbroken word or a fixed-width child prevents the item from shrinking below that intrinsic width, which causes overflow. Setting the minimum size to zero is the standard fix, and it is the reason flexible layouts with text inside need explicit attention.
When should I use flexbox rather than grid?
Flexbox lays out items along a single axis, distributing space among them, and it handles unknown sizes well because items can grow and shrink. Grid defines a two-dimensional structure of rows and columns and places items into it. Use flexbox for a toolbar or a row of cards, and grid for a page layout or anything where rows must align.
Related Developer Tools
You might also need
Tools from other categories that people pair with the Flexbox Generator: