CSS Flexbox Generator

A free tool to experiment with Flexbox layouts by adjusting flex-direction, justify-content, align-items, flex-wrap, and gap through a live preview. Copy the generated CSS code with one click.

Tips

  • justify-content aligns items along the "main axis" while align-items aligns them along the "cross axis". Switching flex-direction to column flips the main axis to vertical, so the visual effect of these two properties swaps.
  • The "Centered" preset combines justify-content: center with align-items: center, which is the most common way to center an element both horizontally and vertically with Flexbox.
  • The gap property only adds space between items, not around the outside of the container, so unlike margin it creates even spacing between siblings without extra padding at the edges.
  • Setting align-self on a single item overrides the container's align-items just for that item, which is handy when you want one element to line up differently from the rest.
  • Enabling flex-wrap: wrap lets items that no longer fit the container's width (or height, in column mode) drop onto the next line automatically, a common technique for responsive card layouts.

Frequently Asked Questions

justify-content controls alignment along the Flexbox "main axis" (the direction set by flex-direction, horizontal by default), while align-items controls alignment along the perpendicular "cross axis". With flex-direction: row the main axis is horizontal, so justify-content handles horizontal placement and align-items handles vertical placement — but switching to flex-direction: column flips the main axis to vertical, so the two properties effectively swap roles.

When flex-direction is column, the main axis runs vertically, so justify-content: center centers items vertically. To also center them horizontally (the cross axis), use align-items: center. Combining both lets you center content both ways even in a stacked column layout.

Flexbox gap support lagged in Chrome and Safari until around 2021, but it is now supported in all major browsers (Chrome 84+, Safari 14.1+, Firefox 63+). If you need to support very old browsers, consider using margin as a fallback.

flex-grow controls how much an item expands to fill leftover space when the container has extra room, while flex-shrink controls how much an item shrinks when the container is too narrow to fit everything at its default size. Both properties determine how surplus or shortfall is distributed relative to an item's base size (flex-basis).

Flexbox generally works best for laying out items along a single direction (a row or a column), such as a navigation bar or a row of cards. CSS Grid, on the other hand, is designed for controlling rows and columns simultaneously in a true two-dimensional layout, such as an entire page grid. As a rule of thumb, reach for Flexbox for simple linear arrangements and Grid for more complex grid-like structures.
ツールくん

Side Note — How Flexbox Changed CSS Layout History

Before CSS Flexbox (formally the CSS Flexible Box Layout Module) arrived, horizontal layouts on the web were typically built using the float property. But float was originally designed to let text wrap around an image, not to build layouts, so using it for that purpose required many workarounds — most famously the "clearfix" hack to prevent a parent element's height from collapsing.

Flexbox, whose first draft was published by the W3C in 2009, was designed specifically for one-dimensional layouts. Tasks that once needed multiple lines of CSS hacks with float — stretching elements along the main axis, distributing space evenly, or vertically centering content — could now be expressed with a single declarative property like justify-content or align-items.

Once major browsers finished implementing Flexbox around 2015, it quickly became the standard way to build common web design patterns such as navigation bars, card layouts, and form alignment. Front-end developers at the time particularly welcomed the fact that vertical centering, long considered one of CSS's trickiest problems, finally had a straightforward solution.

Today, Flexbox coexists with CSS Grid, which specializes in two-dimensional layouts, with each handling a different role: Flexbox for laying out components along a single direction, and Grid for structuring the page as a whole grid.