CSS Flexbox: A Visual Guide

Reviewed & 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:

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

Flexbox layout happens along two axes:

Main axis → left to right

Cross axis → top to bottom

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

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

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

4. Controlling Layout on the Cross Axis

Cross axis alignment uses: align-items

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:

Simple Card Grid Example:

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:

Behind the scenes, that's shorthand for:

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

How much items shrink when space is tight.

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

flex-basis

The initial size before grow/shrink.

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.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

If your cards overflow off the page, add:

❌ 3. Using margins instead of gap

Modern Flexbox supports gap just like CSS Grid:

Cleaner than manually juggling margin-right etc.

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