Lesson 19 of 29 · Layout with Flexbox
CSS Flexbox Basics: Line Up Items in a Row
Flexbox is the layout tool you'll reach for most often in CSS. One property on a parent element lines its children up in a row or a column, and a few more control how they are spaced and aligned. Navigation bars, button rows and perfectly centred hero sections are all just a few lines of flexbox.
Flex containers and flex items
Write display: flex on a parent element. The parent becomes a flex container, and its direct children become flex items that line up side by side:
<div class="toolbar">
<button>Save</button>
<button>Share</button>
<button>Delete</button>
</div>
.toolbar {
display: flex;
gap: 12px; /* space between the items */
}
Each item is as wide as its content. Only the direct children become flex items; elements nested deeper keep their normal layout. Inline children such as links become flex items too, so width and height work on them just like on blocks.
gap adds space between the items, never before the first one or after the last one. That's much cleaner than giving every item a margin.
Main axis and cross axis
flex-direction sets the main axis, the direction in which the items flow:
row(the default): from left to rightcolumn: from top to bottomrow-reverseandcolumn-reverse: the same, backwards
The cross axis runs at a right angle to the main axis. In a row it is vertical; in a column it is horizontal. Remember this, because the two alignment properties follow the axes, not "horizontal" and "vertical".
justify-content and align-items
justify-content distributes the items along the main axis: flex-start (the default), center, flex-end, space-between (first item at the start, last item at the end, equal space in between), space-around and space-evenly.
align-items aligns the items on the cross axis: stretch (the default, items fill the container's height in a row), flex-start, center, flex-end and baseline.
.navbar {
display: flex;
justify-content: space-between; /* logo on the left, menu on the right */
align-items: center; /* both vertically centred */
}
Centring anything
This is the famous centring recipe: make the parent a flex container and centre on both axes. The container needs some height of its own, otherwise there is no free space to centre in:
.hero {
display: flex;
flex-direction: column;
justify-content: center; /* main axis = vertical in a column */
align-items: center; /* cross axis = horizontal */
min-height: 400px;
}
Common mistakes
- Putting
display: flexon the items. It belongs on the parent whose children you want to arrange. - Mixing up the axes. After
flex-direction: column,justify-contentworks vertically andalign-itemshorizontally. - Centring in a container without extra height. Vertical centring only shows when the container is taller than its content, for example thanks to
min-height.
Assignment
Build a centred hero section with a row of buttons:
- Make
.heroa flex container (display: flex) that stacks its items in a column (flex-direction: column) and centres them horizontally and vertically (align-items: centerandjustify-content: center). The starter already gives the hero amin-height. - Make
.actionsa flex container so the three buttons sit side by side in a row with a12pxgap between them.
Write your CSS below the comment in the <style> element.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .