CSS Flexbox Generator
Pick flex settings, watch the boxes move, copy the CSS
How to Build a Flexbox Layout, Step by Step
Set up the container
Pick display, flex-direction, flex-wrap, justify-content, align-items and align-content, and set the row and column gap. Or start from one of the ready-made layouts, such as the centered box or the navbar. The container settings are the heart of flexbox: they decide which way items flow and how leftover space is shared.
Adjust individual items
Use the + and - buttons to have between 1 and 12 items, click one in the preview or in the Items list, and change its flex-grow, flex-shrink, flex-basis, order or align-self. The item properties decide how each box claims or gives up space; MDN has a good page on flex-basis if the numbers feel abstract.
Copy the code
Switch between the CSS and HTML tabs and click Copy, or download the CSS as flexbox.css. Only the values you changed are written unless you ask for defaults too.
.container, and a box only gets its own .item-N class when you changed one of its properties..item rule only gives the boxes their look, so you can swap it for your own.Frequently Asked Questions
How do I build a flexbox layout with this generator?
Set the container first: flex-direction, flex-wrap, justify-content, align-items and the gap. Then click a numbered box, either in the preview or in the Items list, and give it its own flex-grow, flex-shrink, flex-basis, order or align-self. The preview redraws as you change things, and the Code panel shows the CSS and HTML you need to get the same layout.
Is the preview built from the same CSS I copy?
Yes. The preview and the code come from the same settings, so the boxes you see are laid out by exactly the declarations in the CSS tab. The only extra things in the preview are the dashed outline around the container and the orange outline on the selected box, and outlines never move anything.
Why is a property missing from the generated CSS?
Values that match what the browser already does are left out to keep the code short, so you will not see flex-wrap: nowrap or flex-grow: 0 unless you changed them. Tick "Include default values too" if you want every property written out, for example to document a layout or to override styles coming from elsewhere.
Why does align-content do nothing?
align-content spaces out whole lines of items, so it only kicks in when there is more than one line. Set flex-wrap to wrap, make sure the items cannot all fit on one row (add items or give them a flex-basis), and give the container some extra height. With a single line, use align-items instead.
What is the .item rule at the bottom of the CSS?
It is only the look of the demo boxes: padding, a light blue background, a border and the font. It is in the output so that pasting the CSS and HTML into a blank page gives you the same picture as the preview. Replace it with your own styles; the flex settings live in .container and the .item-N rules.
When should I use Flexbox instead of CSS Grid?
Flexbox works along one direction at a time, which suits navbars, button rows, toolbars, centering something, or a list of cards that wraps. Grid places items in rows and columns at once, so it is the better fit for full page layouts or galleries where columns must line up. Plenty of pages use both: Grid for the outer layout and Flexbox inside each area.
Is anything I build here sent or kept anywhere?
No. The layout is worked out in your browser and nothing is sent to our server. Nothing is kept either, so reloading the page starts again from the default layout.
Related Tools
CSS Minifier
Minify and compress CSS code
CSS Beautifier
Beautify and format CSS code
CSS Formatter
Format and organize CSS code
CSS Fixer
Fix broken CSS automatically, repair syntax errors, missing semicolons, and malformed styles
CSS Compressor
Compress and minify CSS code
CSS Autoprefixer
Automatically add vendor prefixes for cross-browser compatibility