CSS Flexbox Complete Guide: Rows, Columns, Alignment, Wrapping, flex-grow and Real Navbar and Card Examples

Web Designing 60 min min read Updated: Nov 17, 2026 Beginner
CSS Flexbox Complete Guide: Rows, Columns, Alignment, Wrapping, flex-grow and Real Navbar and Card Examples
Beginner Topic 1 of 3

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

AxisMeaningControlled by
Main axisThe direction items flow (row: left to right)flex-direction, justify-content
Cross axisPerpendicular to the main axisalign-items, align-content

Container Properties

PropertyValuesEffect
flex-directionrow, column, row-reverseDirection of the main axis
justify-contentflex-start, center, space-between, space-around, space-evenlySpacing along the main axis
align-itemsstretch, center, flex-start, flex-end, baselineAlignment on the cross axis
flex-wrapnowrap, wrapAllow items to go to a new line
gap1remSpace 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 */
PartMeaning
flex-growHow much extra space this item takes (0 means none)
flex-shrinkHow much it can shrink when space is short
flex-basisStarting 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

  1. Build the navbar and push the call to action to the right
  2. Center a card both ways on the screen
  3. 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators