CSS Flexbox
Flexbox (the Flexible Box Layout) is a layout system for arranging items in one dimension: either a row or a column. It makes tasks that used to need floats and hacks easy: putting items side by side, spacing them evenly, centring something vertically, and making boxes grow or shrink to fill space.
Container and items
Flexbox always involves two roles:
| Role | How you create it | What you control |
|---|---|---|
| Flex container | display: flex (or inline-flex) on the parent |
Direction, wrapping, alignment, gaps |
| Flex items | Every direct child of the container, automatically | Growing, shrinking, base size, order, self-alignment |
Only direct children become flex items. Grandchildren are laid out normally unless their own parent is also a flex container.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.normal div, .flex div { background: #7e57c2; color: white; padding: 12px; margin: 4px; border-radius: 6px; }
.flex { display: flex; background: #ede7f6; }
</style>
</head>
<body>
<p>Without flex (block children stack):</p>
<div class="normal"><div>One</div><div>Two</div><div>Three</div></div>
<p>With <code>display: flex</code>:</p>
<div class="flex"><div>One</div><div>Two</div><div>Three</div></div>
</body>
</html>One declaration turns a vertical stack into a row.
Main axis and cross axis
Flexbox thinks in terms of two axes rather than "horizontal" and "vertical":
- The main axis runs in the direction items are placed — left to right for
flex-direction: row(the default), top to bottom forcolumn. - The cross axis runs perpendicular to it.
This matters because the alignment properties are named after the axes: justify-content aligns along the main axis, align-items along the cross axis. When you switch to a column, they swap visual directions.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 16px; }
.box { display: flex; justify-content: center; align-items: flex-end; height: 200px; width: 46%; background: #e0f2f1; border: 2px dashed #00897b; }
.col { flex-direction: column; }
.box div { background: #00897b; color: white; padding: 8px 12px; margin: 3px; border-radius: 4px; }
</style>
</head>
<body>
<div class="box"><div>A</div><div>B</div><div>C</div></div>
<div class="box col"><div>A</div><div>B</div><div>C</div></div>
</body>
</html>Both boxes use the same justify-content: center; align-items: flex-end;. In the row, items are centred horizontally and pushed to the bottom; in the column, they're centred vertically and pushed to the right.
Perfect centring
The most famous Flexbox trick: centring something both ways with three lines.
<!DOCTYPE html>
<html>
<head>
<style>
html, body { height: 100%; margin: 0; }
body {
display: flex;
justify-content: center; /* main axis */
align-items: center; /* cross axis */
background: linear-gradient(135deg, #42a5f5, #ab47bc);
font-family: system-ui, sans-serif;
}
.card { background: white; padding: 24px 32px; border-radius: 14px; font-size: 22px; box-shadow: 0 8px 24px rgba(0,0,0,.25); }
</style>
</head>
<body>
<div class="card">Dead centre 🎯</div>
</body>
</html>Gap and flexible sizes
The gap property puts space between items without the margin-at-the-edges problem. And flex: 1 on an item tells it to take a share of leftover space.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.bar { display: flex; gap: 10px; padding: 10px; background: #263238; border-radius: 8px; }
.bar div { background: #ffca28; padding: 12px; border-radius: 6px; text-align: center; }
.grow { flex: 1; background: #66bb6a !important; }
</style>
</head>
<body>
<div class="bar"><div>Logo</div><div class="grow">flex: 1 — I fill the rest</div><div>Menu</div></div>
</body>
</html>What's in the next lessons
| Lesson | Covers |
|---|---|
| Flex Container | flex-direction, flex-wrap, flex-flow, justify-content, align-items, align-content, gap |
| Flex Items | flex-grow, flex-shrink, flex-basis, flex, order, align-self |
Tip: Use Flexbox for one-dimensional layouts (a nav bar, a row of buttons, a card's internal layout). When you need rows and columns aligned together, reach for CSS Grid.
Test yourself
Which elements become flex items when you set display: flex on a container?
- All descendants
- Only direct children
- Only the first child
- Only elements with class flex-item
With flex-direction: row, which property aligns items vertically?
- justify-content
- align-items
- text-align
- vertical-align
Which pair of declarations centres a single child both horizontally and vertically in a flex container with a height?
- text-align: center; vertical-align: middle
- justify-content: center; align-items: center
- margin: auto; float: center
- align-content: center; order: 0