CSS Flexbox

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 4 Examples
Layout

What You’ll Learn

CSS Flexbox arranges items in a row or column with powerful alignment controls. It is the go-to layout tool for navbars, centered content, card rows, and responsive one-dimensional designs.

01

Container

display: flex.

02

Items

Direct children.

03

Main axis

justify-content.

04

Cross axis

align-items.

05

Direction

row / column.

06

Patterns

Nav & center.

Introduction

The CSS Flexible Box Layout Module — commonly called Flexbox — is a layout model for arranging elements in one dimension: a row or a column. It distributes space along a main axis and aligns content along a cross axis.

What Is Flexbox?

Flexbox simplifies aligning and spacing elements inside a container, even when item sizes are unknown or dynamic. Before Flexbox, centering content vertically or building responsive navbars required hacks with floats and positioning.

Today, Flexbox is a core skill for every web developer. It works alongside CSS Grid: Flexbox for one-dimensional flows, Grid for two-dimensional layouts.

💡
Beginner Tip

Add display: flex to a parent, then use gap for spacing between children. Start with justify-content: center and align-items: center to center content perfectly.

Basic Concepts

  • Flex container — The parent with display: flex or display: inline-flex.
  • Flex items — The direct children inside the flex container.
  • Main axis — The direction items flow (row = horizontal, column = vertical).
  • Cross axis — Perpendicular to the main axis.

📝 Syntax

Enable Flexbox on a container and align its children:

flex-basic.css
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
}

Flex Container Properties

PropertyPurpose
display: flexCreates a flex container (block-level)
flex-directionrow, row-reverse, column, column-reverse
justify-contentAlign along main axis: flex-start, center, space-between, etc.
align-itemsAlign along cross axis: flex-start, center, stretch, etc.
flex-wrapnowrap, wrap, wrap-reverse — allow items onto multiple lines
gapSpace between flex items (modern, preferred over margins)

Flex Item Properties

PropertyPurpose
flex-growHow much an item grows to fill space (default 0)
flex-shrinkHow much an item shrinks when space is tight (default 1)
flex-basisInitial size before growing/shrinking (default auto)
flexShorthand: flex-grow flex-shrink flex-basis
align-selfOverride align-items for one item
orderChange visual order without changing HTML
display: flex justify-content: center align-items: center gap: 1rem

⚡ Quick Reference

TaskCSS
Enable flexboxdisplay: flex;
Center horizontally (row)justify-content: center;
Center vertically (row)align-items: center;
Stack verticallyflex-direction: column;
Space items apartjustify-content: space-between;
Wrap to new lineflex-wrap: wrap;
Gap between itemsgap: 1rem;

When to Use Flexbox

  • Navigation bars — Align logo and links with space-between.
  • Centering content — Perfect horizontal and vertical centering in one container.
  • Card rows — Equal-height cards in a row with align-items: stretch.
  • Form layouts — Label and input pairs in rows or columns.
  • Sticky footers — Push footer to bottom with flex-grow on main content.

Use CSS Grid when you need both rows and columns at once. Flexbox and Grid work great together on the same page.

👀 Live Preview

See Flexbox alignment in action:

Row with gap
1 2 3
justify-content: center; align-items: center
Centered
justify-content: space-between (navbar)
Logo Home   About

Examples Gallery

Practice Flexbox with basic rows, centered content, column stacks, and navigation bars.

🔠 Core Patterns

Start with a flex row, then learn centering.

Example 1 — Basic Flex Row

Apply display: flex and gap to arrange items in a horizontal row.

flex-row.css
.container {
  display: flex;
  gap: 0.75rem;
  padding: 1rem;
  background: #f1f5f9;
}

.item {
  padding: 0.75rem 1rem;
  background: #2563eb;
  color: #fff;
}
Try It Yourself

How It Works

display: flex turns the container into a flexbox. Children line up along the main axis (row by default). gap adds space between them.

Example 2 — Center Content

Combine justify-content and align-items to center content in both directions.

flex-center.css
.center-box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 160px;
  background: #eff6ff;
}
Try It Yourself

How It Works

justify-content: center centers along the main axis. align-items: center centers along the cross axis. Together they center content perfectly.

🎨 Real Layouts

Build vertical stacks and navigation bars with Flexbox.

Example 3 — Column Direction

Stack items vertically with flex-direction: column.

flex-column.css
.stack {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
Try It Yourself

How It Works

flex-direction: column flips the main axis to vertical. justify-content now controls vertical spacing; align-items controls horizontal alignment.

Example 4 — Navigation Bar

Separate logo and links with justify-content: space-between.

flex-navbar.css
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  background: #1e293b;
}

