Tutorials CSS Flexbox Container Properties
Ch 26 / 60
Chapter 26 Beginner 9 min read CSS

CSS Flexbox Container Properties

The previous chapter introduced Flexbox. This chapter goes deep on every property you apply to the flex container (the parent) — the full set of tools that control how children are arranged, wrapped, aligned and spaced. Master these and you can build virtually any one-dimensional layout.

What you'll learn
Complete flex-direction with all 4 values
flex-wrap and the flex-flow shorthand
All 6 justify-content values with live demos
All 5 align-items values
align-content — multi-line alignment
gap, row-gap, column-gap and place-content

All Container Properties — Quick Reference

These are all the properties applied to the flex container (the parent with display: flex):

flex-direction
default: row
Sets the main axis direction. Values: row, row-reverse, column, column-reverse.
flex-wrap
default: nowrap
Controls whether items wrap onto multiple lines. Values: nowrap, wrap, wrap-reverse.
flex-flow
shorthand
Shorthand for flex-direction + flex-wrap together.
justify-content
default: flex-start
Distributes items and free space along the main axis.
align-items
default: stretch
Aligns items along the cross axis within a single line.
align-content
default: normal
Aligns multiple lines of items along the cross axis. Only affects multi-line containers.
gap / row-gap / column-gap
default: 0
Sets spacing between flex items without affecting the outer edges.
place-content
shorthand
Shorthand for align-content + justify-content together.

flex-direction

flex-direction establishes the main axis — the primary direction items are laid out. All four values shown with the same three items:

row (default) — left → right
1
2
3
row-reverse — right → left
1
2
3

1 appears at right — order is reversed visually

column — top → bottom
Item 1
Item 2
Item 3
column-reverse — bottom → top
Item 1
Item 2
Item 3

1 appears at bottom

Axes rotate with direction
When you change flex-direction to column, the main axis becomes vertical and the cross axis becomes horizontal. This means justify-content now aligns vertically and align-items now aligns horizontally. Always think in terms of axes — not top/bottom/left/right.
CSS — flex-direction
.container { display: flex; }

.row     { flex-direction: row; }           /* default */
.row-rev { flex-direction: row-reverse; }
.col     { flex-direction: column; }
.col-rev { flex-direction: column-reverse; }

/* Responsive stacking pattern */
.nav  { display: flex; flex-direction: row; }
@media (max-width: 640px) {
  .nav { flex-direction: column; }
}

flex-wrap & flex-flow

flex-wrap controls whether items can break onto multiple lines. flex-flow is a shorthand that combines flex-direction and flex-wrap in one declaration:

nowrap (default) — one line, may shrink/overflow
Item 1
Item 2
Item 3
Item 4
Item 5

Items overflow container

wrap — items break to next line ✓
Item 1
Item 2
Item 3
Item 4
Item 5
wrap-reverse — wraps upward
Item 1
Item 2
Item 3
Item 4
Item 5

First line appears at the bottom

flex-flow: row wrap (shorthand)
Item 1
Item 2
Item 3
Item 4

flex-flow: row wrap = direction + wrap

CSS — flex-wrap & flex-flow
.container {
  display:    flex;
  flex-wrap: nowrap;       /* default */
  flex-wrap: wrap;          /* multi-line ← use this */
  flex-wrap: wrap-reverse;   /* wrap but bottom-up */
}

/* flex-flow: shorthand for direction + wrap */
.container { flex-flow: row wrap; }
.container { flex-flow: column nowrap; }
.container { flex-flow: row-reverse wrap; }

/* Responsive card grid — items fill row and wrap */
.card-row {
  display:   flex;
  flex-wrap: wrap;
  gap:       20px;
}
.card {
  flex: 1 1 280px;  /* grow / shrink / min-width */
  max-width: 400px;
}

justify-content — Main Axis Distribution

justify-content distributes items and free space along the main axis. All 6 values with the same three items (A, B, C) in a wide container so the spacing differences are visible:

Space distribution comparison
space-between: |A         B         C|
space-around:  |½S A S B S C ½S|
space-evenly:  |S  A S B S C S|

Where S = one unit of space. between = no edge space, around = half-unit at edges, evenly = equal space everywhere including edges.

CSS — justify-content
.container { display: flex; }

/* justify-content values */
.start   { justify-content: flex-start; }   /* default */
.end     { justify-content: flex-end; }
.center  { justify-content: center; }
.between { justify-content: space-between; }
.around  { justify-content: space-around; }
.evenly  { justify-content: space-evenly; }

