Flexbox: axes, alignment and distribution
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Align and distribute items along the main and cross axes.
- Control how flex items grow, shrink and wrap.
Overview
#Flexbox arranges children along one axis. Set display: flex on the container, and the main axis defaults to horizontal. justify-content distributes items along the main axis; align-items aligns them on the cross axis. The mental model is two axes, and every property belongs to one of them.
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.5rem;
}
Items can grow and shrink. flex-grow decides how leftover space is shared, flex-shrink decides how items give up space when there is not enough, and flex-basis sets the starting size. The shorthand flex: 1 means grow and shrink equally from a zero basis, which is the common way to make items share space. flex: 1 on one child and no flex on another pins the second child and lets the first fill the rest.
.row { display: flex; }
.row .grow { flex: 1 1 0; }
.row .fixed { flex: 0 0 auto; }
Wrapping is opt-in. By default items stay on one line and may overflow or shrink. flex-wrap: wrap lets them move to new lines, which is the basis of many responsive card grids. gap adds spacing between items without margins, which avoids margin-collapsing and edge cases.
A frequent bug is a flex item that refuses to shrink below its content size. The default min-width: auto on flex items prevents shrinking past the content. Set min-width: 0 on the item, or min-width: 0 on the text container, to allow truncation with text-overflow: ellipsis.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Build a row where a label stays its natural size and a value fills the remaining space, vertically centered with a 0.5rem gap.
starter.css<div class="row"> <span class="label">Total</span> <span class="value">120</span> </div>Reveal the reference answer
reference.css.row { display: flex; align-items: center; gap: 0.5rem; } .label { flex: 0 0 auto; } .value { flex: 1 1 0; text-align: right; }Authored expected output: The label keeps its size and the value expands to fill the row, both vertically centered.
flex: 0 0 auto pins the label and flex: 1 1 0 lets the value absorb the free space.
Exercise 2Debug
This flex item will not truncate with an ellipsis; the text overflows instead. Fix it.
starter.css.row { display: flex; gap: 1rem; } .title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }Reveal the reference answer
reference.css.row { display: flex; gap: 1rem; } .title { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }Authored expected output: The title truncates with an ellipsis instead of overflowing.
Flex items default to min-width: auto, which prevents shrinking below content; min-width: 0 allows truncation.
Common misconceptions
#- flex-grow values are absolute sizes.
- Flex items shrink below their content size by default.
Follow-up practice
#- html-css-19-css-grid
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.