1. Home
  2. Developer
  3. CSS Flexbox & Grid Generator

CSS Flexbox & Grid Generator

Play with flexbox and grid properties and copy the CSS.

Runs in your browser · No upload
1
2
3
4
CSS code
.container { display: flex; flex-direction: row; flex-wrap: nowrap; justify-content: flex-start; align-items: stretch; gap: 12px; }
Tailwind classes
flex flex-row flex-nowrap justify-start items-stretch gap-[12px]

Arbitrary value syntax such as gap-[12px] is used so it works on any Tailwind version.

How to use

  1. Choose Flexbox or Grid at the top.
  2. Change the selects and sliders and the numbered boxes in the preview rearrange immediately. You can also change the number of boxes.
  3. When the layout looks right, copy the CSS code or the Tailwind classes.

The preview boxes are drawn in different sizes so alignment is easy to see. Those sizes are not part of the output. Only properties set on the container are exported.

Flexbox properties

Property What it does
flex-direction Main axis direction: row, column, and their -reverse forms
flex-wrap Whether items wrap when they do not fit on one line: nowrap, wrap, wrap-reverse
justify-content Alignment along the main axis: start, center, end, or distributing free space with space-between, space-around, space-evenly
align-items Alignment on the cross axis (perpendicular to the main axis). stretch fills the line height
align-content Alignment of the whole stack of lines when there are several. Only meaningful with wrapping, so it is only exported then
gap Space between boxes

justify-content always works on the main axis and align-items on the cross axis. Switching flex-direction to column swaps which screen direction each one controls. That is a common point of confusion, so flip the direction and watch.

Centering needs only this:

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
}

In Tailwind that is flex flex-row flex-nowrap justify-center items-center. The container needs a height for vertical centering to show.

Grid properties

  • Column definition: a fixed count such as repeat(3, 1fr), an automatic count with repeat(auto-fill, minmax(200px, 1fr)) that adapts to the width, or your own value like 1fr 2fr 1fr.
  • Row definition: automatic by default (sized by content), in which case grid-template-rows is not exported.
  • Column gap and row gap: equal values give gap: 16px; different values give gap: 8px 24px (row, then column).
  • justify-items, align-items: how a box sits inside its cell, horizontally and vertically. The default stretch is omitted.

Auto-fill is handy for responsive card lists. With a 200px minimum and a 16px gap the output is:

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

In Tailwind: grid grid-cols-[repeat(auto-fill,minmax(200px,1fr))] gap-[16px].

Flexbox or Grid

Use Flexbox for one direction at a time: menus, button rows, the inside of a card. Use Grid when rows and columns both matter: galleries, dashboards, page frames. They combine well, with Flexbox inside a Grid cell being common.

Things to watch

  • Custom column and row values accept letters, digits, parentheses, commas and % only. With unbalanced parentheses a fixed column count is shown instead.
  • Tailwind gaps use the arbitrary form such as gap-[12px], so values outside the default spacing scale come out exactly.
  • This tool handles container properties only. Per-item properties like flex-grow or grid-column you add yourself.

Frequently asked questions

How do I center something?

Give the container display: flex; justify-content: center; align-items: center; and the child sits in the middle both ways. Vertical centering only shows when the container is taller than the child.

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

justify-content is the main axis and align-items is the cross axis. With flex-direction: row the main axis is horizontal, so justify-content aligns horizontally. With column it is the other way round.

Is flex gap supported everywhere?

Flexbox gap has been supported by all major browsers since about 2021. If you must support much older ones, use margin on the children instead.

Why do my boxes overflow instead of wrapping?

With the default flex-wrap: nowrap the items try to shrink onto one line. Switch to wrap and they move to the next line when space runs out.

Related tools: CSS Box Shadow Generator, CSS Border Radius Generator, CSS Gradient Generator, CSS Beautify and Minify.

Your values and settings are processed in your browser and never sent to a server.

Can't find the tool you need?

Tell us what you want to do. We build the most requested tools first.