CSS justify-content Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Flexbox & Grid

What You’ll Learn

The justify-content property aligns flex items or grid tracks along the main axis and controls how extra space is distributed between them. It is one of the most useful flexbox tools for navbars, button rows, and responsive layouts.

01

Main Axis

Horizontal in row flex.

02

Syntax

Keyword values.

03

Flexbox

Align items in a row.

04

CSS Grid

Position grid tracks.

05

Spacing

between, around, evenly.

06

Compare

vs align-items.

Introduction

The justify-content property in CSS is used in flexbox and grid layouts to align items along the main axis of a container and distribute free space between them.

In a default horizontal flex row, the main axis runs left to right, so justify-content controls horizontal alignment. It is especially useful for navbars, toolbars, card actions, and any layout where you need items spaced predictably.

Definition and Usage

Apply justify-content to a flex or grid container, not to individual items. Use it when you want items grouped at the start, centered, pushed to the ends, or spaced evenly across the available width.

💡
Beginner Tip

Pair justify-content with align-items for full control: justify-content handles the main axis, align-items handles the cross axis.

📝 Syntax

Apply justify-content to a flex or grid container:

syntax.css
.container {
  justify-content: value;
}

Basic Flexbox Example

justify-content-basic.css
.toolbar {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
}
justify-content: flex-start; justify-content: center; justify-content: space-between; justify-content: space-evenly;

Default Value

The default value of justify-content in flexbox is flex-start, which aligns items at the beginning of the main axis.

Syntax Rules

  • Apply to a flex or grid container, not to individual items.
  • Works along the main axis (inline axis in grid).
  • Spacing values distribute free space between items or tracks.
  • The property is not inherited.
  • In a column flex direction, the main axis becomes vertical.

⚡ Quick Reference

QuestionAnswer
Initial value (flexbox)flex-start
Applies toFlex and grid containers
InheritedNo
AnimatableNo
Default row flex main axisHorizontal (left to right in LTR)

💎 Property Values

ValueDescription
flex-startPacks items at the start of the main axis.
flex-endPacks items at the end of the main axis.
centerCenters items along the main axis.
space-betweenFirst item at the start, last item at the end, equal space between items.
space-aroundEqual space around each item, including half-size space at the edges.
space-evenlyEqual space between items and at both outer edges.

justify-content vs align-items

PropertyAxisBest for
justify-contentMain axisHorizontal spacing in a row flex container, or track placement in grid
align-itemsCross axisVertical alignment of items inside one flex line

👀 Live Preview

A flex row with three items using justify-content: center:

1
2
3

Examples Gallery

Center a toolbar, build a navbar with space-between, space icons evenly, and center grid tracks.

📚 Flexbox Layouts

Use justify-content to align items along the main axis in row or column flex containers.

Example 1 — Center Items with center

Center action buttons in a toolbar along the main axis.

justify-content-center.css
.toolbar {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
}
Try It Yourself

How It Works

center groups items in the middle of the container along the main axis, with equal free space on both sides.

Example 2 — Navbar with space-between

Push a logo to one side and navigation links to the other.

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

How It Works

space-between places the first and last flex items at opposite edges with equal gaps between remaining items.

Example 3 — Even Spacing with space-evenly

Distribute icon buttons with equal space between and around them.

justify-content-evenly.css
.icon-row {
  display: flex;
  justify-content: space-evenly;
}
Try It Yourself

How It Works

Unlike space-between, space-evenly leaves the same amount of space before the first item and after the last item.

🚀 CSS Grid Layouts

In grid containers, justify-content distributes tracks along the inline axis when the grid is narrower than its container.

Example 4 — Center Grid Tracks

Center a fixed-width grid inside a wider container.

justify-content-grid.css
.grid {
  display: grid;
  grid-template-columns: repeat(3, 80px);
  justify-content: center;
  gap: 10px;
}
Try It Yourself

How It Works

The grid tracks stay grouped in the center instead of stretching to fill the full container width.

♿ Accessibility

  • Preserve logical reading order — visual reordering with flex does not change DOM order for screen readers.
  • Keep touch targets spacedspace-between nav links still need adequate tap area on mobile.
  • Test keyboard focus — centered or spaced buttons must remain reachable in a sensible tab order.
  • Avoid hiding content off-screen — extreme spacing on small screens can push controls out of view.
  • Use semantic HTML — wrap navigation groups in <nav> with clear labels.

🧠 How justify-content Works

1

Container becomes flex or grid

Items or tracks are laid out along the main axis of the formatting context.

display: flex
2

Free space is calculated

The browser subtracts item sizes from the container size along the main axis.

Layout
3

justify-content distributes space

Keyword values pack, center, or spread items across the remaining main-axis space.

Alignment
=

Balanced main-axis layouts

Toolbars, navbars, and grids align predictably without manual margins on every item.

🖥 Browser Compatibility

justify-content is widely supported in all modern browsers for both flexbox and CSS Grid layouts.

Baseline · Flexbox & Grid

Reliable alignment across browsers

From current Chrome and Safari builds to Firefox and Edge, justify-content is a stable layout tool.

98% Modern browser support
Google Chrome 29+ flex · 57+ grid
Full support
Mozilla Firefox 28+ flex · 52+ grid
Full support
Apple Safari 9+ flex · 10.1+ grid
Full support
Microsoft Edge 12+ flex · 16+ grid
Full support
Opera Modern versions
Full support
justify-content property 98% supported

Bottom line: Use justify-content confidently in modern flexbox and grid layouts.

🎉 Conclusion

The justify-content property is a versatile tool for aligning and distributing space among items in a flex container or tracks in a grid container. Whether you need centered buttons, a spaced navbar, or evenly distributed icons, it offers clear keyword values for the job.

For beginners, start with center and space-between, then experiment with space-evenly and grid track alignment to see how main-axis spacing changes your layouts.

💡 Best Practices

✅ Do

  • Use space-between for navbars and split headers
  • Pair with align-items for cross-axis control
  • Use gap alongside spacing values when you need fixed item separation
  • Test layouts on small screens where wrapping may occur
  • Apply to the container, not individual flex items

❌ Don’t

  • Confuse justify-content with align-items
  • Expect spacing values to work without extra main-axis free space
  • Apply manual margins on every item when one container rule is enough
  • Forget that flex-direction: column rotates the main axis
  • Skip testing keyboard and touch interaction after spacing changes

Key Takeaways

Knowledge Unlocked

Five things to remember about justify-content

Use these points when building flex and grid layouts.

5
Core concepts
start 02

Default

flex-start.

Values
between 03

Spacing

Split navbars.

Pattern
grid 04

Flex + Grid

Both supported.

Scope
align 05

Not align-items

Different axis.

Compare

❓ Frequently Asked Questions

justify-content aligns flex items or grid tracks along the main axis of a container and distributes free space between them.
In flexbox the initial used value is flex-start, which packs items at the start of the main axis.
justify-content controls main-axis alignment. align-items controls cross-axis alignment of individual items inside a line or track.
Yes. In grid layouts, justify-content distributes grid tracks along the inline axis when the grid is narrower than its container.
Use space-between when the first and last items should touch the edges. Use space-evenly when you want equal space on the outside and between items.

Practice in the Live Editor

Open the HTML editor, set display: flex, and try different justify-content values on a toolbar or navbar.

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