TetSharing
CSS tutorial · 12 chapters
  1. Get started
  2. Selectors
  3. Cascade and specificity
  4. Colors
  5. Units and sizes
  6. Box model
  7. Text and fonts
  8. Flexbox
  9. Grid layout
  10. Media queries
  11. Custom properties
  12. Transitions

Final CSS quiz
Cheat sheet: css selectors

Chapter 8 of 12 · By Ethan Cole · Updated November 21, 2026

CSS Flexbox Tutorial: Align Items in a Row or Column

Flexbox lays out items along one axis, a row or a column, and makes it easy to space and align them. Navigation bars, button groups, card footers and "push this to the right" problems are all Flexbox jobs.

Turning it on

display: flex on a parent turns it into a flex container, and its direct children become flex items. By default they line up in a row, shrink if they do not fit, and stretch to the same height.

The two axes

  • flex-direction picks the main axis: row (the default), row-reverse, column or column-reverse.
  • justify-content distributes items along the main axis: flex-start, center, flex-end, space-between (first item at the start, last at the end, equal gaps between) or space-around.
  • align-items aligns items on the cross axis, the perpendicular one: stretch (default), flex-start, center, flex-end or baseline.

Perfect centering is two lines: justify-content: center; align-items: center;.

Space and wrapping

gap: 1rem puts space between items without adding any to the outer edges, which beats juggling margins. flex-wrap: wrap lets items move to a new line instead of shrinking when space runs out.

Growing and shrinking

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

  • flex-grow: how much of the leftover space the item takes (0 means none).
  • flex-shrink: how much it gives up when space is short.
  • flex-basis: its starting size before growing or shrinking.

flex: 1 is shorthand for "grow 1, shrink 1, basis 0", so several flex: 1 items share the row equally. In the example, a 600-pixel row contains a fixed 100-pixel logo and two items with flex: 1 and flex: 2, separated by 20-pixel gaps. The script prints each item's rendered width.

HTML · editable

Expected page text

Logo
flex 1
flex 2
logo: 100px
flex 1: 153px
flex 2: 307px

The free space after the logo and the two gaps is 600 - 100 - 40 = 460 pixels. It is split one part to two, so the items get roughly 153 and 307 pixels, as the output shows.

Common mistakes

  • Putting display: flex on the item instead of its parent. The container is the element whose children you want to arrange.
  • Using justify-content to center vertically in a row. In a row, vertical is the cross axis, so use align-items.
  • Expecting gap to work in very old browsers; every current browser supports it in Flexbox.

Summary

  • display: flex on the parent; children become flex items in a row.
  • justify-content works on the main axis, align-items on the cross axis.
  • gap spaces items; flex controls how they share space.

Quiz: Flexbox

1. In a flex container with flex-direction: row, which property centers items vertically?

In a row the vertical direction is the cross axis, handled by align-items.

2. What does flex: 1 on three sibling items do?

flex: 1 means grow 1, shrink 1, basis 0, so equal shares.

3. Which property puts space between flex items but not around the outer edge?

gap adds space only between items.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.