/* Modern values (same as above but direction-agnostic) */
.start2  { justify-content: start; }
.end2    { justify-content: end; }

/* Real use: nav with logo left, links right */
.navbar  {
  display:         flex;
  justify-content: space-between;
  align-items:     center;
}

align-items — Cross Axis Alignment (Single Line)

align-items aligns items along the cross axis within a single flex line. Each card below has items with different heights so you can see the alignment clearly:

CSS — align-items
.container { display: flex; }

.stretch   { align-items: stretch; }     /* default — fill cross axis */
.top       { align-items: flex-start; } /* top in row direction */
.bottom    { align-items: flex-end; }   /* bottom */
.middle    { align-items: center; }     /* vertically centred ← most used */
.text-line { align-items: baseline; }   /* align text baselines */

/* ── Perfect full centring ── */
.perfect-center {
  display:         flex;
  justify-content: center;  /* main axis */
  align-items:     center;  /* cross axis */
  min-height:      100vh;
}

/* Individual item can override with align-self */
.special-item { align-self: flex-end; }
/* ↑ align-self is an ITEM property (covered in Ch 27) */

align-content — Multi-Line Cross Axis Alignment

align-content aligns the lines themselves within the container when there is more than one flex line. It only has any visible effect when flex-wrap: wrap creates multiple rows and the container has extra space on the cross axis.

align-items vs align-content
align-items — aligns items within a single line (each row in a multi-line container is treated separately).
align-content — aligns the lines as a group within the container. Only affects multi-line containers (needs flex-wrap: wrap and a fixed height with leftover space).
CSS — align-content
.container {
  display:       flex;
  flex-wrap:     wrap;   /* REQUIRED for align-content to work */
  height:        300px;  /* REQUIRED — needs extra cross-axis space */

  align-content: flex-start;    /* lines packed at start */
  align-content: flex-end;      /* lines packed at end */
  align-content: center;        /* lines centred */
  align-content: space-between; /* max space between lines */
  align-content: space-around;  /* equal space around lines */
  align-content: space-evenly;  /* equal space everywhere */
  align-content: stretch;        /* default — lines stretch */
}

/* Common confusion:
   align-items = per-line item alignment
   align-content = whole-container line alignment */

gap, row-gap & column-gap

gap adds space between flex items — not at the outer edges. It's far cleaner than using margins on items because you never need to handle "first item" or "last item" edge cases:

gap: 0 (no spacing)
A
B
C
gap: 10px (uniform)
A
B
C
gap: 4px 16px (row col)
A
B
C
D
❌ margin on items (messy)
.item { margin-right: 10px; }
.item:last-child {
  margin-right: 0; /* edge case! */
}
✓ gap on container (clean)
.container {
  display: flex;
  gap: 10px; /* that's it */
}
CSS — gap, row-gap, column-gap
.container {
  display: flex;

  gap:        16px;       /* row AND column gap */
  row-gap:    12px;       /* between rows (with flex-wrap) */
  column-gap: 20px;       /* between columns */

  /* Shorthand: gap: <row> <column> */
  gap:        12px 24px;  /* 12px rows, 24px columns */
}

/* gap works in Grid too — same syntax */

/* Common values */
.compact { gap: 8px; }
.normal  { gap: 16px; }
.loose   { gap: 24px; }
.wide    { gap: clamp(12px, 2vw, 24px); } /* responsive */

place-content — Alignment Shorthand

place-content is a shorthand for both align-content and justify-content. The most useful application is place-content: center which centres items on both axes at once — great for centering content in a container:

place-content: center
A
B
place-content: start
A
B
place-content: end
A
B
CSS — place-content shorthand
/* place-content: <align-content> <justify-content> */
.container {
  display: flex;

  /* Long form */
  align-content:   center;
  justify-content: center;

  /* Shorthand — same result */
  place-content: center;

  /* Or different values for each axis */
  place-content: flex-start space-between;
  /*             ↑align-content  ↑justify-content */
}

/* Perfect full centring — ultra compact */
.centred {
  display:       flex;
  place-content: center;
  min-height:    100vh;
}

All Container Properties — Complete Reference

Property Default Values Controls
flex-direction row row | row-reverse | column | column-reverse Main axis direction
flex-wrap nowrap nowrap | wrap | wrap-reverse Single vs multi-line
flex-flow row nowrap <direction> <wrap> Shorthand: direction + wrap
justify-content flex-start flex-start | flex-end | center | space-between | space-around | space-evenly Main axis distribution
align-items stretch stretch | flex-start | flex-end | center | baseline Cross axis per-line alignment
align-content normal flex-start | flex-end | center | space-between | space-around | space-evenly | stretch Cross axis multi-line alignment
gap 0 <length> | <row> <col> Space between items
place-content normal <align-content> [<justify-content>] Shorthand: align-content + justify-content
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all Flexbox container and item properties with visual examples.
Download PDF

