🍱 Layout Generator

CSS Flexbox & Grid Layout Playground

Interactively adjust flex direction, alignment, grid template columns, and gaps with visual live preview and instant CSS export.

Live Layout Container
Item 1
Item 2
Item 3
Item 4
flex-direction
Gap Spacing16px
Generated CSS Code
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 16px;

One axis or two: the real difference

Flexbox lays items out along a single main axis, set by flex-direction, and lets them grow, shrink and align along it. Grid defines rows and columns first and then places items into the resulting cells. The practical test is simple: if you care about items lining up in both directions at once, as in a card grid or a dashboard, use Grid. If you are distributing a set of items along one line, such as a navbar, a button group or a row of tags, use Flexbox.

The two are not rivals. A typical page uses Grid for the overall layout and Flexbox inside each card or header to align its contents.

How justify-content actually divides the space

justify-content works on the free space left over after the items and gaps are placed. Take a 600px wide flex row with four 100px items and gap: 16px. The items use 400px and the three gaps use 48px, leaving 152px of free space.

  • flex-start puts all 152px after the last item; flex-end puts it all before the first; center splits it 76px on each side.
  • space-between shares the 152px across the three gaps between items, about 50.67px each, so items end up roughly 66.67px apart (16px gap plus 50.67px) with no space at the edges.
  • space-around gives each item 38px (152 divided by 4), half on each side, so the outer edges get 19px and the space between items is 38px plus the 16px gap, 54px in total.

Worked example: what 1fr really means

grid-template-columns: repeat(3, 1fr) with gap: 16px in a 960px container first subtracts the two gaps (32px) and then shares the remaining 928px equally, giving three columns of about 309.33px. The fr unit always divides space left after fixed sizes and gaps are taken out.

One catch: 1fr is shorthand for minmax(auto, 1fr), so a column will not shrink below its content's minimum size. A long URL or a wide table in one cell can push that column wider than the others and overflow the container. Write repeat(3, minmax(0, 1fr)) when the columns must stay equal no matter what is inside them.

Making the generated layout responsive

A fixed column count is a starting point. For a card grid that adapts to any screen without media queries, replace the generated columns with grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)). The browser fits as many 240px-minimum columns as the container allows and stretches them to fill the row.

Flex rows do not wrap by default, so items squeeze rather than move to a new line. Add flex-wrap: wrap to the container when items should drop to the next row on narrow screens. Switching flex-direction to column at a breakpoint is another common pattern for stacking navigation on mobile.

Use the gap property rather than margins on children for spacing. Gap only appears between items, never at the outer edges, so there is no need for :last-child overrides.

Flexbox & Grid Playground: frequently asked questions

Why does align-items: center not centre my items vertically?

align-items works on the cross axis, and the container needs spare height for centring to be visible. If the container is only as tall as its tallest item, there is nothing to centre within. Give the container a height or min-height, and remember that with flex-direction: column the axes swap, so justify-content handles vertical alignment instead.

What does align-items: stretch do?

It makes items without an explicit cross-axis size fill the full height of a row, or the full width of a column. This is why cards in a flex row often end up the same height. Setting a fixed height on an item overrides the stretch.

Is gap supported for Flexbox as well as Grid?

Yes. All current major browsers support gap on flex containers, not just grid containers. Only very old browser versions lack it, and those would need margins on the children instead.

What is the difference between auto-fit and auto-fill in repeat()?

Both create as many columns as fit. auto-fill keeps empty tracks when there are fewer items than columns, while auto-fit collapses the empty tracks so the existing items stretch to fill the row. For a handful of cards that should fill the width, auto-fit is usually what you want.

Why is one grid column wider than the others when they are all 1fr?

Each 1fr track has a minimum of its content's minimum width, so unbreakable content like a long word or image forces that column wider. Use minmax(0, 1fr) to allow the track to shrink below its content and let the content wrap or overflow instead.

All css & visual generators