Home › Generators › Flexbox Generator

Flexbox Generator

Build a CSS flexbox layout without writing code. Pick a direction, set the alignment and spacing, and watch the preview update as you go. When the layout looks right, copy the finished CSS and paste it into your own stylesheet.

row · space-between · center
5 items · wrap · 16 px gap — the CSS below matches this preview exactly.

Tip: try justify-content: space-between with align-items: center — it is the classic recipe for a navigation bar.

How it works

Flexbox starts with a container and the items inside it. Setting display: flex on the container turns its children into flex items and lines them up along the main axis. The main axis runs left to right when flex-direction is row, and top to bottom when it is column. The cross axis always runs at right angles to the main axis, so in a row layout the cross axis is vertical.

flex-direction chooses the direction items flow. Row is the default and reads left to right; row-reverse flips it so the first item sits on the right. Column stacks items from top to bottom, and column-reverse stacks them from the bottom up. Reverse directions are handy for right-to-left layouts or for putting an image below its text on small screens without changing the HTML.

justify-content spreads items along the main axis. Flex-start packs them at the start, center groups them in the middle, and flex-end packs them at the end. Space-between pushes the first item to the start and the last item to the end with equal gaps between the rest. Space-around and space-evenly add space around every item too, with space-evenly making every gap exactly equal.

align-items lines items up along the cross axis. Center is the most used value — it centers every item vertically in a row layout. Stretch, the default, makes items fill the full height of the container. Flex-start and flex-end pin items to the top or bottom, and baseline lines up the text inside items that have different font sizes.

flex-wrap decides what happens when items run out of room. With nowrap, the default, items shrink to fit on one line, which can squeeze them badly. With wrap, extra items drop to a new line, which is what you usually want for card grids. Gap sets the space between items in pixels — one value that replaces fiddly margins on every item. Change any control and the preview and the CSS update together, so the code you copy always matches what you see.

.container { display: flex; flex-direction: row; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 16px; }

Worked example

Suppose you are building a row of five product cards for a small shop website. You want the cards spread across the full width, centered vertically, with a 16-pixel gap, and wrapping to a new line on narrow screens. Set the controls like this:

Step 1: Set flex-direction to row, so the cards run left to right.
Step 2: Set justify-content to space-between, so the first card touches the left edge, the last card touches the right edge, and the gaps between cards are equal.
Step 3: Set align-items to center, so shorter cards sit in the middle of the row instead of stretching.
Step 4: Set flex-wrap to wrap and the gap to 16 px, with the item count at 5.

The preview shows five blue boxes spread across the row, and the tool produces exactly this CSS:

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
}

Copy that block, paste it into your stylesheet, and give your card row the class container. On a wide screen the five cards sit on one line; on a phone the last cards wrap neatly underneath with the same 16-pixel gap.

FAQ

What is CSS flexbox?

Flexbox is a CSS layout model that arranges items in a row or a column inside a container. You set display: flex on the container, and the browser lines the items up along a main axis and a cross axis. It handles spacing, alignment, wrapping, and equal sizing, which makes it the easiest way to build navigation bars, card rows, and centered content.

What is the difference between justify-content and align-items?

justify-content positions items along the main axis, which is horizontal when flex-direction is row and vertical when it is column. align-items positions items along the cross axis, which runs in the other direction. In a row layout, justify-content moves items left and right while align-items moves them up and down.

When should I use flex-wrap: wrap?

Use flex-wrap: wrap when the items might not fit on one line, such as a row of cards on a small screen. With wrap, extra items move to a new line instead of shrinking or overflowing. Keep nowrap only when you are sure the items will always fit, for example a short navigation menu on a desktop layout.

How do I copy the CSS this generator makes?

Press the Copy CSS button under the code box. The full .container rule is copied to your clipboard, ready to paste into your stylesheet. Give your own container the class name container, or rename the class in the copied code to match the class you already use.

Does the generated CSS work in all browsers?

Yes. Flexbox is supported by every modern browser, including current versions of Chrome, Edge, Firefox, and Safari, on desktop and mobile. The properties this generator produces — display: flex, flex-direction, justify-content, align-items, flex-wrap, and gap — need no prefixes or fallbacks in any browser released in the last several years.