CSS Flexbox Generator

Pick flex settings, watch the boxes move, copy the CSS

Loading the flexbox generator...

How to Build a Flexbox Layout, Step by Step

Step 1

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.

•
flex-direction: row lays items out side by side, column stacks them. The reverse options flip the order.
•
justify-content: spreads items along the main direction (left to right for a row, top to bottom for a column). See the complete guide to flexbox for pictures of every value.
•
align-items: lines items up across the other direction. Give the container a height to see it work on a row.
•
gap: space between items, without margins to clean up on the first or last one.
Step 2

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.

•
flex-grow: how much of the free space a box takes. A box with 2 gets twice the extra space of a box with 1.
•
flex-shrink: how willing a box is to get smaller when there is not enough room. 0 keeps it at its basis, which is handy for sidebars.
•
order and align-self: move one box to another position or line it up differently from the rest, without touching the HTML.
Step 3

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.

•
Class names: the container is .container, and a box only gets its own .item-N class when you changed one of its properties.
•
Demo styles: the .item rule only gives the boxes their look, so you can swap it for your own.
•
Tidy it up: paste the result into the CSS formatter to sort or re-indent it, or shrink it with the CSS minifier. For two-dimensional layouts, try the CSS grid generator.

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.