← Back to CSS Course | Chapter 10: Flexbox | Lesson 3 of 5

CSS Flex Container

The flex container is the stage director: it never appears in the show itself, but every instruction it gives (direction, spacing, alignment) controls how the performers (flex items) are arranged.
Syntax
css
selector {
  display: flex;
  flex-direction: row | column;
  flex-wrap: nowrap | wrap;
  justify-content: flex-start | center | space-between;
  align-items: stretch | center;
}

flex-direction

The flex-direction property sets the main axis: row (default, left to right), row-reverse, column (top to bottom), or column-reverse.

Changing this doesn't just reverse visual order — it swaps which axis justify-content and align-items operate on, since "main axis" always follows flex-direction.

Note:
  • Accepted values:
  • flex-direction: row | row-reverse | column | column-reverse — main axis direction, row is default
  • flex-wrap: nowrap | wrap | wrap-reverse
  • justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly
  • align-items: stretch | flex-start | flex-end | center | baseline
  • display: flex | inline-flex
  • gap: <length> — space between items
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: flex-direction

css
<style>
.container {
  display: flex;
  flex-direction: column;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.container div {
  background: dodgerblue;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>Top</div>
  <div>Bottom</div>
</div>

flex-wrap

By default flex items stay on a single line and shrink to fit (nowrap). Setting flex-wrap: wrap allows items to flow onto additional lines when they no longer fit, which is essential for responsive card grids; wrap-reverse does the same but stacks new lines above instead of below.

Note:
  • Accepted values:
  • nowrap — default, all items on one line
  • wrap — items wrap onto extra lines
  • wrap-reverse — wraps, with lines stacked in reverse
  • note: applies to the flex container
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: flex-wrap

css
<style>
.container {
  display: flex;
  flex-wrap: wrap;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.item {
  width: 150px;
  background: coral;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

flex-flow shorthand

The flex-flow property combines flex-direction and flex-wrap into one declaration, e.g. flex-flow: row wrap. It is purely a convenience shorthand — using the two longhand properties separately produces identical results, so pick whichever reads clearer in your stylesheet.

Note:
  • Accepted values:
  • flex-flow: <direction> <wrap> — shorthand order
  • flex-flow: row nowrap — default
  • flex-flow: column wrap — vertical wrapping
  • flex-flow: row-reverse wrap-reverse — both reversed
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: flex-flow shorthand

css
<style>
.container {
  display: flex;
  flex-flow: row wrap;
  background: #eef;
  padding: 10px;
  gap: 8px;
}
.container div {
  background: coral;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>A</div>
  <div>B</div>
</div>

justify-content and align-items

justify-content distributes extra space along the main axis (flex-start, center, space-between, space-around, space-evenly), while align-items aligns items along the cross axis (stretch by default, plus flex-start, center, flex-end, baseline).

These two properties together solve the vast majority of real-world centering and spacing problems.

Note:
  • Accepted values:
  • justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly — main axis
  • align-items: stretch | flex-start | flex-end | center | baseline — cross axis
  • justify-content default — flex-start
  • align-items default — stretch
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: justify-content and align-items

css
<style>
.container {
  display: flex;
  height: 150px;
  justify-content: center;
  align-items: center;
  background: #eef;
}
.container div {
  background: dodgerblue;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div>Centered both ways</div>
</div>

align-content for Multi-Line Wrapping

align-content only has a visible effect once a flex container has wrapped onto multiple lines (via flex-wrap: wrap) and has leftover cross-axis space — it controls how those lines are distributed, not how items within a single line align (that's align-items' job).

It's a commonly confused property precisely because it does nothing on a single-line flex row.

Note:
  • Accepted values:
  • normal | stretch — default, lines stretch to fill the container
  • flex-start | flex-end | center — packs lines
  • space-between | space-around | space-evenly — distributes lines
  • note: only affects multi-line (wrapped) containers
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: align-content for Multi-Line Wrapping

css
<style>
.container {
  display: flex;
  flex-wrap: wrap;
  height: 200px;
  align-content: space-between;
  background: #eef;
}
.item {
  width: 150px;
  background: coral;
  color: white;
  padding: 10px;
}
</style>
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using align-items when you need justify-content, since they work on different axes.
  2. Forgetting that flex-direction: column swaps the main axis, so the two properties swap roles.
  3. Expecting align-content to work on a single-line container, when it needs flex-wrap: wrap and several lines.
Browser Support

Unprefixed Flexbox is supported in Chrome 29+, Firefox 28+, Safari 9+ and Edge 12+.

🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.