Flexbox Playground / Generator
Toggle every flexbox container and item property against a live preview and copy working CSS — entirely on this device.
.container {
display: flex;
gap: 12px;
}—
<div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div>
Do it offline:$ cat flexbox.css >> styles.css
How to use it
- Set the container first — display, direction, wrap, both alignment properties and the gaps are labeled control groups.
- Select an item in the preview, by click or arrow keys, to give it order, grow, shrink, basis or align-self overrides.
- Load a preset chip as a starting point; your next edit makes the state yours.
- Copy each block on its own, copy all CSS at once, download flexbox.css, or share the state as a link.
Flip flexbox container and item properties from labeled controls and a real DOM preview restyles instantly — the layout is the browser's own, not a canvas drawing of it. Container CSS, per-item CSS and a minimal HTML snippet stay in sync with every change, ready to copy block by block or all at once, and six presets cover the classic layouts, centering a div included.
How it works
Flexbox runs on two axes. The main axis follows flex-direction — row runs along the line of text, column stacks — and justify-content distributes items along it. The perpendicular cross axis belongs to align-items, and to align-content once wrapping produces multiple lines. The axes are logical: direction: rtl or a column direction moves where start points without renaming a single property.
The famous overflow gotcha is specified behavior: css-flexbox-1 gives every flex item an automatic minimum size, so min-width defaults to auto and shrinking stops at the item's min-content width, whatever flex-shrink says. An unbreakable word sets that floor at its own full width. min-width: 0 on the item removes the floor and lets it shrink normally.
Examples
Preset: center a div.container {
display: flex;
justify-content: center;
align-items: center;
}justify-content: center places the item at the middle of the main axis; align-items: center does the same on the cross axis. Set only one and the box centers in one direction while hugging the start of the other.
Column, stretch, 5 items.container {
display: flex;
flex-direction: column;
gap: 12px;
}Every jittered item widens to the container's full inline size the moment the axis turns vertical — align-items: stretch ignores intrinsic width. Stretch is the initial value, so the copied CSS omits it.
RTL with flex-start.container {
display: flex;
gap: 12px;
direction: rtl;
}The same justify-content: flex-start now packs items against the right edge: flex alignment is logical, so start follows the writing direction. Only direction: rtl needs serializing — flex-start stays implicit as the initial value.
Preset: min-width auto<div class="container">
<div class="item">1</div>
<div class="item">pneumonoultramicroscopicsilicovolcanoconiosis</div>
<div class="item">3</div>
</div>The wide item keeps its default flex-shrink: 1 yet the row overflows: shrinking cannot pass the automatic minimum size — the width of that unbreakable word. min-width: 0 on the item restores expected shrinking.
Every example on this page is executed against the same engine that powers the tool, on every build.
Gotchas worth knowing
Jitter never reaches the CSS
The varied preview sizes are deterministic scaffolding keyed to item index. Toggle jitter and the serialized output does not change by a byte — real sizes belong to your content.
Fifty items is the ceiling
The count stepper stops at 50, and restored or shared states clamp to the same bound — which keeps every preview restyle instant.
align-content waits for a second line
It spaces the lines themselves, so on a single line changing it moves nothing. The declaration still serializes, ready for when wrapping bites.
One gap value or two
Equal row and column gaps collapse to shorthand, gap: 16px; unequal pairs print both, row first, as gap: 12px 24px. Zero gaps are omitted entirely.
flex-shrink: 0 overflows honestly
An item forbidden to shrink eventually pushes past the container; the preview scrolls and lights an overflow indicator rather than hiding the excess.
Negative order, one-decimal factors
order: -1 pulls an item ahead of the default 0 crowd without touching the HTML. Grow and shrink factors print with at most one decimal — 0.5, never 0.50.
A bare 0 is a valid flex-basis
flex-basis: 0 sizes an item purely from its grow factor and is the one length allowed to omit its unit — the holy-grail preset leans on it.
Container queries stay out of scope
The preview answers to its own pane only; @container rules and cqw units are not simulated. What serializes is plain flexbox.
Common errors
`banana` is not a valid flex-basis — use a length (px, em, rem, %, vw, vh, ch, ex, vmin, vmax), `auto`, `content` or `0`.- The one free-text field accepts the flex-basis grammar and nothing else; the diagnostic repeats your token, lists every accepted form, and the last good output holds while you fix it.
`200` needs a unit — try `200px` or `25%` — only `0` can omit its unit.- A bare number is the commonest near-miss, so the suggestion reuses your own value with units attached. Zero is the single exception the grammar allows.
flex-basis is longer than the 64-character limit for this tool — shorten the value.- Values past 64 characters are refused whole, never trimmed — silent truncation could turn one valid length into a different one. Any single flexbox length fits comfortably.
FAQ
How do I center a div with flexbox?
Give the parent display: flex, then justify-content: center for the main axis and align-items: center for the cross axis. The center-a-div preset loads exactly that state; one property alone centers in one direction only.
What is the difference between justify-content and align-items?
justify-content distributes items along the main axis, the direction items flow in; align-items aligns them along the perpendicular cross axis. In a row container that means horizontal versus vertical — flip flex-direction to column and the two swap visual duties.
Should I use flexbox or grid?
Flexbox distributes content along one axis at a time and lets item sizes drive the layout; grid defines rows and columns up front, placing items on both axes at once. Navbars, toolbars and card rows lean flexbox; page scaffolds lean grid. The two compose freely.
Keyboard shortcuts
- ⌘/Ctrl + C
- Copy all CSS while nothing is selected.
- ⌘/Ctrl + Shift + C
- Copy all CSS even over an active selection.
- ⌘/Ctrl + S
- Download the current CSS as flexbox.css.
- ? or ⌘/Ctrl + /
- Open the keyboard-help dialog.
- t
- Cycle the color theme.
- Esc
- Close the topmost dialog.