JomMula

Frontend Development: HTML to React · CSS · Lesson 07

Flexbox: lay things out in a row

Flexbox is the layout tool you'll reach for most: rows, columns, spacing, centering, and wrapping. Learn the five properties that do 90% of the work.

7 min readCSSlayoutflexboxUpdated October 2026

Before Flexbox, centering something vertically in CSS was a personality test. People used floats, tables, absolute positioning, and dark magic. Flexbox made it boring.

Flexbox is for laying out items in one direction — a row or a column. That’s it. Once you get that, the properties make sense.

The five properties

You put display: flex on the parent. The parent becomes a flex container, and its direct children become flex items.

.row {
  display: flex;
  flex-direction: row;      /* row | column */
  justify-content: center;  /* main axis */
  align-items: center;      /* cross axis */
  gap: 16px;
}
  • flex-direction — the main axis: row (default) or column.
  • justify-content — how items are distributed along the main axis.
  • align-items — how items line up on the cross axis.
  • gap — space between items. No margin hacks.
  • flex-wrap — whether items can wrap to the next line.

Common values

For justify-content:

  • flex-start — pack at the start.
  • center — center.
  • space-between — first item at the start, last at the end, equal space between.
  • space-around / space-evenly — variations of even spacing.

For align-items:

  • stretch — fill the cross axis (default).
  • center — center.
  • flex-start / flex-end.

For items:

  • flex: 1 — grow to fill the available space.
  • flex: 0 0 auto — don’t grow or shrink; keep natural size.
  • flex: 1 1 180px — start at 180px, then grow or shrink. Great for cards.

Try it: a header and some cards

This is the same pattern you’ll use for the calculator’s header, form rows, and results.

Preview

Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.

Try resizing the preview. The cards wrap because of flex-wrap: wrap and the flex: 1 1 180px starting width.

Flex the calculator

the plan's verify step

Make the page header a flex row: brand on the left, nav on the right, vertically centered. Then make a row of two result cards. Finally, put each label/input pair in a column so the label sits above the input.

If something isn't lining up, check which element is the flex parent. display: flex goes on the container, not the children.

The mistakes everyone makes

  • Putting display: flex on the children. It goes on the parent.
  • Mixing up justify-content and align-items. Main axis vs cross axis.
  • Using margins for spacing. Use gap.
  • Forgetting flex-wrap. Cards shrink into unreadable slivers instead of wrapping.
  • Expecting flex to do a two-dimensional grid. It can fake it, but Grid is better. Next lesson.
  • A flex item with no width inside a row. It may collapse or overflow; give it a flex value or a min-width.

Flexbox lays out items in one direction: a row or a column.

display: flex goes on the parent.

justify-content = main axis; align-items = cross axis.

gap is the clean way to space flex items.

flex: 1 grows; flex-wrap lets items move to the next line.

Next: CSS Grid and responsive design — two-dimensional layouts and pages that work on a phone.

Previous: CSS basics: selectors, cascade, and the box model
08

Up next

Grid and responsive design

Grid handles layouts with rows and columns at the same time. Pair it with media queries and fluid sizing, and your page works on a phone, a tablet, and a laptop.

Read now