CSS Flexbox Generator
Visually configure every flexbox property with a live preview. Pick a preset to start fast, then tweak container and per-item settings — copy the generated CSS or HTML when you're done.
display
Block-level vs inline-level flex container.
flex-direction
Sets the main axis and item flow direction.
flex-wrap
Whether items wrap onto new lines when they overflow.
justify-content
Distributes items and free space along the main axis.
align-items
Aligns items along the cross axis within a flex line.
align-content
Distributes space between flex lines (only when wrapping).
⚠ Enable flex-wrap: wrap to activate align-content.
gap
Space between flex items. Applies to both row and column gaps.
Items
Click a number to select. Click × to remove. Max 10 items.
Editing item
align-self
flex-grow
⚠ flex-grow has no effect when justify-content distributes all free space.
How much this item grows relative to siblings when extra space is available.
flex-shrink
⚠ flex-shrink has no effect when flex-wrap is enabled (items wrap instead of shrink).
How much this item shrinks when the container is too small.
flex-basis
Initial size before flex-grow/shrink applies.
Shorthand
width
0 = not set
height
0 = not set
order
Visual order (not DOM order). Negative values move item earlier.
Container properties
- display
- flex | inline-flex
- flex-direction
- row | row-reverse | column | column-reverse
- flex-wrap
- nowrap | wrap | wrap-reverse
- justify-content
- flex-start | flex-end | center | space-between | space-around | space-evenly
- align-items
- stretch | flex-start | flex-end | center | baseline
- align-content
- stretch | flex-start | flex-end | center | space-between | space-around
- gap
- Shorthand for row-gap + column-gap
Item properties
- flex-grow
- Growth factor (default: 0)
- flex-shrink
- Shrink factor (default: 1)
- flex-basis
- Initial size before grow/shrink (default: auto)
- flex
- Shorthand: grow shrink basis
- align-self
- Override align-items for this item
- order
- Visual order (default: 0)
Flex shorthand cheatsheet
- flex: 1
- = flex: 1 1 0% (grow + shrink)
- flex: auto
- = flex: 1 1 auto
- flex: none
- = flex: 0 0 auto (rigid)
- flex: 0 0 200px
- Fixed 200px, no grow/shrink
Common patterns
- Perfect centering
- jc: center; ai: center
- Equal columns
- each: flex: 1 1 0
- Fixed sidebar
- sidebar: flex: 0 0 Npx; main: flex: 1
- Push last item
- last child: margin-left: auto
What is CSS Flexbox?
CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that lets a container control the size, direction, and alignment of its children. One-dimensional means Flexbox works along a single axis at a time — either a row or a column — giving you precise control over how items flow in that direction. When you need to control rows and columns simultaneously, CSS Grid is the better tool; in practice you'll often use both: Grid for the overall page structure, Flexbox for the components within it.
Set display: flex on a parent element and it becomes a flex container — its direct children become flex items that respond to the container's rules. Use display: inline-flex when you need the container itself to behave like an inline element while still using flexbox internally. Flexbox is supported in every modern browser (Chrome, Firefox, Safari, Edge) without vendor prefixes and has been baseline available since 2017.
How to use this generator
Start with a preset to get a working layout instantly, then fine-tune from there. The Container tab controls the parent element: set the main axis with flex-direction, distribute space along that axis with justify-content, and align items on the perpendicular axis with align-items. Switch to the Items tab to configure individual children — click any numbered box in the preview to jump straight to that item. Toggle between All and Changed in the code panel to see either the complete snapshot or only the properties you've modified.
Key flexbox properties explained
- flex-direction — Sets the main axis:
row(left-to-right),row-reverse,column(top-to-bottom), orcolumn-reverse. Everything else — justify-content, align-items, flex-basis — is relative to this axis. - justify-content — Distributes free space along the main axis.
space-betweenpushes items to the edges;centergroups them in the middle;space-evenlyadds equal gaps all around. Note: when this property distributes all available space,flex-growhas no effect. - align-items — Aligns items on the cross axis (perpendicular to the main axis) within a single flex line.
stretchfills the container height;centercenters vertically in row mode;baselinealigns text baselines across items of different sizes. - flex-wrap — Controls whether items overflow onto new lines.
nowrapforces everything onto one line (items may shrink or overflow);wrapenables multi-line layouts. Enabling wrap also activatesalign-content, which controls spacing between the lines. - gap — Adds consistent space between flex items without using margins. Works uniformly between all pairs of adjacent items.
- flex-grow — Defines how much a child expands when extra space is available.
0means no growth;1on all items divides remaining space equally;2on one item gives it twice the share of a1neighbour. - flex-shrink — Controls how a child shrinks when the container is too small.
1(default) lets items shrink proportionally;0prevents shrinking entirely. Only active whenflex-wrap: nowrap. - flex-basis — Sets the initial size before the grow/shrink calculation runs. Behaves like
widthin row mode andheightin column mode. Settingflex-basis: 0withflex-grow: 1makes items share all available space equally, regardless of content. - align-self — Overrides the container's
align-itemsvalue for a single flex item. Useful when one child needs to break out of the cross-axis alignment applied to all its siblings — for example, pinning one card to the bottom of a row while the others stretch. - order — Changes the visual rendering order of items without altering DOM order. All items default to
0; items with a lower value appear first, higher values appear last. Negative values are valid. Note that reordering withorderis purely visual — keyboard navigation and screen readers still follow the DOM order, so use this carefully in accessible interfaces.
align-content vs align-items
align-items aligns items within a single flex line. align-content distributes space between multiple flex lines — it only has an effect when flex-wrap is wrap or wrap-reverse and there is more than one line. Think of it like justify-content but for the cross axis at the line level.
A common gotcha: setting align-content on a flex container that uses flex-wrap: nowrap (the default) produces no visible effect — the property is silently ignored because there is only ever one line. If you set align-content and nothing changes, check that wrapping is enabled and that your container actually has enough items to overflow onto multiple lines. The align-content control in this generator is greyed out for exactly this reason — it only activates when wrap is on.
Frequently asked questions
justify-content controls alignment along the main axis (the direction set by flex-direction), while align-items controls alignment on the cross axis (perpendicular to the main axis). In a row layout, justify-content is horizontal and align-items is vertical — switch to column and they swap.
flex: 1 is shorthand for flex: 1 1 0% — the item can grow (flex-grow: 1), shrink (flex-shrink: 1), and starts from a calculated base size of zero (flex-basis: 0%). This forces all siblings with flex: 1 to share available space equally, regardless of their content size. Two other common values: flex: auto expands to flex: 1 1 auto (grows and shrinks but respects natural content size), and flex: none expands to flex: 0 0 auto (rigid — no growing or shrinking).
flex-basis sets the starting size of a flex item before remaining space is distributed according to flex-grow and flex-shrink. Unlike width, it respects the flex axis — in a column layout it behaves like height. Setting flex-basis: 0 with flex-grow: 1 makes all items share space equally regardless of content size.
display: flex, then add align-items: center to center vertically and justify-content: center to center horizontally. Use both together to center a div in the middle of its container — this is the standard CSS centering solution. Try the Centered preset in this generator to see it instantly.
flex: 1 (shorthand for flex: 1 1 0). Setting flex-basis to 0 forces all items to start from zero and divide available space equally, regardless of how much content each column contains. For three equal columns with a gap, set the container to display: flex; gap: 1rem and each child to flex: 1. Use the Equal Cols preset in this generator to apply this pattern instantly.
min-width: auto — the browser default for flex items — which prevents items from shrinking below their natural content size. Add min-width: 0 to the overflowing item to allow it to shrink correctly. Images inside flex items often need max-width: 100% for the same reason. In a column layout, min-height: 0 has the same effect on the cross axis.