Module 4 · Layout · Lesson 3 of 5

CSS Flexbox

Intermediate ⏱ 40 min 6 sections · 1 exercise · 5-question quiz

Flexbox is the layout system you'll use most often: it distributes space along a single row or column and aligns items with one or two properties instead of margin arithmetic. By the end of this lesson you'll build navigation bars, centered cards, and equal-height columns without a single float.

1. What is Flexbox?

Before Flexbox, laying items out in a row meant floating them, using inline-block (and fighting whitespace gaps), or abusing tables. Each approach broke down as soon as you wanted vertical centering or equal heights. Flexbox — the Flexible Box Layout Module — solves exactly this class of problem: arranging a set of items along one axis and controlling how leftover space is distributed between them.

You activate it with a single declaration on a parent element. The parent becomes a flex container, and its direct children automatically become flex items:

CSS
.container {
  display: flex;
}

That one line changes everything about how the children behave. They line up in a row, shrink to fit their content, and stretch to equal heights. Everything else in this lesson is about fine-tuning that behavior.

💡 Rule of thumb: Flexbox is for one-dimensional layout (a row or a column). When you need rows and columns at the same time, reach for CSS Grid — the next lesson.

2. The two axes

Every flex container has a main axis and a cross axis, and almost every flex property aligns things along one of them. Which direction the main axis runs is set by flex-direction:

ValueMain axisTypical use
row (default)Left → rightNav bars, button groups, card rows
row-reverseRight → leftMirrored layouts
columnTop → bottomStacked forms, mobile layouts
column-reverseBottom → topChat message lists

Remembering the axes is the key that unlocks Flexbox: justify-content always works along the main axis, and align-items always works along the cross axis. If a property "isn't working," you're almost always aligning on the wrong axis.

3. Distributing space: justify-content

justify-content controls where extra space along the main axis goes. Here is the same three-item row with the three values you'll use most:

CSS
.nav {
  display: flex;
  justify-content: space-between;
  /* also try: flex-start | center | space-around | space-evenly */
}

Live result of each value (real flex containers, resize your window to see them adapt):

123flex-start
123center
123 · space-between

4. Aligning on the cross axis: align-items

While justify-content distributes items along the row, align-items positions them across it. The default is stretch, which is why flex children magically become equal height. The value you will type most in your career is this one:

CSS — the classic "center anything" recipe
.hero {
  display: flex;
  justify-content: center;  /* horizontal */
  align-items: center;      /* vertical   */
  min-height: 60vh;
}

Other values: flex-start (top of a row), flex-end (bottom), and baseline (align text baselines — handy for mixed-size labels). A single item can override the group with align-self.

5. Gap, wrapping, and flexible sizing

Three more properties complete your everyday toolkit:

CSS — responsive cards, no media queries
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.cards > article {
  flex: 1 1 220px; /* grow, shrink, ideal width */
}
⚠️ Common mistake: putting flex properties on the wrong element. justify-content, align-items, gap and flex-wrap go on the container; flex, align-self and order go on the items.

6. Try it yourself

The editor below is live. Your task: the three boxes are currently squashed to the left. Edit the .box-row rule so the boxes are evenly spread across the full width and vertically centered. Press Run (or Ctrl/Cmd + Enter) to see your result.

exercise-flexbox.html — editable
Solution: justify-content: space-between; align-items: center;

7. Quiz — check your understanding

Flexbox Quiz

5 questions · pass at 60%

Q1. Which declaration turns an element into a flex container?

Flexbox is activated through the display property on the parent element. Its direct children then become flex items automatically.

Q2. justify-content aligns items along which axis?

Main axis. Its counterpart on the cross axis is align-items. Which direction the main axis runs depends on flex-direction.

Q3. You want space between flex items, but none at the edges of the container. The cleanest way is:

gap adds space only between items — no leaking margins to cancel at the edges, and the same property works in CSS Grid.

Q4. Which property must be set on a flex item (not the container)?

flex (grow / shrink / basis) describes how one child sizes itself. The other three configure the container as a whole.

Q5. To perfectly center one child both horizontally and vertically, the container needs:

The classic recipe: flex container, center on the main axis with justify-content, center on the cross axis with align-items.