CSS Flexbox: A Visual Guide

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.

1. The Flexbox Mental Model: Container + Items

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

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

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

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

4. Controlling Layout on the Cross Axis

5. Making Items Wrap (Responsive Rows)

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

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

6. The flex Shorthand: Grow, Shrink, Basis

flex-grow

How much an item can grow relative to others.

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

flex-shrink

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

flex-basis

8. Common Flexbox Layout Patterns

8.1 Centering Anything (The "Cheat Code")

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

8.2 Navbar with Logo and Menu

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

8.3 Sidebar + Main Content

8.4 Equal-Height Cards

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

Modern Flexbox supports gap just like CSS Grid:

Cleaner than manually juggling margin-right etc.

10. Flexbox Cheat Sheet (Quick Reference)

11. How to Practice Flexbox (Easy Drills)

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

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

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