6 / 9 · Basic
Flexbox
Before flexbox, centring a box vertically took tricks. With display: flex, it takes two declarations: justify-content and align-items.
Jump to the 10 practice questionsYou will learn
- Lay boxes along one line with display: flex
- Align on the main and cross axes and wrap with gap
- Share space with flex-grow, -shrink and -basis
Before this: Typography
1Explain
One row or one column
Flexbox, the CSS Flexible Box Layout module, arranges a group of boxes along one line. Put display: flex on a container and its direct children, the flex items, line up side by side instead of stacking. That one declaration is enough for a menu bar, a row of buttons, or a card whose footer should stick to the bottom. Flexbox works in one dimension at a time: either a row or a column.
In a row, the main axis runs left to right (in English) and the cross axis top to bottom. justify-content works along the main axis, align-items along the cross axis.
2Explore
Direction and justify-content
Everything in flexbox is described in terms of two axes. The main axis is the direction the items flow, set by flex-direction: row (the default) or column. The cross axis runs at right angles to it. justify-content distributes the items along the main axis: flex-start, center, flex-end, space-between (first and last item at the edges, equal gaps between) or space-around.
justify-content works along the direction the items flow, so in a column it moves them up and down.
Tap a choice: the code and the preview change together.
<style>
.row { display: flex; flex-direction: row; justify-content: flex-start; align-items: center; gap: 8px; height: 150px; background: #fff4d6; }
.row div { background: #e8eefa; padding: 6px 12px; }
</style>
<div class="row"><div>Bread</div><div>Cakes</div><div>Visit</div></div>Nothing is drawn: there is no visible content.
3Problem
Spread items in a row
In a row, which property spreads items horizontally?
Hint
In a row the main axis runs left to right.
Solution
Answer: justify-content
justify-content works along the main axis, which is horizontal in a row.
4Problem
justify-content in a column
With flex-direction: column, what does justify-content control?
Hint
The main axis turns with flex-direction.
Solution
Answer: Vertical distribution
justify-content follows the main axis. With flex-direction: column, that axis runs vertically, so it distributes space from top to bottom.
5Explain
Gaps and wrapping
gap puts space between items without adding any at the outer edges, which is what you usually want and what margins make awkward. By default, flex items stay on one line and shrink to fit, even if that squashes them. flex-wrap: wrap lets them move onto a new line when there is not enough room. So a row of four cards on a laptop can become two rows of two on a tablet and a single column on a phone, with no media query.
.bar {
display: flex;
justify-content: space-between; /* logo left, menu right */
align-items: center; /* vertically centred */
gap: 16px; /* space between items */
}
.cards { display: flex; flex-wrap: wrap; gap: 12px; }
.card { flex: 1 1 200px; } /* grow, shrink, start at 200px */6Explore
How items grow
The flex shorthand on an item takes three values. flex-grow says how much of the leftover space the item takes compared with its siblings; flex-shrink says how willingly it shrinks when space runs out; flex-basis is its starting size before growing or shrinking.
With flex-basis 0, the leftover space is shared in proportion to flex-grow: 2 against 1 and 1 gives Big half.
Tap a choice: the code and the preview change together.
<style>
.row { display: flex; background: #fff4d6; }
.row div { flex: 1 1 0; background: #e8eefa; border: 1px solid #fff; padding: 8px; }
.row .big { flex-grow: 1; }
</style>
<div class="row"><div class="big">Big</div><div>B</div><div>C</div></div>Nothing is drawn: there is no visible content.
7Problem
Four equal items
Four items have flex: 1 in a 400px container with no gaps. How wide is each?
px
Hint
400 shared four ways.
Solution
Answer: 100 px
flex: 1 means a basis of 0 and equal growth: 400 / 4 = 100.
8Problem
flex-grow 2 against 1 and 1
Three items with flex-basis 0 share 400px. One has flex-grow 2, the others 1. How wide is the big one?
px
Hint
There are 4 shares in total.
Solution
Answer: 200 px
Shares 2 + 1 + 1 = 4; 400 times 2/4 = 200.
9Problem
Visual order and keyboard order
Flexbox can change the order items appear in, with flex-direction: row-reverse or the order property, without touching the HTML. Use this with care. Keyboard focus and screen readers still follow the order in the HTML, so if the screen shows Contact before Home but Tab visits Home first, keyboard users get lost. When the order matters to meaning, change it in the HTML.
The order property puts Contact first on screen, but it is last in the HTML. What still visits it last?
Hint
The keyboard follows the HTML.
Solution
Answer: Keyboard focus and screen readers
They follow the HTML order, so visual and real order disagree.
10Practice
A header bar
Make .bar a flex container with the name on the left and the menu on the right (space-between), vertically centred. Then make .menu a flex row with a 16px gap.
- .bar is flex
- space-between
- centred vertically
- .menu is flex
- 16px gap
<style>
.bar { background: #fff4d6; padding: 8px 16px; }
.bar ul { list-style: none; margin: 0; padding: 0; }
</style>
<header class="bar">
<strong>Corner Bakery</strong>
<ul class="menu">
<li><a href="#">Bread</a></li>
<li><a href="#">Cakes</a></li>
<li><a href="#">Visit</a></li>
</ul>
</header>Hint
Two flex containers: .bar for the whole header, .menu for the links.
A solution
<style>
.bar { background: #fff4d6; padding: 8px 16px; display: flex; justify-content: space-between; align-items: center; }
.menu { display: flex; gap: 16px; }
.bar ul { list-style: none; margin: 0; padding: 0; }
</style>
<header class="bar">
<strong>Corner Bakery</strong>
<ul class="menu">
<li><a href="#">Bread</a></li>
<li><a href="#">Cakes</a></li>
<li><a href="#">Visit</a></li>
</ul>
</header>11Practice
Cards that wrap
Make .cards a wrapping flex container with a 12px gap, and give each .card flex: 1 1 120px. The cards move onto a new line whenever there is no room for 120 pixels each.
- flex container
- flex-wrap: wrap
- 12px gap
- cards grow
- basis 120px
<style>
.card { background: #e8eefa; padding: 12px; }
</style>
<div class="cards">
<div class="card">Sourdough</div>
<div class="card">Rye</div>
<div class="card">Baguette</div>
<div class="card">Brioche</div>
</div>Hint
flex-wrap: wrap lets items move onto a new line.
A solution
<style>
.card { background: #e8eefa; padding: 12px; }
.cards { display: flex; flex-wrap: wrap; gap: 12px; }
.card { flex: 1 1 120px; }
</style>
<div class="cards">
<div class="card">Sourdough</div>
<div class="card">Rye</div>
<div class="card">Baguette</div>
<div class="card">Brioche</div>
</div>12Explain
Common mistakes
<style>
.menu li { display: flex; gap: 16px; }
</style>
<ul class="menu"><li>Bread</li><li>Cakes</li></ul><style>
.menu { display: flex; gap: 16px; list-style: none; padding: 0; }
</style>
<ul class="menu"><li>Bread</li><li>Cakes</li></ul>display: flex goes on the container. On each li it only arranges the li's own content.
13
Summary
- display: flex lines up the direct children along the main axis.
- justify-content on the main axis, align-items on the cross axis, gap between.
- flex: grow shrink basis; flex-wrap: wrap for rows that reflow; keep visual order equal to HTML order.