Flexbox Generator

Every flexbox property on one panel, with live boxes that rearrange as you change direction, wrapping, justify-content or align-items. Easiest way to finally see which axis is which.

Flexbox Alignment Explained: justify-content, align-items and the Main Axis

Flexbox is the layout tool most front-end developers reach for every day. It centres things, spaces navigation links, aligns icons with text and builds toolbars. Yet many people still try justify-content and align-items at random until something works. The confusion disappears once you understand one concept: the two axes.

Flexbox justify-content values flex-start, center and space-between compared

Main axis and cross axis

A flex container lays out its children along a line called the main axis. The direction of that line is set by flex-direction:

  • row (the default): the main axis runs left to right (in left-to-right languages).
  • column: the main axis runs top to bottom.
  • row-reverse and column-reverse flip the direction.

The cross axis is always perpendicular to the main axis. Everything about Flexbox alignment depends on which axis a property controls:

PropertyAxisControls
justify-contentMainDistribution of items along the line
align-itemsCrossAlignment of items within the line
align-selfCrossAlignment of one item, overriding align-items
align-contentCrossSpacing between lines when items wrap

So when you switch from row to column, justify-content suddenly controls vertical spacing and align-items controls horizontal alignment. This is the number-one cause of Flexbox confusion, and it’s easy to see in action in the flexbox generator above.

justify-content values

ValueEffect
flex-startPack items at the start
centerPack items in the middle
flex-endPack items at the end
space-betweenFirst and last items at the edges, equal space between
space-aroundEqual space around each item (so edge gaps are half the middle gaps)
space-evenlyExactly equal space everywhere, including edges

align-items values

ValueEffect
stretch (default)Items stretch to fill the line’s cross size
flex-start / flex-endAlign to one side of the line
centerCentre on the cross axis
baselineAlign the first line of text in each item

baseline is underrated: it lines up text of different sizes perfectly, for example a large price next to a small “/month” label.

Centring anything

The famous one-liner:

.parent { display: flex; justify-content: center; align-items: center; }

Grid offers an even shorter version, display: grid; place-items: center;, but the Flexbox approach is so common it is worth knowing by heart.

Wrapping and align-content

By default, flex items stay on one line and shrink to fit. flex-wrap: wrap lets them flow onto new lines. Once there are multiple lines, align-content controls the spacing between those lines, much like justify-content does for items. With a single line, align-content does nothing, which explains why it often seems broken.

gap

gap works in Flexbox in all modern browsers (Safari was last, in version 14.1, 2021). It replaces margin hacks for spacing items, and it never adds space at the outer edges. For tag lists and button groups that wrap, display: flex; flex-wrap: wrap; gap: 8px; is all you need.

How items size themselves: grow, shrink, basis

Each item has three sizing properties, usually set with the flex shorthand:

  • flex-grow: how much of the leftover space the item takes (0 by default).
  • flex-shrink: how much it gives up when space runs out (1 by default).
  • flex-basis: its starting size before growing or shrinking (auto by default, meaning its width or content size).
ShorthandExpands toMeaning
flex: 11 1 0%Grow and shrink equally from zero; items end up the same size
flex: auto1 1 autoGrow and shrink from content size; bigger content gets more space
flex: none0 0 autoFixed at content size
flex: 0 0 200pxNo grow, no shrink, 200pxA fixed-width sidebar

The margin: auto trick

Auto margins absorb free space in Flexbox. In a navigation bar, giving the last item margin-left: auto pushes it (and everything after it) to the far right. This is cleaner than space-between when you want a logo on the left, links in the middle-left and a button on the right.

Classic gotchas

Text overflow in flex items. Flex items have min-width: auto, which means they won’t shrink below their content. A long word or URL can overflow the container. Add min-width: 0 to the item (and overflow-wrap: anywhere or text-overflow: ellipsis to the text).

Images stretching. With the default align-items: stretch, images in a row can stretch vertically. Set align-items: flex-start or give images align-self: center.

Order versus accessibility. The order property and row-reverse change the visual order but not the order screen readers and keyboard navigation follow. Keep the source order logical.

Percent heights in column layouts. Children of a column flex container often need flex: 1 and min-height: 0 to scroll properly inside a fixed-height area.

Flexbox versus Grid

Flexbox works along one axis and lets items’ content drive their size. Grid works in two dimensions and lets the container define the tracks. Use Flexbox for components (navbars, toolbars, media objects, button rows) and Grid for page layouts and galleries. We compare them in detail in our CSS Grid guide.