Try It Yourself

All container properties in one interactive editor. Change justify-content, align-items, flex-direction and see results instantly:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 26

Questions & Answers
Both control cross-axis alignment, but they operate at different levels:

align-items aligns items within a single flex line. In a multi-line container, each line is treated independently — items in each row are aligned relative to that row's cross size. It works on both single-line and multi-line containers.

align-content aligns the flex lines themselves as a group within the container. It only has a visible effect when: (1) flex-wrap: wrap is set AND (2) there is extra space on the cross axis (the container has a fixed height larger than the combined height of all lines). In a single-line container, align-content has no effect.

Think of it this way: imagine a bookshelf. align-items arranges the books on each shelf. align-content arranges the shelves themselves within the bookcase.
flex-flow is a shorthand that combines flex-direction and flex-wrap in one declaration. The syntax is: flex-flow: <direction> <wrap>.

So flex-flow: row wrap is exactly equivalent to writing:
flex-direction: row;
flex-wrap: wrap;

This is the most commonly used flex-flow value — a horizontal row that wraps to a new line when items don't fit. Other examples: flex-flow: column nowrap, flex-flow: row-reverse wrap.

Using the shorthand saves one line and makes the intent clear at a glance.
Using gap on the container is cleaner than adding margins to items for several reasons:

No edge case handling — With margins, you need to remove the margin from the last item (.item:last-child { margin-right: 0 }) or use negative margins on the container. With gap, space only appears between items — never at the outer edges — automatically.

Works with wrap — When items wrap to multiple lines, row-gap and column-gap control both axes independently. Margins would require complex selectors to handle first/last items per row.

Single responsibility — The container controls spacing. Items don't need to know about layout context.

Works in Grid too — Same gap syntax works identically in CSS Grid, making your code consistent.
The axes rotate when you change flex-direction. The properties themselves don't change meaning — they always align on their respective axes — but the physical directions change:

With flex-direction: row:
— Main axis = horizontal, so justify-content aligns left/right
— Cross axis = vertical, so align-items aligns top/bottom

With flex-direction: column:
— Main axis = vertical, so justify-content now aligns top/bottom
— Cross axis = horizontal, so align-items now aligns left/right

This is why justify-content: center in a column container centres items vertically — and why many developers are initially confused when switching direction. Always think in terms of main/cross axes, not fixed directions.
place-content is a shorthand for both align-content and justify-content. Syntax: place-content: <align-content> [<justify-content>]. If only one value is given, it applies to both axes.

Most useful for: full centring in compact syntax.
Instead of:
align-content: center; justify-content: center;
You can write:
place-content: center;

Combined with display: flex; min-height: 100vh, this is the most concise way to centre content in a full-screen container. It also works with different values for each axis: place-content: flex-start space-between (first value = align-content, second = justify-content).
Multiple Choice Questions
Question 1 of 5
Which property aligns multiple flex lines (rows) along the cross axis when flex-wrap: wrap is used?
Question 2 of 5
What is the shorthand property that combines flex-direction and flex-wrap?
Question 3 of 5
With flex-direction: column, which property aligns items horizontally?
Question 4 of 5
What justify-content value places equal space between items AND at the outer edges?
Question 5 of 5
What does place-content: center do?
Chapter Summary
  • flex-direction — sets the main axis. row (default), row-reverse, column, column-reverse. Axes rotate with direction change.
  • flex-wrap — nowrap (default), wrap (multi-line), wrap-reverse. Essential for responsive layouts.
  • flex-flow — shorthand for direction + wrap. Most common: flex-flow: row wrap.
  • justify-content — main axis: flex-start (default), flex-end, center, space-between, space-around, space-evenly.
  • align-items — cross axis per-line: stretch (default), flex-start, flex-end, center, baseline.
  • align-content — cross axis multi-line alignment. Only works with flex-wrap: wrap AND a fixed container height with extra space.
  • align-items vs align-content: items = per-row alignment. content = rows as a group.
  • gap — space between items (not at edges). Use instead of margins. Supports row-gap and column-gap separately. Shorthand: gap: row col.
  • place-content — shorthand for align-content + justify-content. place-content: center = perfect centring on both axes.
~9 min read
Beginner level
Chapter 26 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel