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.
flex-direction with
all 4 valuesflex-wrap and the flex-flow shorthandjustify-content values
with live demosalign-items valuesalign-content — multi-line
alignmentgap, row-gap, column-gap and place-contentAll Container Properties — Quick Reference
These are all the properties applied to the flex container (the
parent with display: flex):
row, row-reverse, column, column-reverse.nowrap, wrap, wrap-reverse.flex-direction + flex-wrap together.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:
1 appears at right — order is reversed visually
1 appears at bottom
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.
.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:
First line appears at the bottom
flex-flow: row wrap = direction + wrap
.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:
Where S = one unit of space. between = no edge space, around = half-unit at edges, evenly = equal space everywhere including edges.
.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:
tall
tall
tall
tall
.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-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).
.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:
.item { margin-right: 10px; }
.item:last-child {
margin-right: 0; /* edge case! */
}
.container {
display: flex;
gap: 10px; /* that's it */
}
.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: <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 |
Try It Yourself
All container properties in one interactive editor. Change justify-content, align-items, flex-direction and see results instantly:
Quick Check — Chapter 26
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.
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.
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/bottomWith
flex-direction: column:— Main axis = vertical, so
justify-content now
aligns top/bottom— Cross axis = horizontal, so
align-items now
aligns left/rightThis 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).
flex-wrap: wrap is used?flex-direction and flex-wrap?flex-direction: column, which property
aligns items horizontally?justify-content value places equal space
between items AND at the outer edges?place-content: center do?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 withflex-wrap: wrapAND 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. Supportsrow-gapandcolumn-gapseparately. Shorthand:gap: row col.place-content— shorthand for align-content + justify-content.place-content: center= perfect centring on both axes.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.