ToolsHub4u

Build a flexbox container and copy the CSS

Choose a direction, a justification, an alignment, a wrapping mode and a gap. The page writes the container rule and shows how three items behave under it.

  • Live preview
  • Copy-ready CSS
  • Runs locally
Ready Developer & Utility
1Enter
2Adjust
3Copy result

Flexbox Generator

1

Container settings

px
No data yet Live
2

CSS

A row container with space-between justification and a 16px gap.

Live preview

CSS

  • Items will not wrapFlex items shrink by default and stay on one line. On a narrow screen they can become unreadably small.
  • Row directionIn a row, the main axis is horizontal. justify-content controls horizontal placement and align-items controls vertical placement.
  • Children are not styledThe output sets the container only. Sizing, order, flex-grow and alignment on individual children belong in their own rules.
Direction
row
Justify
space-between
Align
center
Wrap
nowrap
Gap
16px
Declarations
6

The preview always shows three items. Real content will differ; test the layout with long text, missing items and a narrow viewport.

This page writes the CSS for a flex container. Choose the direction, the main-axis and cross-axis alignment, the wrapping behaviour and a gap, and the page returns six declarations plus a live preview of three items. The output is container-only: it does not style the children, because their size, order and growth are separate decisions.

It is a quick way to get the six lines right without remembering which property controls which axis, and a way to see how space-between, baseline or wrap-reverse actually look before you commit them to a stylesheet.

What the output contains

The rule is always the same shape: display: flex, then flex-direction, justify-content, align-items, flex-wrap and gap. That is six declarations, and the stats panel confirms the count. The class is named .flex-container; rename it or copy the declarations into an existing rule.

No width, height, padding or background is written. The preview styles its own box so the items are visible, but those styles are not part of the CSS. A container with no height and no content will collapse, so the parent layout still matters.

The generator adds no vendor prefixes. Flexbox and flex gap are supported in current Chrome, Firefox, Safari and Edge without them. If you must support a browser from before 2021, check its gap support separately; the older workaround used margins on the children.

Main axis and cross axis

In a row, the main axis runs horizontally: justify-content places items left to right and align-items places them top to bottom. In a column, the axes swap: justify-content now controls vertical placement and align-items controls horizontal placement. The checklist repeats this because it is the most common source of confusion when a layout is switched from row to column.

flex-start, center and flex-end pack the items at the start, middle or end of the main axis. space-between puts the first item at the start and the last at the end, with equal gaps between. space-around gives each item equal space on both sides, so the outer edges get half a gap. space-evenly makes every gap, including the outer ones, the same size.

On the cross axis, stretch is the default: items with no fixed cross-size fill the container. baseline aligns the first line of text in each item, which is useful when the items contain different font sizes. The other values pack the items at the start, centre or end of the cross axis.

Wrapping and the gap

With nowrap, flex items shrink to stay on one line. That is often not what a card grid wants: on a narrow screen the items can become too small to read. The checklist warns whenever nowrap is selected. wrap lets items move to a new line when they run out of room, and wrap-reverse does the same in the opposite cross-axis direction.

The gap slider runs from 0 to 80 pixels and is written as a pixel value. gap applies between rows and columns, so a wrapped layout gets the same spacing in both directions. It replaces the old margin-based spacing and does not add space around the outer edge of the container.

If the items still overflow with wrapping on, the cause is usually a child with a fixed width wider than the container or a min-width that prevents shrinking. The container CSS cannot fix that; inspect the child rules.

A worked example

The default settings produced a row container with space-between justification, centred cross-axis alignment, no wrapping and a 16-pixel gap.

Three container settings and the CSS they produced
SettingsResult
row, space-between, center, nowrap, 16display: flex; flex-direction: row; justify-content: space-between; align-items: center; flex-wrap: nowrap; gap: 16px;
column, center, stretch, wrap, 0Six declarations with flex-direction: column and gap: 0px
row-reverse, space-evenly, baseline, wrap-reverse, 48Six declarations with the reversed and baseline values as selected

The second run is the one that changes the mental model: in a column, justify-content: center centres the items vertically, and align-items: stretch makes them fill the width. The same two values in a row would centre them horizontally and stretch them vertically.

The third run put the items in reverse order and aligned their text baselines. Because wrapping is wrap-reverse, a new line appears above the first line rather than below it. The preview shows the order, which is easier than reasoning about it from the property names.

The preview has three fixed itemsReal content may have one item, twenty or a long unbreakable word. Test the container with those cases, and at a narrow viewport, before you ship it.

What the container does not control

Child sizing is the big omission. flex-grow, flex-shrink, flex-basis, order, align-self and individual margins all live on the items. A common layout uses the container for direction and gap, then gives one child flex: 1 so it takes the remaining space. That rule has to be written by hand.

The generator also does not set min-height, padding or a background. Those are visual decisions, and keeping them out means the copied rule can drop into an existing component without overriding its look. If the component already sets display: grid, remove that declaration or the last one wins.

For a complete layout, pair this rule with the gradient generator for the background and the box shadow generator for depth. The CSS beautifier will tidy the rest of the stylesheet around it.

Questions

Common questions

Short answers about this tool and its limits.

Does it style the flex items too?

No. It writes the container rule only. Child properties such as flex-grow, flex-basis, order and align-self belong in separate rules and are not generated here.

Why does justify-content behave differently in a column?

Because it works on the main axis, and the main axis changes with flex-direction. In a row it is horizontal; in a column it is vertical. align-items works on the other axis.

Why does the page warn about nowrap?

With nowrap, items shrink to stay on one line. On a narrow screen they can become unreadably small or overflow. The warning is a prompt to test, not an error; nowrap is valid when the items are small and fixed.

Is flex gap supported without prefixes?

Yes in current Chrome, Firefox, Safari and Edge. Older browsers may need margins on the children instead. The generator writes the unprefixed property and no fallback.

What is the difference between space-around and space-evenly?

space-around gives each item equal space on both sides, so the outer edges receive half a gap. space-evenly makes every gap, including the two outer edges, the same size.

Can I add a second row of items?

Set wrapping to wrap or wrap-reverse. Items move to a new line when they run out of room. The gap applies between rows as well as between columns.

Does the output include vendor prefixes?

No. The rule uses standard properties only. Current browsers do not need prefixes for flexbox or gap. If you maintain an old browser target, add fallbacks in your build rather than in the copied rule.

Is the CSS saved for later?

No. It exists in the page until you copy it or download flexbox.css. There is no account or history, so save it in your stylesheet before closing the tab.