CSS Flexbox: A Visual Guide

Published by Brayan K

Master the flex container, axes, alignment properties, and common layout patterns with clear visual examples.

Master the flex container, axes, alignment properties, and common layout patterns with visual examples

If you've ever tried to center something in CSS and nearly cried, Flexbox is your new best friend.

Flexbox (Flexible Box Layout) is a layout system designed to help you build responsive, modern UIs without fighting floats, magic margins, or weird hacks.

This guide will walk you visually through:

1. The Flexbox Mental Model: Container + Items

Everything in Flexbox starts with two roles:

<div class="container">
  <div class="item">One</div>
  <div class="item">Two</div>
  <div class="item">Three</div>
</div>
.container {
  display: flex; /* turns this into a flex container */
}

2. Axes: Main Axis vs Cross Axis (Super Important)

Flexbox layout happens along two axes:

.container {
  display: flex;        /* default: flex-direction: row */
}

Main axis → left to right

Cross axis → top to bottom

.container {
  display: flex;
  flex-direction: column;
}

Main axis → top to bottom

Cross axis → left to right

Many Flexbox properties literally mean "Align along the main axis" or "Align along the cross axis". Once you understand that, the rest feels logical.

3. Controlling Layout on the Main Axis

Main axis alignment is controlled with: justify-content

.container {
  display: flex;
  justify-content: flex-start;   /* default */
  /* flex-start | flex-end | center | space-between | space-around | space-evenly */
}

All items packed to the left

All items packed to the right

First item at start, last at end, equal space between

Equal space around each item

Equal space between all items and edges

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Logo on left, menu on right, perfectly centered vertically.

4. Controlling Layout on the Cross Axis

Cross axis alignment uses: align-items

.container {
  display: flex;
  align-items: stretch;   /* default */
  /* stretch | flex-start | flex-end | center | baseline */
}

Items align at the top of the container

Stick to the bottom

Each item stretches to fill container height

5. Making Items Wrap (Responsive Rows)

By default, Flexbox tries to keep all items on one line. To allow wrapping:

.container {
  display: flex;
  flex-wrap: wrap;        /* allow items to go to the next line */
  gap: 1rem;              /* modern spacing between items */
}

Simple Card Grid Example:

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.card {
  flex: 1 1 250px; /* grow, shrink, base width */
  background: #f7f7f7;
  padding: 16px;
  border-radius: 8px;
}

Each card tries to be 250px wide, but can grow or shrink, and wraps when needed.

6. The flex Shorthand: Grow, Shrink, Basis

On the item level, you often see:

.item {
  flex: 1;
}

Behind the scenes, that's shorthand for:

.item {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 0;
}

flex-grow

How much an item can grow relative to others.

.item-1 { flex-grow: 1; }
.item-2 { flex-grow: 2; }
.item-3 { flex-grow: 1; }

Total grow = 1 + 2 + 1 = 4. .item-2 gets 2x the extra space

flex-shrink

How much items shrink when space is tight.

.item { flex-shrink: 0; } /* don't shrink */

Use it when you don't want a button or logo to shrink too small.

flex-basis

The initial size before grow/shrink.

.item {
  flex: 0 0 200px;   /* fixed-width cards that can wrap */
}

8. Common Flexbox Layout Patterns

8.1 Centering Anything (The "Cheat Code")

.center {
  display: flex;
  justify-content: center; /* center horizontally */
  align-items: center;     /* center vertically */
}
<div class="center" style="height: 100vh;">
  <div>Perfectly Centered!</div>
</div>

This may be the most useful Flexbox snippet you'll ever learn.

8.2 Navbar with Logo and Menu

<nav class="navbar">
  <div class="logo">MySite</div>
  <ul class="menu">
    <li>Home</li>
    <li>Docs</li>
    <li>Pricing</li>
  </ul>
</nav>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: #111827;
  color: #f9fafb;
}

.menu {
  display: flex;
  gap: 16px;
  list-style: none;
}

Logo on left, menu items on right, centered vertically.

8.3 Sidebar + Main Content

<div class="layout">
  <aside class="sidebar">Sidebar</aside>
  <main class="content">Main Content</main>
</div>
.layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  flex: 0 0 250px;      /* fixed 250px width */
  background: #1f2933;
  color: #fff;
}

.content {
  flex: 1;              /* take remaining space */
  padding: 24px;
}

8.4 Equal-Height Cards

.cards {
  display: flex;
  gap: 16px;
}

.card {
  flex: 1;
  background: #f3f4f6;
  padding: 16px;
}

Flexbox automatically makes all cards the same height (stretch on cross axis).

9. Common Flexbox Pitfalls (And Fixes)

❌ 1. Expecting align-items to work vertically with flex-direction: column

For vertical spacing in a column, use justify-content, not align-items.

❌ 2. Forgetting flex-wrap for responsive grids

If your cards overflow off the page, add:

.container {
  display: flex;
  flex-wrap: wrap;
}

❌ 3. Using margins instead of gap

Modern Flexbox supports gap just like CSS Grid:

.container {
  display: flex;
  gap: 12px;
}

Cleaner than manually juggling margin-right etc.

10. Flexbox Cheat Sheet (Quick Reference)

.container {
  display: flex;           /* activate flexbox */
  flex-direction: row;     /* row | row-reverse | column | column-reverse */
  flex-wrap: nowrap;       /* nowrap | wrap | wrap-reverse */
  justify-content: center; /* main axis: start | end | center | space-between | space-around | space-evenly */
  align-items: center;     /* cross axis: stretch | start | end | center | baseline */
  align-content: center;   /* multi-row cross-axis alignment */
  gap: 1rem;               /* spacing between items */
}
.item {
  flex: 1;                 /* flex-grow flex-shrink flex-basis */
  align-self: center;      /* override align-items for single item */
  order: 1;                /* change visual order */
}

11. How to Practice Flexbox (Easy Drills)

1. Rebuild a Navbar

Look at any modern site and try to recreate the header with Flexbox.

2. Build a Pricing Section

3 pricing cards side-by-side, equal height, responsive down to mobile.

3. Create a Simple Dashboard Layout

Sidebar + main content using flex: 0 0 250px and flex: 1.

Practice centering modals, loaders, and buttons using Flexbox, not position hacks.

The more real UI you try to copy, the faster Flexbox will feel natural.

Summary

Flexbox is the modern way to build layouts without fighting CSS.

Master the main axis vs cross axis mental model, and the rest becomes intuitive:

Practice by rebuilding real UI — navbars, cards, dashboards — and you'll become fluent fast.

Related articles

Links on this page