C.W.K.
Stream
Lesson 02 of 04 · published

Alignment & Justification: justify-content, align-items, align-self

~11 min · justify-content, align-items, align-self, alignment

Level 0Markup Novice
0 XP0/34 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"Centering used to be a CSS interview question. Now it's two lines."

The Two Verbs: Justify And Align

Flexbox has two alignment families, one per axis:

  • justify-* — acts on the main axis (the direction items flow).
  • align-* — acts on the cross axis (perpendicular to main).

This is the consistent vocabulary: justify = along, align = across. It carries through to Grid, where the same prefixes mean the same things.

justify-content: Where Items Sit Along Main

How extra space along the main axis is distributed:

  • flex-start — items packed to the start of the main axis (default). Extra space at the end.
  • flex-end — items packed to the end. Extra space at the start.
  • center — items packed in the middle. Extra space split between the two ends.
  • space-between — equal space between items; no space at the ends.
  • space-around — equal space around each item; ends get half the inter-item space.
  • space-evenly — equal space everywhere, including ends.

align-items: Where Items Sit Along Cross

How items align on the cross axis (within their line):

  • stretch — default. Items stretch to fill the cross axis.
  • flex-start — items align to the start of the cross axis.
  • flex-end — items align to the end.
  • center — items centered on cross axis.
  • baseline — items align by the baseline of their text. Useful when items have different font sizes and you want the text bottoms to line up.

Centering, In One Line Each

The famous "vertically center this" question, solved:

.container {
  display: flex;
  justify-content: center;  /* horizontal center */
  align-items: center;       /* vertical center */
}

Or, in Grid (even shorter): display: grid; place-items: center;.

align-self: Override For One Child

When most items should align one way but one needs to be different, align-self on the child overrides align-items on the parent:

align-content: For Multi-Line Flex

When flex-wrap: wrap creates multiple lines, align-content controls how those lines are distributed in the cross-axis space (similar to how justify-content distributes items in the main axis). It has the same values as justify-content: flex-start, center, space-between, etc.

Don't confuse align-content (lines) with align-items (items within a line). Both are cross-axis, but they operate at different levels.

The place-content Shorthand

place-content: center = align-content: center; justify-content: center;. place-items: center = align-items: center; justify-items: center; (Grid). place-self: center = align-self: center; justify-self: center; (Grid). Useful for terse code; readable code uses the longhand.

margin: auto As Alignment

A flex child with margin-left: auto consumes all available main-axis space on its left, pushing it to the right. This is how you create a navbar with a logo on the left and a menu on the right with no justify-content:

margin-right: auto does the opposite — pushes everything after it to the right. margin-top: auto on a column flex pushes it to the bottom. margin: auto centers in both axes (the source of the old "center with margin auto" pattern, now generalized).

justify-content for spacing across all items; margin: auto for a single push. If only one item needs to push siblings to the other side (like a logo + menu), margin: auto on that one item is cleaner than restructuring with justify-content: space-between and a wrapper.

Common Patterns, Reduced

Pippa's Note

The cwkPippa header is a flex row: logo on the left, settings buttons on the right. The implementation is nav { display: flex; align-items: center; gap: 0.5rem; } with .actions { margin-left: auto }. One declaration on one element pushes the buttons to the right edge regardless of how many menu items appear in between. The Council's brain picker uses justify-content: space-between to spread four brain cards across the width — same vocabulary, different layout intent.

Code

justify-content × align-items reference·css
/* Six justify-content values, visualized */
.demo {
  display: flex;
  outline: 2px dashed #ccc;
  height: 60px;
  margin: 1rem 0;
}
.demo > div { background: #5BA3D8; padding: 0.5rem 1rem; color: white; }

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

/* same for align-items on the cross axis: stretch (default), flex-start,
   flex-end, center, baseline */
Centering and margin: auto patterns·css
/* Common centering, by row vs column */

/* Centered button group */
.btn-row {
  display: flex;
  justify-content: center;   /* horizontal */
  align-items: center;        /* vertical for single-line items */
  gap: 1rem;
}

/* Full-page hero centering */
.hero {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  text-align: center;
}

/* OR with grid for terser syntax */
.hero-grid {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

/* Card with content top and CTA at bottom */
.card {
  display: flex;
  flex-direction: column;
  height: 300px;
  padding: 1.5rem;
}
.card .cta {
  margin-top: auto;          /* pushes CTA to bottom of column */
}
Real patterns using alignment·css
/* Navbar: logo left, links right, no nested containers */
.navbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid #eee;
}
.navbar .links {
  margin-left: auto;          /* push to the right edge */
  display: flex;
  gap: 1rem;
}

/* align-self override */
.review {
  display: flex;
  align-items: flex-start;     /* default for the row */
  gap: 1rem;
}
.review .timestamp {
  align-self: flex-end;        /* this one item aligns to the bottom */
  font-size: 0.85rem;
  color: #999;
}

/* baseline alignment for mixed text sizes */
.headline-with-byline {
  display: flex;
  align-items: baseline;
  gap: 1rem;
}
.headline-with-byline h2 { font-size: 2rem; }
.headline-with-byline span { font-size: 0.9rem; color: #666; }
/* The byline text bottom aligns with the h2 baseline */

External links

Exercise

Build three layouts using only justify-content, align-items, gap, and margin: auto: (1) a navbar with logo + links + user avatar where logo is left, avatar is right, links are in between; (2) a card with title, description, and a 'Learn more' link pinned to the bottom; (3) a single-line button row centered in a 200px-tall hero, with three different-height buttons aligned by baseline. Confirm each with DevTools' Flexbox overlay.
Hint
For the navbar, give the avatar margin-left: auto OR use justify-content: space-between if there are only three children. For the card, the bottom-pinned link wants margin-top: auto in a flex column. For baseline alignment, the trick is the buttons must have text content — baseline alignment on icon-only or empty elements falls back to other rules.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.