CSS Flex Container
The properties in this lesson go on the flex container — the element with display: flex. They decide which way items flow, whether they wrap onto new lines, and how they're distributed and aligned.
Container properties at a glance
| Property | Values | Purpose |
|---|---|---|
flex-direction |
row, row-reverse, column, column-reverse |
Direction of the main axis |
flex-wrap |
nowrap, wrap, wrap-reverse |
Whether items may wrap to new lines |
flex-flow |
e.g. row wrap |
Shorthand for direction + wrap |
justify-content |
flex-start, flex-end, center, space-between, space-around, space-evenly |
Distribute items on the main axis |
align-items |
stretch, flex-start, flex-end, center, baseline |
Align items on the cross axis |
align-content |
same as justify-content + stretch |
Distribute lines when wrapping |
gap, row-gap, column-gap |
lengths | Space between items |
flex-direction
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 13px; }
.c { display: flex; background: #e3f2fd; padding: 4px; gap: 4px; }
.c span { background: #1e88e5; color: white; padding: 6px 10px; border-radius: 4px; }
.r { flex-direction: row; }
.rr { flex-direction: row-reverse; }
.co { flex-direction: column; }
.cr { flex-direction: column-reverse; }
</style>
</head>
<body>
<div>row<div class="c r"><span>1</span><span>2</span><span>3</span></div></div>
<div>row-reverse<div class="c rr"><span>1</span><span>2</span><span>3</span></div></div>
<div>column<div class="c co"><span>1</span><span>2</span><span>3</span></div></div>
<div>column-reverse<div class="c cr"><span>1</span><span>2</span><span>3</span></div></div>
</body>
</html>Note: The
-reversevalues change only the visual order. Screen readers and the Tab key still follow the HTML order, so don't rely on them for meaningful reordering.
flex-wrap
By default (nowrap) items squeeze onto one line, shrinking if needed. With wrap, items that don't fit move to a new line. Resize the frame to watch them reflow.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.c { display: flex; gap: 6px; background: #fff3e0; padding: 6px; margin-bottom: 10px; }
.wrap { flex-wrap: wrap; }
.c div { width: 90px; background: #fb8c00; color: white; padding: 10px 0; text-align: center; border-radius: 4px; }
</style>
</head>
<body>
nowrap (items shrink):
<div class="c"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>
wrap (items keep 90px):
<div class="c wrap"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>
</body>
</html>justify-content
Distributes free space along the main axis.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 12px; margin: 6px; }
.c { display: flex; background: #f3e5f5; margin: 2px 0 6px; padding: 3px; }
.c i { width: 36px; height: 18px; background: #8e24aa; border-radius: 3px; }
</style>
</head>
<body>
flex-start<div class="c" style="justify-content:flex-start"><i></i><i></i><i></i></div>
center<div class="c" style="justify-content:center"><i></i><i></i><i></i></div>
flex-end<div class="c" style="justify-content:flex-end"><i></i><i></i><i></i></div>
space-between<div class="c" style="justify-content:space-between"><i></i><i></i><i></i></div>
space-around<div class="c" style="justify-content:space-around"><i></i><i></i><i></i></div>
space-evenly<div class="c" style="justify-content:space-evenly"><i></i><i></i><i></i></div>
</body>
</html>space-between puts no space at the edges; space-around gives each item equal space on both sides (so edges get half); space-evenly makes every gap identical.
align-items
Aligns items on the cross axis within their line. The default, stretch, makes items as tall as the container — that's why flex columns are automatically equal height.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; font-size: 12px; }
.c { display: flex; height: 120px; background: #e8f5e9; gap: 3px; padding: 3px; }
.c div { background: #43a047; color: white; padding: 4px; border-radius: 3px; }
.c div:nth-child(2) { font-size: 22px; }
</style>
</head>
<body>
<div>stretch<div class="c" style="align-items:stretch"><div>a</div><div>b</div></div></div>
<div>flex-start<div class="c" style="align-items:flex-start"><div>a</div><div>b</div></div></div>
<div>center<div class="c" style="align-items:center"><div>a</div><div>b</div></div></div>
<div>flex-end<div class="c" style="align-items:flex-end"><div>a</div><div>b</div></div></div>
<div>baseline<div class="c" style="align-items:baseline"><div>a</div><div>b</div></div></div>
</body>
</html>baseline lines up the text baselines, which is useful when items have different font sizes.
align-content and gap
align-content only does something when items wrap onto several lines and the container has spare height. It distributes the lines themselves, just as justify-content distributes items.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 10px; font-size: 12px; }
.c { display: flex; flex-wrap: wrap; height: 200px; width: 150px; background: #fce4ec; row-gap: 6px; column-gap: 6px; padding: 4px; }
.c div { width: 40px; height: 30px; background: #d81b60; border-radius: 3px; }
</style>
</head>
<body>
<div>flex-start<div class="c" style="align-content:flex-start"><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
<div>space-between<div class="c" style="align-content:space-between"><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
</body>
</html>Tip:
flex-flow: row wrapis shorthand forflex-direction: row; flex-wrap: wrap;.
Test yourself
Which value of justify-content puts equal space between items but none at the outer edges?
- space-around
- space-evenly
- space-between
- center
When does align-content have a visible effect?
- Always
- Only when items wrap onto multiple lines and there is extra cross-axis space
- Only with flex-direction: column
- Only on a single item
Why are flex items in a row equal height by default?
- Because of flex-grow
- Because align-items defaults to stretch
- Because of gap
- They aren't