THE OPEN STUDIO / LAYOUT

Why are Flexbox and Grid different?

Flexbox thinks in one dimension at a time. Change direction, alignment and spacing to see how a flexible relationship behaves.

Interactive · Mature featurePlain HTML routeOpen source

Sometimes you only need one direction.

Imagine a navigation bar. Its items belong next to each other, and you want the browser to manage the space between them. Or imagine a stack of cards that should grow or shrink together. These are natural Flexbox problems: coordinate a row or a column without specifying every item's position.

Older CSS layout techniques used floats and manual sizing to imitate flexible rows. Flexbox introduced a model built for distributing space, aligning boxes and responding to their intrinsic sizes.

Change the direction of the conversation.

Switch the main axis from row to column. Now try space-between. The meaning of justify-content changes with the flex direction: it always works along the main axis.

LIVE EXPERIMENT / FLEXChange one variable at a time
123
CSS THE BROWSER IS USING

These controls update CSS custom properties; the example's HTML stays the same.

Remember: justify-content follows the main axis. align-items works along the cross axis. Those axes swap when the direction swaps.

A relationship, not a coordinate map.

.toolbar {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

Flex items have a basis, grow factor and shrink factor. The browser computes available free space and distributes it according to those rules. This is why flex: 1 is not quite the same as “every item has exactly the same natural width.” Content constraints still matter.

When items wrap, each flex line is laid out independently. If you need columns to align across rows, Grid is often the clearer tool. Flexbox and Grid are complementary rather than competitors.

Pick a tool by the shape of the problem.

  • Use Flexbox for navigation, button groups, media-object layouts and short one-axis arrangements.
  • Use Grid when tracks should coordinate in two directions.
  • Use gap for consistent space between items instead of fragile margins on every child.
  • Beware of forcing no-wrap toolbars into a small screen: allow wrapping or plan an alternative pattern.

Real order, small screens.

The row-reverse and column-reverse values change visual order, not the DOM's source order. For interactive controls, that can create a confusing mismatch between keyboard focus and what people see.

At 200% zoom, test whether your toolbar fits, wraps or obscures labels. Flexbox is generally efficient for ordinary layout; don't animate properties that force massive repeated layout calculations without measuring the impact.

Make something people can use.

Break it

Put five long navigation labels into a fixed-width row that cannot wrap. Observe the overflow. What would a more robust layout do?

Build it

Create a responsive header with a logo, three links and a utility button. Try Flexbox first, then Grid. Explain which expresses the actual layout relationship more clearly. Compare with the reimagined Flexbox resource cards.

Go a little deeper.