CSS Flexbox Complete Guide: Rows, Columns, Alignment, Wrapping, flex-grow and Real Navbar and Card Examples
What Is Flexbox?
Flexbox is a layout system for arranging items in one direction (a row or a column). It handles alignment, spacing and flexible sizing very well.
css
.container {
display: flex; /* children become flex items */
}
Two Axes
| Axis | Meaning | Controlled by |
|---|---|---|
| Main axis | The direction items flow (row: left to right) | flex-direction, justify-content |
| Cross axis | Perpendicular to the main axis | align-items, align-content |
Container Properties
| Property | Values | Effect |
|---|---|---|
flex-direction | row, column, row-reverse | Direction of the main axis |
justify-content | flex-start, center, space-between, space-around, space-evenly | Spacing along the main axis |
align-items | stretch, center, flex-start, flex-end, baseline | Alignment on the cross axis |
flex-wrap | nowrap, wrap | Allow items to go to a new line |
gap | 1rem | Space between items (no margin hacks) |
Center Anything
css
.center {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
Item Properties
css
.item {
flex: 1 1 200px; /* grow shrink basis */
}
.sidebar { flex: 0 0 260px; } /* fixed width, never grows or shrinks */
.main { flex: 1; } /* takes the remaining space */
.special { order: -1; } /* visually moves first */
.push { margin-left: auto; }/* pushes this item to the far end */
| Part | Meaning |
|---|---|
flex-grow | How much extra space this item takes (0 means none) |
flex-shrink | How much it can shrink when space is short |
flex-basis | Starting size before growing or shrinking |
Example 1: Responsive Navbar
html
<header class="site-header">
<a class="logo" href="/">Sunrise</a>
<nav>
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="/cakes.html">Cakes</a></li>
<li><a href="/contact.html">Contact</a></li>
</ul>
</nav>
<a class="cta" href="/order.html">Order</a>
</header>
css
.site-header {
display: flex;
align-items: center;
gap: 1.5rem;
padding: 1rem 1.5rem;
}
.menu {
display: flex;
gap: 1rem;
list-style: none;
margin: 0;
padding: 0;
}
.cta { margin-left: auto; } /* push the button to the right */
Example 2: Wrapping Card Row
css
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.cards > .card {
flex: 1 1 16rem; /* each card is at least about 16rem wide, then grows */
}
The cards sit in a row on wide screens and wrap automatically on small screens, with no media query.
Flexbox vs Grid Use Flexbox for one-dimensional layouts such as navbars, toolbars and card rows. Use Grid for two-dimensional page layouts. They work together.
Practice
- Build the navbar and push the call to action to the right
- Center a card both ways on the screen
- Make 6 cards that wrap on narrow screens
Conclusion
Flexbox lines items up along one axis. Use justify-content, align-items, gap and flex to build navbars, centered boxes and wrapping card rows.
Quick Summary: Idea; axes; container; centering; items; navbar; cards; vs grid.

