OnlineCBT Logo OnlineCBT
CSS Tutorial
CSS · LESSON 24

Flex Direction, Wrap, and Flow

By Ratan Updated: 15 Oct 2026

Control row/column orientation and multi-line wrapping using flex-direction and flex-wrap.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Set the main axis and decide whether items wrap

flex-direction chooses the main-axis direction. flex-wrap decides whether items stay on one line or form additional lines. The flex-flow shorthand combines them. Row follows the inline axis, so writing direction matters.

.cards {
  display: flex;
  flex-flow: row wrap;
  gap: 1rem;
}
.card { flex: 1 1 16rem; }
.steps {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

The cards attempt a row and wrap as space becomes insufficient. Each item starts from a 16rem basis but can grow or shrink. The steps form a column. In a column, the main axis is vertical in the usual horizontal writing mode, so main-axis alignment properties act differently than in a row.

Wrapping is not a grid

Each flex line distributes its own available space. If the last row has one item, that item may grow across the row even when rows above contain several equal items. Use grid when columns must align across rows.

Reverse direction carefully

row-reverse and column-reverse change visual layout direction without changing the underlying document order. That can create a mismatch between visual reading and keyboard focus. Write the source in the intended logical order and use reversal only when the meaning remains clear.

Column wrapping needs a constraint

A column-wrapping container normally needs a relevant height constraint before items can wrap into additional columns. Merely writing flex-direction:column and flex-wrap:wrap on an unconstrained auto-height box may not produce the extra columns you expect.

Exercises

  1. Create five cards and compare nowrap with wrap.
  2. Switch row to column and observe how the axes change.
  3. Use a narrow viewport and confirm cards do not force horizontal scrolling.
  4. Tab through controls after applying row-reverse. Explain why the focus sequence may no longer match the visible arrangement.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat