My Tool Studio
CSS

CSS Flexbox Generator

This CSS flexbox generator lets you set container properties from dropdowns, click any box to give it its own flex-grow, flex-shrink, flex-basis, order and align-self, and copy the finished CSS, HTML or Tailwind classes. The preview is a live flex container, so every change moves the boxes the way a browser would. Nine layout buttons load common patterns in one click, from center a div and a navbar to a media object, wrapping tags and pricing columns. Separate row and column gaps, random item sizes and a text content mode show how wrapping and baseline alignment behave. Front-end developers use it to settle a layout before writing it. Say a nav bar keeps squashing its logo: select the logo, set flex-shrink to 0 and watch it hold its width.

Always freeNo sign upRuns in your browser
Layouts:

Click a box to edit its flex-grow, flex-shrink, flex-basis, order and align-self.

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-items: stretch;
  gap: 12px;
}

The preview boxes have demo widths and heights that stand in for real content. The code only contains flex properties.

Container

gap12px
Separate row and column gap

How to use

01

Set up the container

Pick a layout button or set display, flex-direction, flex-wrap, justify-content, align-items and align-content from the dropdowns. Drag the gap slider, or switch on separate row and column gaps, and set Item count from 1 to 20.

02

Click a box to edit it

Clicking a box in the preview, or choosing it in Selected item, opens its controls: flex-grow, flex-shrink, flex-basis, order, align-self and a label. Reset item puts it back to the defaults.

03

Copy the code

Switch between the CSS, HTML, CSS + HTML and Tailwind tabs under the preview, then copy or download. Copy share link saves the container and every item in a URL.

Why CSS Flexbox Generator

Flexbox property cheat sheet: container vs item

Flexbox properties split into two groups. Container properties go on the parent and affect all children. Item properties go on a child and only change that child. Mixing them up is the most common reason a flex rule seems to do nothing.

PropertyGoes onWhat it doesDefault
display: flex / inline-flexContainerTurns direct children into flex itemsn/a
flex-directionContainerSets the main axis: row or column, normal or reversedrow
flex-wrapContainerLets items move to new lines instead of shrinkingnowrap
justify-contentContainerSpaces items along the main axisflex-start
align-itemsContainerAligns items on the cross axis within a linestretch
align-contentContainerSpaces lines on the cross axis when wrappingnormal
gapContainerSpace between items, not at the outer edges0
flex-growItemShare of free space the item takes0
flex-shrinkItemHow much the item gives up when space runs out1
flex-basisItemStarting size on the main axisauto
orderItemVisual position among siblings0
align-selfItemOverrides align-items for one itemauto

Common flexbox layout recipes

Each of these matches one of the layout buttons above the preview. Load it, then click the items to see which item rules make it work.

  • Center a div: .container { display: flex; justify-content: center; align-items: center; }
  • Navbar: .container { display: flex; justify-content: space-between; align-items: center; } with logo, links and a button as children.
  • Equal cards: .container { display: flex; flex-wrap: wrap; gap: 16px; } and each card gets flex: 1 1 180px so cards share a row and wrap when narrow.
  • Sticky footer: .container { display: flex; flex-direction: column; min-height: 100vh; } and the main area gets flex: 1 1 auto so it pushes the footer down.
  • Sidebar and content: the sidebar gets flex: 0 0 160px so it never shrinks, and the content gets flex: 1 1 0 to fill the rest.
  • Push the last item right: give a spacer item flex-grow: 1, or put margin-left: auto on the last item.

How flex-grow splits free space: a worked example

Take a 600px wide row with no gap and two items, both with flex-basis: 200px. The items start at 200px each, which leaves 200px of free space.

Give item A flex-grow: 1 and item B flex-grow: 2. The free space is split into three shares. A gets one share, about 66.7px, and ends up about 266.7px wide. B gets two shares, about 133.3px, and ends up about 333.3px wide.

flex-shrink works the other way. If the items would overflow, each one gives up space in proportion to its shrink value multiplied by its basis. An item with flex-shrink: 0 keeps its basis and lets its siblings shrink instead, which is how the sidebar layout keeps a fixed width.

A common shortcut is flex: 1, which means flex: 1 1 0%. Because the basis is 0, all free space is shared by grow values alone, so items with flex: 1 end up equal width no matter how much text they hold.

Common questions

What does the flexbox generator copy to my clipboard?
The CSS tab gives a .container rule with display, flex-direction, flex-wrap, justify-content, align-items, gap and align-content when you change it. Each item you edited gets its own .item-N rule with flex, order or align-self. The HTML tab gives the matching markup, CSS + HTML joins the two, and Tailwind writes the same layout as utility classes.
How do I set flex-grow on just one flex item?
Click that box in the preview or pick it in the Selected item list, then type a number in flex-grow. The item takes a share of the free space in proportion to its grow value. The output writes it as a flex shorthand on that item only.
Why does justify-content stop working when an item has flex-grow?
Growing items soak up the free space on the main axis, so justify-content has nothing left to distribute. The tool shows a note under the preview when this happens. Set flex-grow back to 0 or use a margin such as margin-left: auto if you need both.
Why does align-content do nothing in my flex container?
align-content only spaces out lines, so it needs flex-wrap set to wrap and enough items to form more than one line. On a nowrap container there is a single line and nothing to move. The generator warns you when align-content is set while wrap is off.
What is the difference between flex-basis and width on a flex item?
flex-basis sets the starting size along the main axis before grow and shrink are applied. When it is auto, the browser falls back to width in a row or height in a column. When basis has a value, it wins over width for that axis.
Does the flexbox order property change the HTML order for screen readers?
No. order only changes the visual position, so screen readers and the Tab key still follow the source order. Use it for small visual tweaks, and change the HTML itself when the reading order matters.
Why does justify-content control vertical spacing in a flex column?
justify-content always works on the main axis, and flex-direction decides which way that axis runs. In a column the main axis goes top to bottom, so justify-content spaces items vertically and align-items handles the horizontal. Switch the direction dropdown to see the swap.
How do I center a div with flexbox in two lines of CSS?
Put display: flex on the parent, then justify-content: center and align-items: center. The Center a div button sets exactly that. The parent needs a height larger than the child, or there is no vertical space to center within.
When should I use inline-flex instead of flex?
Use inline-flex when the container itself should sit inside a line of text or next to other inline elements, like a button with an icon. It lays out its children the same way, but the container only takes the width it needs instead of the full row.
Are the box sizes in the flexbox preview part of the copied CSS?
No. The boxes have demo widths and heights that stand in for real content, and Random item sizes changes them to show how wrapping behaves. Text content swaps the boxes for text of different lengths and sizes. The copied code only contains flex properties, so your real content decides the sizes.

More CSS tools

View all