Flexbox Layout
Arrange elements along a row or column with Flexbox, controlling alignment, spacing, and wrapping with a handful of properties.
What you'll learn
- Turn a container into a flex container with display: flex
- Control alignment along the main and cross axes with justify-content and align-items
- Control wrapping and spacing with flex-wrap and gap
Prerequisites
Explanation
Before Flexbox, centering a box vertically or evenly spacing a row of navigation links required a pile of workarounds. Flexbox turns those everyday layout problems into a small, learnable set of properties applied to a container and its direct children.
Turning On Flex
Adding display: flex; to an element does two things at once: it becomes a flex container, and every one of its direct children instantly becomes a flex item, lined up along a single row by default — no floats, no manual positioning required.
Two Axes
Flexbox thinks in terms of two perpendicular axes:
- The main axis runs in the direction items are laid out — a row, left to right, by default. Set
flex-direction: columnto make the main axis run top to bottom instead. - The cross axis runs perpendicular to the main axis.
This distinction matters because two different properties control each axis:
justify-contentaligns items along the main axis — common values includeflex-start,center,space-between(push items to the edges, evenly space the rest), andspace-around.align-itemsaligns items along the cross axis — common values includeflex-start,center, andstretch(the default, filling the full cross-axis size).
If you set flex-direction: column, the axes swap roles: justify-content now controls vertical spacing, and align-items controls horizontal alignment. Keeping "main axis = direction of flow" straight in your head makes both properties predictable regardless of direction.
Spacing and Wrapping
gapadds consistent space between flex items, without needing individual margins on each one — much simpler than the old margin-based tricks.flex-wrap: wrapallows items to drop onto a new line when they run out of room on the main axis; the default,nowrap, forces everything onto a single line, shrinking or overflowing instead.
Sizing Individual Items
Beyond arranging items as a group, each flex item can be told how to share leftover space using the flex shorthand (commonly flex: 1 to let an item grow and fill available space) or fixed with flex: none. Early on, you'll get a long way with just display: flex, justify-content, align-items, and gap on the container — those four cover the overwhelming majority of everyday layouts like navigation bars, button groups, and centered content.
Flexbox is deliberately one-dimensional — it's built for arranging items along a single row or column. When you need rows and columns to work together as a grid, that's a job for CSS Grid, covered next.
Flexbox's two axes
A flex container has a main axis (a row by default, running left to right) controlled by justify-content, and a perpendicular cross axis (a column by default, running top to bottom) controlled by align-items. Setting flex-direction: column swaps which axis is which, so justify-content then controls vertical spacing and align-items controls horizontal alignment.
Example
A navigation bar built with Flexbox: a logo and a link list spaced apart and vertically centered.
<!doctype html>
<html>
<head>
<style>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
background: #222;
}
.navbar .logo {
color: white;
font-weight: bold;
}
.navbar ul {
display: flex;
gap: 16px;
list-style: none;
margin: 0;
padding: 0;
}
.navbar a {
color: white;
text-decoration: none;
}
</style>
</head>
<body>
<div class="navbar">
<div class="logo">BrightPath</div>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#courses">Courses</a></li>
<li><a href="#about">About</a></li>
</ul>
</div>
</body>
</html>Try it yourself
Change justify-content to center or flex-end and press Run to see the layout shift.
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Guided exercise
Guided exercise
Turn #container into a flex container that centers its children horizontally: add display: flex; and justify-content: center;
Checks: #container has display: flex · plus 1 hidden check
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Stuck? Get a hint.
Independent exercise
Independent exercise
Make #row a flex container whose three .item children lay out left to right, spaced apart with justify-content: space-between and vertically centered with align-items: center.
Checks: #row is a flex container · #row uses justify-content: space-between · plus 1 hidden check
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Stuck? Get a hint.
Common mistakes
- Forgetting that justify-content works along the main axis and align-items works along the cross axis — flex-direction swaps which is which.
- Adding display: flex to the wrong element — it must go on the parent container, not the children you're trying to arrange.
- Expecting flex items to wrap onto a new line automatically — by default flex-wrap is nowrap, so items shrink or overflow instead of wrapping.
- Reaching for margin: auto tricks or manual positioning to center content, when a single justify-content/align-items pair on the container does it directly.
Knowledge check
Takeaway
Flexbox turns 'please arrange these boxes in a row and space them nicely' from a pile of hacks into two or three clear CSS properties.
Summary
Setting display: flex on a container arranges its direct children along a main axis. justify-content controls spacing along that main axis, align-items controls alignment along the perpendicular cross axis, and gap plus flex-wrap handle spacing and wrapping — together covering most everyday one-dimensional layouts.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.