Lesson 21 of 296
Flexbox
What you will learn
- What Flexbox is for
- Containers, items and the two axes
- The properties for the container
- The properties for the items
- How
flex: 1really works - Common layouts built with Flexbox
On this page
- What is Flexbox?
- Container and items
- The two axes
- Container properties
- justify-content examples
- Perfect centring
- Item properties
- How flex: 1 works
- Common layout: header bar
- Push one item to the end
- Wrapping cards
- A flex item that will not shrink
- A warning about order
- Common mistakes
- How to say it in an interview
- Key words
What is Flexbox?
Flexbox is a layout system for arranging items in one direction: a row or a column. It makes it easy to line things up, share space, and centre things. It is perfect for navigation bars, rows of buttons, cards in a row, and centring.
Container and items
You turn on Flexbox on a parent. Its direct children become flex items.
.nav {
display: flex;
}<nav class="nav">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>The three links now sit side by side in a row. Only the direct children are flex items. Their own children are not affected.
The two axes
- The main axis is the direction the items are placed in. By default it goes left to right.
- The cross axis is the other direction, at a right angle. By default it goes top to bottom.
flex-direction sets the main axis:
| Value | Main axis |
|---|---|
row (default) | Left to right |
row-reverse | Right to left |
column | Top to bottom |
column-reverse | Bottom to top |
Important: when you switch to column, the axes swap. justify-content then works vertically and align-items works horizontally.
Container properties
| Property | Controls | Common values |
|---|---|---|
justify-content | Position along the main axis | flex-start, center, flex-end, space-between, space-around, space-evenly |
align-items | Position along the cross axis | stretch (default), center, flex-start, flex-end, baseline |
gap | Space between items | 16px, 1rem 2rem (row gap, column gap) |
flex-wrap | Whether items can go onto a new line | nowrap (default), wrap |
align-content | How the lines are spaced when there are several lines | Same values as justify-content |
justify-content examples
For a row of three boxes:
flex-start [A][B][C]
center [A][B][C]
flex-end [A][B][C]
space-between [A] [B] [C]
space-evenly [A] [B] [C] Perfect centring
The most famous Flexbox trick:
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100dvh;
}The child is centred both horizontally and vertically.
Item properties
| Property | Controls |
|---|---|
flex-grow | How much of the extra space this item takes. 0 = none (default). |
flex-shrink | How much it shrinks when there is not enough space. 1 = can shrink (default). |
flex-basis | The starting size before growing or shrinking. auto (default) uses the item's width. |
flex | Shorthand for the three above |
align-self | Overrides align-items for this one item |
order | Changes the visual order. Default 0. |
How flex: 1 works
.item { flex: 1; }This means flex-grow: 1; flex-shrink: 1; flex-basis: 0%. The starting size is zero, then all the space is shared by the grow numbers. If every item has flex: 1, they get equal widths.
.sidebar { flex: 0 0 250px; }
.content { flex: 1; }The sidebar stays 250px (no grow, no shrink). The content takes all the rest.
.a { flex: 1; }
.b { flex: 2; }.b gets twice as much space as .a.
Common layout: header bar
.header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}<header class="header">
<a href="/" class="logo">Acme</a>
<nav>...</nav>
</header>Logo on the left, menu on the right, both centred vertically.
Push one item to the end
.toolbar { display: flex; gap: 8px; }
.toolbar .logout { margin-left: auto; }margin-left: auto takes all the free space on its left, so the logout button is pushed to the far right.
Wrapping cards
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card { flex: 1 1 250px; }Each card wants to be 250px, can grow and shrink, and moves to a new line when there is not enough room.
A flex item that will not shrink
Flex items will not shrink smaller than their content by default (min-width: auto). Long words or wide content can overflow. Fix:
.item { min-width: 0; }This is a very common hidden bug, especially with text truncation inside flex items.
A warning about order
order and row-reverse change only what you see. The keyboard Tab order and screen readers still follow the HTML order. Do not use them to fix content that is in the wrong order in your HTML.
Common mistakes
- Putting
display: flexon the item instead of the parent. - Mixing up
justify-contentandalign-items, especially after switching tocolumn. - Using margins between items instead of
gap. - Forgetting
flex-wrap: wrap, so items squash instead of moving to a new line. - Text truncation failing in a flex item because
min-width: 0is missing.
How to say it in an interview
"Flexbox is a one-dimensional layout system. You set display flex on the container and its direct children become items. justify-content aligns items along the main axis, align-items along the cross axis, and flex-direction decides which way the main axis goes. flex: 1 means grow 1, shrink 1, basis 0, so items with flex 1 share space equally. I use gap for spacing and flex-wrap to let items move to new lines."
Key words
| Word | Meaning |
|---|---|
| Flex container | The parent with display: flex |
| Flex item | A direct child of a flex container |
| Main axis | The direction items are placed in |
| Cross axis | The direction at a right angle to the main axis |
| justify-content | Alignment on the main axis |
| align-items | Alignment on the cross axis |
| gap | Space between items |
| flex-grow / shrink / basis | How an item grows, shrinks and starts |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.