.links {
  display: flex;
  gap: 1rem;
}
Try It Yourself

How It Works

space-between pushes the first and last groups to opposite edges. Nested flex on .links keeps nav links evenly spaced.

💬 Usage Tips

  • Use gap — Prefer gap over margins between flex items.
  • Min-height for centering — Give the container a height so vertical centering is visible.
  • Nested flex — Nav link groups often need their own display: flex.
  • flex: 1 — Let one item grow to fill remaining space (great for sidebars).
  • DevTools — Chrome and Firefox show flex overlay lines to debug alignment.

⚠️ Common Pitfalls

  • Flex only direct children — Only immediate children become flex items.
  • Confusing axesjustify-content follows the main axis; it switches when you change flex-direction.
  • Forgetting container height — Vertical centering needs a defined height on the container.
  • Using Flexbox for full page grids — CSS Grid is better for two-dimensional layouts.
  • min-width: 0 — Flex items can overflow; add min-width: 0 on text children when needed.

♿ Accessibility

  • DOM order matters — Screen readers follow HTML order, not visual order property.
  • Focus order — Do not use order in ways that confuse keyboard navigation.
  • Touch targets — Flex nav links still need adequate padding for mobile taps.
  • Responsive wrapping — Use flex-wrap: wrap so items do not overflow on small screens.
  • Semantic HTML — Use <nav> for navigation even when styled with Flexbox.

🧠 How Flexbox Works

1

display: flex on parent

The container becomes a flex formatting context. Direct children become flex items.

Container
2

Main & cross axes set

flex-direction defines the main axis. The cross axis is perpendicular.

Axes
3

Items aligned & spaced

justify-content and align-items position items. gap adds space between them.

Align
=

Flexible layout

Items adapt to available space with clean alignment.

🖥 Browser Compatibility

CSS Flexbox has universal support in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers.

Baseline · Universal support

CSS Flexbox Module

Flexbox is production-ready in every current browser. No vendor prefixes needed.

100% Modern browsers
Google Chrome Full support
Full support
Mozilla Firefox Full support
Full support
Apple Safari Full support
Full support
Microsoft Edge Full support
Full support
Opera Full support
Full support
CSS Flexbox 100% supported

Bottom line: Flexbox is safe for production. Legacy IE issues are irrelevant for modern development.

🎉 Conclusion

CSS Flexbox is essential for modern web layout. With display: flex, justify-content, and align-items, you can build navbars, center content, and create responsive one-dimensional layouts with minimal code.

Practice the four examples above, then explore individual properties like flex-grow and flex-wrap as your layouts grow more complex. Combine Flexbox with CSS Grid for full-page designs.

💡 Best Practices

✅ Do

  • Use gap for spacing between flex items
  • Center with justify-content + align-items together
  • Use semantic <nav> for navigation bars
  • Add flex-wrap: wrap for responsive rows
  • Combine Flexbox with CSS Grid on complex pages

❌ Don’t

  • Expect flex to affect nested grandchildren
  • Rely on order for accessibility-critical sequence
  • Use Flexbox for full two-dimensional page grids
  • Forget container height when vertically centering
  • Use margins when gap is cleaner

Key Takeaways

Knowledge Unlocked

Five things to remember about Flexbox

Use these points when building flexible layouts.

5
Core concepts
jc 02

Main axis

justify-content.

Align
ai 03

Cross axis

align-items.

Align
dir 04

Direction

row / column.

Flow
1D 05

One axis

Not full grid.

Scope

❓ Frequently Asked Questions

Flexbox (Flexible Box Layout) is a CSS layout model for arranging items in a row or column. A flex container distributes space and aligns its children along a main axis and cross axis — ideal for navbars, centering, and responsive one-dimensional layouts.
Set display: flex or display: inline-flex on the parent element. That parent becomes a flex container, and its direct children become flex items.
justify-content aligns items along the main axis (horizontal in a row, vertical in a column). align-items aligns items along the cross axis (perpendicular to the main axis).
Use Flexbox for one-dimensional layouts — a row of nav links or a vertical stack. Use CSS Grid when you need rows and columns together — full page layouts or card grids.
Yes. Flexbox is supported in all modern browsers including Chrome, Firefox, Safari, Edge, and mobile browsers. It is safe for production use.

Practice in the Live Editor

Open the HTML editor, add display: flex to a container, and experiment with justify-content and align-items.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful