Complete Developer Guide

CSS Flexbox Complete Guide

Properties, Examples & Cheat Sheet (2025)

Table of Contents

  1. Introduction to CSS Flexbox
  2. Container Properties
  3. Item Properties
  4. Common Layout Patterns
  5. Flexbox vs Grid
  6. Frequently Asked Questions
  7. Get More Tools

Introduction to CSS Flexbox

CSS Flexbox is a one-dimensional layout module that makes it incredibly easy to design flexible responsive layout structures with minimal code. It's one of the most important CSS features for modern web development.

What is Flexbox?

Flexbox (Flexible Box Module) provides an efficient way to arrange, distribute, and align elements in a container—even when their size is unknown or dynamic. Unlike traditional layout methods (floats, positioning), flexbox automatically handles spacing, alignment, and responsive behavior.

When Should You Use Flexbox?

Browser Support

Flexbox has excellent browser support. All modern browsers (Chrome, Firefox, Safari, Edge) support flexbox fully. No IE 11 support without prefixes.

/* Enable Flexbox on a container */ .flex-container { display: flex; }

Flexbox Container Properties

These properties are applied to the flex container (parent element). They control how flex items are laid out and distributed within the container.

1. display: flex

The foundation of flexbox. This property activates flexbox layout on a container and makes all direct children flex items.

.container { display: flex; /* Items will now be flex items */ }
Visual Demo — display: flex
Item 1
Item 2
Item 3
✓ All modern browsers support display: flex (Chrome 21+, Firefox 18+, Safari 7+, Edge 12+)

2. flex-direction

Defines the direction in which flex items are placed in the flex container. Controls the main axis orientation.

Values:

  • row (default) — Left to right
  • row-reverse — Right to left
  • column — Top to bottom
  • column-reverse — Bottom to top
.container { display: flex; flex-direction: column; }
Demo 1: flex-direction: row (default)
A
B
C
Demo 2: flex-direction: column
A
B
C
✓ Full support in all modern browsers (Chrome 21+, Firefox 18+, Safari 7+, Edge 12+)

3. flex-wrap

Determines whether flex items should wrap to a new line when they don't fit in the container.

Values:

  • nowrap (default) — All items stay on one line
  • wrap — Items wrap to the next line
  • wrap-reverse — Items wrap in reverse order
.container { display: flex; flex-wrap: wrap; gap: 10px; }
Demo: flex-wrap: wrap (resize browser to see wrapping)
Item 1
Item 2
Item 3
Item 4
✓ Full support (Chrome 21+, Firefox 28+, Safari 7+, Edge 12+)

4. justify-content

Aligns flex items along the main axis. It distributes space between and around items.

Values:

  • flex-start (default) — Items at the start
  • flex-end — Items at the end
  • center — Items centered
  • space-between — Space between items equally
  • space-around — Space around items equally
  • space-evenly — Space evenly distributed
.container { display: flex; justify-content: space-between; }
justify-content: center
Item
Item
justify-content: space-between
Item
Item
justify-content: space-around
A
B
C
✓ Full support (Chrome 21+, Firefox 18+, Safari 7+, Edge 12+)

5. align-items

Aligns flex items along the cross axis (perpendicular to the main axis).

Values:

  • stretch (default) — Items stretch to fill the container
  • flex-start — Items aligned to the start
  • flex-end — Items aligned to the end
  • center — Items centered
  • baseline — Items aligned to baseline
.container { display: flex; align-items: center; height: 200px; }
align-items: center
A
B
align-items: stretch
A
B
✓ Full support (Chrome 21+, Firefox 20+, Safari 7+, Edge 12+)

6. align-content

Aligns flex lines when there's extra space in the cross axis (only works with flex-wrap: wrap).

Values:

  • stretch (default) — Lines stretch to fill the container
  • flex-start — Lines aligned to start
  • center — Lines centered
  • space-between — Space between lines
  • space-around — Space around lines
.container { display: flex; flex-wrap: wrap; align-content: center; height: 300px; }
✓ Full support (Chrome 21+, Firefox 28+, Safari 7+, Edge 12+)

7. gap (row-gap, column-gap)

Defines the space between flex items. Modern alternative to margin-based spacing.

.container { display: flex; gap: 20px; /* Or: row-gap: 20px; column-gap: 10px; */ }
Demo: gap: 20px
Item 1
Item 2
Item 3
✓ Modern support (Chrome 84+, Firefox 63+, Safari 14.1+, Edge 84+). Use margin as fallback.

Flexbox Item Properties

These properties are applied to flex items (children of flex containers). They control how individual items grow, shrink, and align.

1. flex-grow, flex-shrink & flex-basis

These properties control how flex items expand and contract to fill available space.

flex-grow

Defines how much a flex item will grow relative to other items. Default: 0.

.item { flex-grow: 1; /* Item will grow to fill available space */ }
Demo: flex-grow (Item 2 has flex-grow: 1)
Item 1
Item 2
Item 3

flex-shrink

Defines how much a flex item will shrink relative to other items. Default: 1.

.item { flex-shrink: 0; /* Item won't shrink below flex-basis */ }

flex-basis

The default size of a flex item before flex-grow/shrink is applied. Default: auto.

.item { flex-basis: 200px; /* Item starts at 200px width */ }
✓ Full support (Chrome 21+, Firefox 18+, Safari 7+, Edge 12+)

2. flex (Shorthand)

Combines flex-grow, flex-shrink, and flex-basis into one property. Recommended approach.

/* flex: grow shrink basis */ .item { flex: 1 1 200px; }

Common flex shorthand values:

  • flex: 1 — Equivalent to flex: 1 1 0
  • flex: 0 0 auto — Item doesn't grow or shrink
  • flex: 1 0 200px — Grows, doesn't shrink, 200px basis
✓ Full support (Chrome 22+, Firefox 18+, Safari 7+, Edge 12+)

3. order

Changes the visual order of flex items without affecting HTML order. Default: 0.

.item-1 { order: 2; } .item-2 { order: 1; } .item-3 { order: 3; } /* Displays in order: Item 2, Item 1, Item 3 */
Demo: Visual order changed (HTML: 1,2,3 → Display: 2,1,3)
1
2
3
✓ Full support (Chrome 21+, Firefox 18+, Safari 7+, Edge 12+)

4. align-self

Allows individual flex items to override the container's align-items property.

Values (same as align-items):

  • auto (default) — Inherits from align-items
  • flex-start, flex-end, center, stretch, baseline
.container { display: flex; align-items: flex-start; height: 200px; } .item-special { align-self: center; /* This item centers despite align-items: flex-start */ }
Demo: Item 2 has align-self: center
Item 1
Item 2
Item 3
✓ Full support (Chrome 21+, Firefox 20+, Safari 7+, Edge 12+)

Common Flexbox Layout Patterns

Pattern 1: Perfect Centering (The "Holy Grail")

Center content both horizontally and vertically—one of the most common use cases.

.center-container { display: flex; justify-content: center; align-items: center; height: 200px; }
Interactive Demo: Perfect Centering
✓ Centered Content

Pattern 2: Navigation Bar

Create a horizontal navigation bar with logo on left and menu items on right.

/* HTML structure */ /* CSS */ nav { display: flex; justify-content: space-between; align-items: center; padding: 15px 30px; }
Navigation Bar Demo
Logo
Home
About
Contact

Pattern 3: Card Grid

Responsive card layout that wraps on smaller screens.

.card-grid { display: flex; flex-wrap: wrap; gap: 20px; } .card { flex: 1 1 300px; background: #1a1a1a; padding: 20px; border-radius: 8px; }
Card Grid Demo
Card 1
Card 2
Card 3

Pattern 4: Sidebar Layout

Main content area with a fixed-width sidebar.

.layout { display: flex; gap: 20px; } .sidebar { flex: 0 0 250px; /* Fixed width, no grow/shrink */ } .main { flex: 1; /* Takes remaining space */ }
Sidebar Layout Demo
Sidebar
Main Content

Pattern 5: Sticky Footer

Footer always sticks to the bottom of the page, even with minimal content.

body { display: flex; flex-direction: column; min-height: 100vh; } main { flex: 1; /* Grows to fill available space */ } footer { /* Stays at bottom */ }

Pattern 6: Equal Height Columns

Make all columns the same height regardless of content.

.columns { display: flex; gap: 20px; } .column { flex: 1 1 0; /* flex-basis: 0 ensures equal width distribution */ }
Equal Height Columns
Short
Medium length content
Longer content here

Flexbox vs CSS Grid: When to Use Each

While both are powerful layout tools, they solve different problems. Flexbox is one-dimensional (row or column), while Grid is two-dimensional (rows and columns).

Feature Flexbox CSS Grid
Dimensions 1D (row or column) 2D (rows and columns)
Best for Linear layouts, navigation, alignment Complex page layouts, dashboards
Content-first or layout-first? Content-first (items drive layout) Layout-first (grid defines structure)
Wrapping Yes (flex-wrap) Rows automatically created
Browser support Excellent (all modern browsers) Excellent (all modern browsers)
Learning curve Easier for beginners More complex, more powerful

Use Flexbox When:

Use CSS Grid When:

Pro tip: Often, the best solution combines Flexbox for content alignment with Grid for page structure. Use Grid for the main layout and Flexbox for component-level layouts.

Need a CSS Grid Guide?

Check out our comprehensive CSS Grid guide to master two-dimensional layouts.

CSS Grid Guide →

Frequently Asked Questions

Q: How do I center content horizontally and vertically with Flexbox?

Use both justify-content: center and align-items: center on the container, along with a fixed height:

.container { display: flex; justify-content: center; align-items: center; height: 200px; }

This centers any content within that container perfectly.

Q: What's the difference between justify-content and align-items?

justify-content aligns items along the main axis (horizontal in row direction).

align-items aligns items along the cross axis (vertical in row direction).

Think of it this way: if your flex-direction is row, justify-content handles left-right spacing, and align-items handles top-bottom spacing.

Q: How do I make equal-width columns in Flexbox?

Set flex: 1 on each column. This makes them grow equally to fill available space:

.columns { display: flex; gap: 20px; } .column { flex: 1; /* Equal width columns */ }

Q: How do I prevent items from shrinking below a minimum size?

Use flex-shrink: 0 or set a min-width:

.item { flex-shrink: 0; /* Won't shrink */ width: 200px; } /* Or use min-width */ .item { flex: 1; min-width: 150px; /* Won't shrink below 150px */ }

Q: Why aren't my flex items wrapping?

You need to add flex-wrap: wrap to the container. By default, Flexbox is flex-wrap: nowrap, which forces all items onto one line:

.container { display: flex; flex-wrap: wrap; gap: 20px; }

Without flex-wrap, items will shrink to fit rather than wrap to a new line.

Ready to Master Flexbox?

Use our interactive Flexbox generator to experiment with different properties and see real-time results.

Flexbox Generator → CSS Grid Generator →

Explore interactive demos and build layouts with real-time visualization.

Flexbox Cheat Sheet

Container Properties Quick Reference

/* Container */ .flex-container { display: flex; flex-direction: row; /* row, column, row-reverse, column-reverse */ flex-wrap: wrap; /* nowrap, wrap, wrap-reverse */ justify-content: center; /* start, end, center, space-between, space-around, space-evenly */ align-items: center; /* stretch, start, end, center, baseline */ align-content: center; /* Used with flex-wrap: wrap */ gap: 20px; /* Space between items */ }

Item Properties Quick Reference

/* Item */ .flex-item { flex: 1 1 200px; /* Shorthand: grow shrink basis */ /* Or individually: */ flex-grow: 1; /* How much to grow (0-∞) */ flex-shrink: 1; /* How much to shrink (0-∞) */ flex-basis: 200px; /* Base size before grow/shrink */ order: 1; /* Change visual order */ align-self: center; /* Override container align-items */ }

Common Patterns

/* Centering */ .center { display: flex; justify-content: center; align-items: center; } /* Space Between */ .space-between { display: flex; justify-content: space-between; } /* Column Layout */ .column { display: flex; flex-direction: column; } /* Equal Width Items */ .equal { display: flex; } .equal > * { flex: 1; } /* Wrap with Gap */ .wrap { display: flex; flex-wrap: wrap; gap: 20px; } /* Sidebar */ .sidebar-layout { display: flex; gap: 20px; } .sidebar { flex: 0 0 250px; } .main { flex: 1; }

Conclusion

CSS Flexbox is an indispensable tool for modern web development. With these 7 container properties and 4 item properties, you can create virtually any flexible layout. The key to mastering Flexbox is understanding the main and cross axes, and how justify-content and align-items work along those axes.

Remember: Flexbox is perfect for one-dimensional layouts. For complex two-dimensional layouts, consider CSS Grid. Most modern applications use both—Grid for page structure and Flexbox for component-level layouts.

Now that you understand Flexbox comprehensively, experiment with the interactive demos above and start building responsive layouts. Happy coding!

\xF0\x9F\x92\x99 Tip\xF0\x9F\x93\x9A Get Bundle \x244.99