SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
FlexboxDirection and WrappingFlex Item SizingMain-Axis AlignmentCross-Axis AlignmentFlexbox PatternsDebugging Flexbox Sizing and aligning a flex layout
Flexbox Layouts

Direction and Wrapping

Set the direction of flex items and control wrapping.

flex-direction chooses the main axis. flex-wrap decides whether items stay on one flex line or can move to additional lines. Together, they determine the flow of a flex layout.

Choose a direction

.steps {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

In a horizontal writing mode, the steps form a vertical stack. The first step stays before the second in the document and appears above it.

DirectionMain axisStarting side in horizontal, left-to-right text
rowInline axis.Left.
row-reverseReversed inline axis.Right.
columnBlock axis.Top.
column-reverseReversed block axis.Bottom.

A row follows the document's inline direction. For right-to-left text, row starts on the right; it is not permanently tied to the left edge.

Allow items to wrap

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.cards > article {
  flex: 0 1 10rem;
}

Each article has a starting size, its basis, of 10rem. Flexbox decides which articles fit together before distributing extra space or shrinking the items on each line. Include the gaps when you predict the break. The next sizing lesson explains the three flex values.

With a 16-pixel root font size, no item margins or padding, and enough room for each card's content:

  • At 352 pixels of available width, two 160-pixel cards fit with a 16-pixel gap.
  • At 320 pixels, the cards and gap need 336 pixels, so the cards move onto separate lines.
  • An item that is itself wider than a line may still need to shrink or handle overflowing content.

Items wrap when the container runs out of space, whatever device is in use. For a column to wrap into more columns, give it a height limit.

Distinguish wrapping values

  • nowrap: The initial value. Items remain on one flex line, even if they shrink or overflow.
  • wrap: Adds lines toward the cross-end side.
  • wrap-reverse: Adds lines with cross-start and cross-end reversed.

wrap-reverse changes the stacking direction of flex lines. It does not reverse the sequence of items within each line.

Direction and wrapping can also be written together:

.cards {
  flex-flow: row wrap;
}

Add this declaration to a flex container; the shorthand sets both properties.

Preserve a meaningful reading order

Reversing the direction changes what you see, but keeps the HTML order and usual Tab order. Write the HTML in a sensible reading order first.

Predict the next line

With the card sizes above, what happens at 320 pixels if you change wrap to nowrap? The cards can shrink; which part of the rule permits that?

Check your reasoning

They stay on one line. The middle 1 in flex: 0 1 10rem permits shrinking, although minimum sizes can still cause overflow.

A column needs multiple columns of steps. Which constraint must it have before wrapping can occur?

Check your reasoning

Give the column a limited main size (height in horizontal writing), so there is a point where another line is needed.

Key Takeaways

  • Direction: Chooses the main axis and its starting side.
  • Wrapping: Creates more flex lines when the items do not fit.
  • Shorthand: flex-flow combines direction and wrapping.
  • Order: Keep the document sequence meaningful when visual direction changes.

Try it yourself

Try it yourself

Wrap a row

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

How is this lesson?

Previous lessonFlexboxNext lessonFlex Item Sizing
Edit on GitHubReport on GitHub
On this page
%

On this page

Choose a directionAllow items to wrapDistinguish wrapping valuesPreserve a meaningful reading orderPredict the next lineKey TakeawaysTry it yourself