CSS Flex Container
In this page:
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.
- 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
<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.
- 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
<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.
- 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
<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.
- 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
<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.
- 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
<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>
- Using
align-itemswhen you needjustify-content, since they work on different axes. - Forgetting that
flex-direction: columnswaps the main axis, so the two properties swap roles. - Expecting
align-contentto work on a single-line container, when it needsflex-wrap: wrapand several lines.
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: