Tutorials CSS Flexbox Alignment & Justify
Ch 28 / 60
Chapter 28 Beginner 8 min read CSS

CSS Flexbox Alignment & Justify Properties

Flexbox has six alignment properties spread across containers and items. Previous chapters introduced them individually — this chapter brings them all together: the full taxonomy, the shorthands that combine them, and the complete cookbook of centring and alignment recipes you will use every day.

What you'll learn
The full alignment taxonomy: main vs cross axis
justify-content — all 6 values in depth
align-items — single-line cross-axis
align-self — per-item cross-axis override
align-content — multi-line cross-axis
Shorthands: place-items, place-self, place-content

The Alignment Taxonomy

CSS alignment properties always operate along one of two axes. The table below maps every Flexbox alignment property to its axis, target, and where it's applied. Understanding this map removes all confusion:

→ Main Axis (flex-direction)
justify-contentcontainer → all items
justify-self*n/a in Flexbox
justify-items*n/a in Flexbox
↕ Cross Axis (perpendicular)
align-itemscontainer → all items (per line)
align-selfitem → overrides align-items
align-contentcontainer → all lines (multi-line)

* justify-self and justify-items have no effect in Flexbox — use margin: auto to achieve individual main-axis positioning of flex items.

Property Applies to Axis Effect Default
justify-content Container Main Distributes all items + free space flex-start
align-items Container Cross Aligns items in a single line stretch
align-content Container Cross Aligns multiple lines (wrap only) normal
align-self Item Cross Overrides align-items for one item auto
place-items Container Both Shorthand: align-items + justify-items* normal
place-self Item Both Shorthand: align-self + justify-self* auto
place-content Container Both Shorthand: align-content + justify-content normal

justify-content — Main Axis Distribution

justify-content distributes flex items and free space along the main axis. It acts on the container. In flex-direction: row (default), this is horizontal distribution:

When flex-direction: column, justify-content distributes items vertically:

column + flex-start
A
B
column + center
A
B
column + space-between
A
B

Bonus: margin: auto — Since justify-self doesn't work in Flexbox, use margin: auto on an item to push remaining space to one side:

Logo
Login
Sign Up

Login has margin-left:auto — pushes it to the right, Login+SignUp stay together at end

CSS — justify-content + margin:auto trick
.container { display: flex; }

.jc-start   { justify-content: flex-start; }   /* default */
.jc-end     { justify-content: flex-end; }
.jc-center  { justify-content: center; }
.jc-between { justify-content: space-between; }
.jc-around  { justify-content: space-around; }
.jc-evenly  { justify-content: space-evenly; }

/* margin:auto as per-item justify-self replacement */
.nav         { display: flex; align-items: center; }
.nav .logo   { }
.nav .login  { margin-left: auto; } /* pushes to right edge */

/* Space-before one item in a column */
.footer-link { margin-top: auto; } /* pushes to bottom */

align-items — Cross Axis (Single Line)

align-items aligns all items along the cross axis within a single flex line. Container has items with different heights so the differences are visible. Default is stretch:

CSS — align-items
.container { display: flex; }

.ai-stretch   { align-items: stretch; }     /* default — fill height */
.ai-start     { align-items: flex-start; } /* top */
.ai-end       { align-items: flex-end; }   /* bottom */
.ai-center    { align-items: center; }     /* ← use this most often */
.ai-baseline  { align-items: baseline; }   /* text baseline align */

/* Perfect centring */
.center-both {
  display:         flex;
  justify-content: center;
  align-items:     center;
  min-height:      100vh;
}

align-self — Per-Item Cross Axis Override

align-self lets one individual item override the container's align-items. The container below has align-items: center. Each card shows one item with a different align-self value (green) while others remain centred (blue):

CSS — align-self real-world patterns
.container {
  display:     flex;
  align-items: flex-start;   /* default for all items */
}

/* One item overrides */
.special { align-self: center; }
.fill    { align-self: stretch; }

/* ── Pattern: Card with CTA at the bottom ── */
.card {
  display:        flex;
  flex-direction: column;
}
.card .cta {
  margin-top: auto;        /* pushes CTA to bottom */
  align-self: flex-start;  /* don't stretch button full width */
}

/* ── Pattern: Avatar stays at top in tall card row ── */
.card-row      { display: flex; align-items: stretch; }
.card .avatar  { align-self: flex-start; }  /* top-aligns avatar */

align-content — Multi-Line Cross Axis

align-content aligns multiple flex lines as a group within the container. It only takes effect when flex-wrap: wrap creates more than one line and the container has extra cross-axis space. Each demo has a fixed height with 4 wrapped items:

align-items vs align-content — final word
Use align-items for the vast majority of cases — it works in both single-line and multi-line containers and aligns items within each row. Only reach for align-content when you have flex-wrap: wrap, a taller container, and want to control how the rows as a group are distributed in that extra vertical space.

place-items, place-self & place-content

CSS provides three shorthand properties that combine alignment pairs. Their behaviour differs between Flexbox and Grid — in Flexbox the justify-* component has limited effect:

place-content
= align-content + justify-content
Works fully in Flexbox. The most useful shorthand. place-content: center centres items on both axes at once.
place-items
= align-items + justify-items
In Flexbox: only align-items part has effect. justify-items is ignored for flex items.
place-self
= align-self + justify-self
In Flexbox: only align-self part has effect. justify-self is ignored for flex items.

Live place-content demos on a flex container with a fixed height — rows on a 3-column grid:

place-content: center
A
B
place-content: flex-start
A
B
place-content: flex-end
A
B
CSS — place-content, place-items, place-self
/* ── place-content: <align-content> [<justify-content>] ── */
.center-all {
  display:       flex;
  place-content: center;  /* both axes */
  min-height:    100vh;
}

/* Different values for each axis */
.nav { place-content: center space-between; }
/*                 ↑align-content  ↑justify-content */

/* ── place-items: <align-items> [<justify-items>] ── */
.container { place-items: center; }
/* In Flexbox: only align-items:center takes effect */
/* In Grid: both align-items and justify-items apply */

/* ── place-self: <align-self> [<justify-self>] ── */
.item { place-self: center; }
/* In Flexbox: only align-self:center takes effect */

/* RECOMMENDATION: in Flexbox use explicit properties
   for clarity, save shorthands for Grid where they fully work */

Centring Cookbook — Recipes for Every Situation

These are the patterns you'll use constantly. All are pure Flexbox — no position tricks needed:

Full-screen centred hero
Centred content
display:flex;
justify-content:center;
align-items:center;
min-height:100vh;
Horizontally centred only
Centred H
display:flex;
justify-content:center;
Vertically centred only
Centred V
display:flex;
align-items:center;
min-height: Npx;
Push last item to end
A
B
End
.last { margin-left: auto; }
/* no justify-self needed */
Column centred vertically
Title
Subtitle
flex-direction:column;
justify-content:center;
align-items:center;
Equal height cards
Short card
Tall card with more content
display:flex;
align-items:stretch; /* default */
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all Flexbox alignment and justify properties with visual examples.
Download PDF

Try It Yourself

All alignment properties in one editor. Try changing values and see how items respond. Experiment with margin: auto, align-self overrides and place-content:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 28

Questions & Answers
justify-self (and justify-items) have no effect on flex items by specification. In Flexbox, the main axis distribution is controlled at the container level by justify-content — it distributes all items together. There is no per-item main-axis positioning in the same way there is for the cross axis (align-self).

The replacement: margin: auto
When you want to push one specific flex item or a group of items to a different position along the main axis, set margin-left: auto or margin-right: auto. auto margins in Flexbox absorb all available free space on that side.

Examples:
— Logo left, login+signup right: .login { margin-left: auto; }
— Items left, one item pinned right: .pinned { margin-left: auto; }
— CTA pushed to bottom of a column card: .cta { margin-top: auto; }
Two approaches — classic and compact:

Classic (3 properties, most explicit):
display: flex;
justify-content: center; (main axis)
align-items: center; (cross axis)

Compact (2 properties, using shorthand):
display: flex;
place-content: center; (both axes)

Both require the container to have a defined height — otherwise there's no vertical space to centre within. Use min-height: 100vh for full-screen, height: Npx for fixed containers, or min-height: Npx for flexible containers.

The classic form is preferred in teams for readability. The compact form is useful in prototyping or when writing concise utility classes.
In flex-direction: column, the main axis is vertical (top to bottom). So justify-content operates vertically:

justify-content: center → centres items vertically in the container
justify-content: space-between → distributes vertical space between rows

And the cross axis is horizontal, so:
align-items: center → centres items horizontally

This is the axis rotation principle: properties don't change meaning, but the physical directions they control rotate with flex-direction. Always think in terms of "main axis" and "cross axis" rather than "horizontal" and "vertical".
Use flex-direction: column on the card and margin-top: auto on the CTA button:

.card { display: flex; flex-direction: column; }
.cta { margin-top: auto; }

margin-top: auto absorbs all available free space above the CTA, pushing it to the bottom regardless of how much content is above it. This is why cards in a grid with different content lengths all have their CTAs aligned at the bottom.

Often combined with:
align-self: flex-start on the CTA — so the button doesn't stretch to full card width when align-items: stretch is active.

This is the cleanest solution — no absolute positioning, no JavaScript height measurement needed.
place-content combines align-content + justify-content. Both components work in Flexbox. This is the most useful shorthand in Flexbox for full centring: place-content: center.

place-items combines align-items + justify-items. In Flexbox, only the align-items part works — justify-items has no effect on flex items. So place-items: center in Flexbox only centres items on the cross axis.

place-self combines align-self + justify-self. In Flexbox, only align-self works.

Recommendation: in Flexbox use place-content (it works fully). For Grid, all three shorthands work completely as expected.
Multiple Choice Questions
Question 1 of 5
You want to push the last nav item to the right edge without changing the others. What is the cleanest approach in Flexbox?
Question 2 of 5
What does align-items: baseline align?
Question 3 of 5
The container has flex-direction: column and align-items: center. In which direction are items centred?
Question 4 of 5
In Flexbox, which alignment shorthand works fully on both axes?
Question 5 of 5
A flex card uses flex-direction: column. You want the CTA button at the card bottom regardless of content height. What CSS should the CTA have?
Chapter Summary
  • Taxonomy: Main axis → justify-content. Cross axis → align-items, align-self, align-content. justify-self / justify-items have no effect in Flexbox.
  • justify-content — main axis: flex-start, flex-end, center, space-between, space-around, space-evenly. Rotates with flex-direction.
  • margin: auto — the per-item main axis alignment technique in Flexbox. margin-left: auto pushes an item to the right. margin-top: auto pushes down in column containers.
  • align-items — cross axis for all items in a line: stretch (default), flex-start, flex-end, center, baseline.
  • align-self — overrides align-items for one item. Same values plus auto.
  • align-content — cross axis for multiple lines (requires flex-wrap: wrap + extra height). Use only when you need line-group control.
  • Shorthands: place-content works fully in Flexbox (align-content + justify-content). place-items and place-self only affect the cross axis in Flexbox.
  • Perfect centring recipe: display:flex; justify-content:center; align-items:center; min-height:100vh — or compact: display:flex; place-content:center.
  • CTA to bottom: flex-direction: column on card + margin-top: auto on CTA.
~8 min read
Beginner level
Chapter 28 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel