~/TechPurAI
~/tutorials/css-from-scratch/flexbox-fundamentals
intermediate·part 9 of 22·3 min read

Flexbox fundamentals: building the real header and navigation

Updated Aug 16, 2026CSS

Every layout so far has relied on the real, default block and inline flow from part 8. Flexbox is CSS's real, purpose-built system for arranging elements along a single row or column — starting here with Bright Leaf Coffee's actual header, which needs its logo and navigation links positioned deliberately, not just stacked in default document order.

The real header this part builds

html
<header class="site-header">
  <img src="/images/logo.svg" alt="Bright Leaf Coffee" class="logo" />
  <nav class="main-nav">
    <a href="/">Home</a>
    <a href="/subscriptions">Subscriptions</a>
    <a href="/about">About</a>
    <a href="/contact">Contact</a>
  </nav>
</header>
css
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem var(--space-lg);
}

.main-nav {
  display: flex;
  gap: var(--space-lg);
}

display: flex: turning on the real layout system

Setting display: flex on .site-header makes it a real flex container — every direct child (the logo image and the <nav>) becomes a real flex item, arranged in a row by default, left to right. This single declaration is what replaces what used to require floats and manual clearing in older CSS — a real, purpose-built system instead of a layout mechanism borrowed from something it wasn't originally designed for.

The real main axis: justify-content

css
.site-header {
  justify-content: space-between; /* logo and nav pushed to opposite ends */
}
text
flex-start      → items packed at the start (the default)
center           → items packed together in the center
space-between    → real, even space distributed BETWEEN items only
space-around     → real, even space around each item, including the ends

justify-content controls alignment along the real main axis — horizontal, for the default row direction used here. space-between is exactly right for this header: the logo stays pinned to the left edge, the nav to the right, with the real, available space between them growing or shrinking as the viewport width changes, with zero manual positioning math required.

The real cross axis: align-items

css
.site-header {
  align-items: center; /* vertically centers the logo and nav */
}

align-items controls alignment along the real cross axis — perpendicular to the main axis, so vertical for this row-direction header. center here is what keeps the logo image and the nav links vertically centered relative to each other, even if they have slightly different real heights — a real, common alignment need that used to require genuine hacks (like manually calculated padding or vertical-align tricks) before Flexbox existed.

gap: real, even spacing without margin math

css
.main-nav {
  display: flex;
  gap: var(--space-lg);
}

gap adds real, even spacing between flex items only — never before the first or after the last, unlike using margin on each item, which requires either a real :last-child exception or accepts uneven trailing space. This is a genuinely simpler, more reliable way to space out the nav links than the older real pattern of margin-right on every link except the last.

Why it matters

Flexbox items shrink and reflow automatically as the container's real width changes — nothing about the header above needs a media query to remain usable as the viewport narrows, since justify-content and gap keep recalculating the real spacing continuously. This is a genuine, structural improvement over older layout techniques, which generally required explicit, manual recalculation at every real breakpoint.

flex-direction: real column layouts, same system

css
.mobile-nav-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

flex-direction: column swaps the real main axis to vertical — genuinely useful for Bright Leaf Coffee's mobile navigation panel, which needs its links stacked vertically rather than in the horizontal row used for the desktop header above. Every other Flexbox property covered in this part works identically, just rotated onto the new real axis.

Next: Flexbox deep dive — flex-wrap, flex-grow, and flex-shrink, building Bright Leaf Coffee's real, wrapping subscription cards grid.

VK

Vijay Kumar

Founder of TechPurAI — writing hands-on tutorials and honest tool breakdowns.

LinkedIn ↗
← previous8. Display: block, inline, inline-block, and nonenext →10. Flexbox deep dive: flex-wrap, grow, and shrink for the real subscription cards