HandyTools Hub

CSS Flexbox Generator

Design flexbox layouts visually and copy the CSS

Click an item in the preview to set per-item overrides.

 

The flexbox generator lets you build a flex container visually — display, direction, wrap, justification, alignment, and gap — while watching a live preview with 3 to 12 numbered items. You can then override individual items and copy clean CSS for the container and each changed item.

How the Main and Cross Axes Work

Every flexbox decision is relative to two axes. flex-direction sets the main axis, and justify-content distributes items along it. align-items and align-content act on the cross axis, perpendicular to it. That is why setting flex-direction: column appears to swap the two alignment properties: the main axis is now vertical, so justify-content controls vertical placement and align-items controls horizontal.

PropertyAxisAffects
justify-contentmainspacing and position along the row or column
align-itemscrosssingle-line alignment of every item
align-contentcrossspacing between wrapped lines (needs wrap)
gapbothfixed space between items and lines

Worked Example: A Space-Between Navbar

A common header has a logo on the left and a link group on the right, vertically centered. Choose:

  • display: flex
  • justify-content: space-between
  • align-items: center

The generated CSS is:

.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

flex-direction, flex-wrap, align-content, and gap are absent because they sit at their defaults and would change nothing. If you add a 16-pixel gap, the output grows a gap: 16px; line.

Worked Example: Proportional Item Sizing

Select item 2 in the preview and set flex-grow to 2, then select item 1 and set it to 1. With a 600-pixel container and no gap, if each item’s content is about 100 pixels wide, the leftover space is 600 − 300 = 300 pixels. It is split in proportion to the grow factors: item 1 receives one third (100 px) and item 2 receives two thirds (200 px), so their final widths are roughly 200 px and 300 px.

The output emits a rule only for the items you changed:

.item-1 {
  flex-grow: 1;
}

.item-2 {
  flex-grow: 2;
}

flex-shrink and align-self stay out because they were left at their defaults.

Why Some Properties Are Missing from the Output

The generator elides properties that match CSS defaults, so the copied block contains only what actually changes the layout. display is always written; flex-direction: row, flex-wrap: nowrap, justify-content: flex-start, align-items: stretch, align-content: stretch, a zero gap, flex-grow: 0, flex-shrink: 1, and align-self: auto are omitted. A short block is therefore a feature, not a sign that a setting was lost.

When Flexbox Fits and How to Nest It

Flexbox distributes items along a single axis, and it excels at components: navbars, button groups, cards in a row, form rows, and centering a single child. It is the wrong first choice for a full two-dimensional page grid with rows and columns that align across each other — that is what CSS Grid handles. A practical rule: if you are arranging items in one direction and letting them wrap, reach for flexbox; if rows and columns must line up simultaneously, reach for grid. Flexbox aligns items within a line, while grid aligns tracks across the whole container; mixing those mental models is the source of most “why won’t this center” moments.

Beyond the Demo: Nesting and Real Layouts

The preview shows one container, but real pages nest them. A common pattern is a flex row for the page shell, with each column itself a flex column. Build the outer container with display: flex and the sidebar and main area as items, then build the inner column with display: flex; flex-direction: column. The generated .container block applies to whichever element carries that class, so you can paste it into more than one nested element and adjust the selectors.

Making Items Fill the Height

A frequent question is why items do not stretch to the tallest sibling. By default align-items: stretch does exactly that on the cross axis, so if items look short, a child inside may have a fixed height or the container may have no height to distribute. Setting an explicit height on the container, or using align-self: stretch on a single item, usually resolves it. If one item should stay short while its siblings stretch, select it and set align-self to flex-start rather than changing the whole container.

Common Mistakes

  • Reaching for align-content on a single line. It does nothing without wrapping. Add flex-wrap: wrap and shrink the preview to see it engage.
  • Forgetting that flex-grow only distributes leftover space. If items already fill the container, increasing flex-grow cannot create room; you need a larger container or smaller items.
  • Setting flex-shrink: 0 on everything. It stops items squashing, but it can push content past the container edge instead, causing horizontal overflow.
  • Expecting the classes to match your markup. The output uses .container and .item-N; rename them to your real selectors before pasting.
  • Assuming gap pads the edges. gap only adds space between items; it does not create outer breathing room. Use padding on the container for that.

Frequently Asked Questions

Why does changing align-content do nothing to my layout?

align-content only has an effect when items wrap onto more than one line, which requires flex-wrap set to wrap or wrap-reverse and a container small enough to force wrapping. On a single-line row it is ignored by the browser — use align-items instead.

Do justify-content and align-items swap when I set flex-direction to column?

Yes. justify-content always works along the main axis and align-items along the cross axis. Switch to column and the main axis becomes vertical, so justify-content moves items up and down while align-items moves them left and right.

Why is there no flex-shrink value in the generated item rule?

Defaults are omitted to keep the output clean. flex-shrink defaults to 1, so a rule includes it only when you set something other than 1. To hold an item at its size, set flex-shrink to 0 and the line appears.

What units does the gap control use?

The slider and number box write a plain pixel value between 0 and 48, emitted as gap: 12px;. The control does not switch to rem or percentages, so adjust the number after copying if you use relative units.

Can I rename the .container and .item-N classes?

Yes. The generated CSS is a starting point, not a live binding. Rename .container to your real class and .item-3 to whatever selector targets that element; only the property values need to be preserved.

What is the difference between align-items and an item's align-self?

align-items sets cross-axis alignment for every item in the container. align-self is set on one selected item and overrides the container value for that item only, which is useful for centering or stretching a single element while the rest follow the container rule.

Reviewed by the HandyTools Hub team · Last updated