# Flex Alignment

> Stop guessing how to center a div. Learn how Flexbox distributes space along the main and cross axes.

Subject: CSS | Lesson 21 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/flex-alignment
Previous: [Flexbox Fundamentals](https://codewithanimation.in/css/flexbox-fundamentals.md) | Next: [Flex Items: Sizing & Order](https://codewithanimation.in/css/flex-item-sizing.md)

## What it is

Flexbox alignment properties dictate how extra space is distributed inside the parent container.

`justify-content` aligns items along the **Main Axis**. `align-items` aligns items along the **Cross Axis**.

## Why it matters

Perfectly centering an element vertically used to be the hardest thing in CSS, often requiring negative margins, absolute positioning, or table-cell hacks. Flexbox alignment solved this entirely. Mastering these properties gives you absolute control over spacing.

## How it works

1. **Main Axis vs Cross Axis**: If `flex-direction: row` (the default), the Main Axis is horizontal (left to right) and the Cross Axis is vertical (top to bottom). If `flex-direction: column`, they swap!

2. **justify-content**: Controls alignment on the Main Axis. Common values: `flex-start`, `center`, `flex-end`, `space-between` (pushes items to the edges), and `space-evenly` (equal gaps everywhere).

3. **align-items**: Controls alignment on the Cross Axis. Common values: `stretch` (default), `flex-start` (top), `center`, and `flex-end` (bottom).

## Common mistake: The Axis Swap Confusion

The most frustrating part of learning Flexbox alignment is forgetting that the axes swap based on `flex-direction`.

If you set `flex-direction: column`, `justify-content` suddenly controls **vertical** alignment, and `align-items` controls **horizontal** alignment! Always ask yourself: 'Which way is my flex-direction flowing?'

## Tip: The Holy Grail of Centering

Want to perfectly center a child element both vertically and horizontally inside a container? Memorize this three-line combo applied to the parent:

`display: flex; justify-content: center; align-items: center;`

## Try it: Try it

Toggle the flex-direction to observe the Axis Swap in real-time. Then, experiment with justify-content and align-items to see how they control the items along the active axes.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/flex-alignment

## Check yourself

### Question 1

What happens to the 'Main Axis' if you set `flex-direction: column`?

- A. The Main Axis becomes vertical
- B. The Main Axis remains horizontal
- C. The Cross Axis becomes vertical

Correct answer: A

Flexbox alignment is entirely relative to the `flex-direction`. If the direction is column (top-to-bottom), the Main Axis points top-to-bottom.

- Why not B: The axes swap. Row means main is horizontal, Column means main is vertical.
- Why not C: The Cross Axis becomes horizontal.

### Question 2

Which property is responsible for distributing space between items along the Main Axis?

- A. justify-content
- B. align-items
- C. align-content

Correct answer: A

`justify-content` controls the Main Axis. If your direction is 'row', `justify-content: space-between` pushes the items horizontally apart.

- Why not B: align-items controls the Cross Axis.
- Why not C: align-content controls spacing between wrapped lines on the Cross Axis.

### Question 3

If you want to perfectly center a child both horizontally and vertically inside a 'row' container, what two properties do you apply to the parent?

- A. justify-content: center and align-items: center
- B. align-content: center and margin: auto
- C. text-align: center and vertical-align: middle

Correct answer: A

Applying `justify-content: center` centers along the main axis, and `align-items: center` centers along the cross axis. Together, they achieve perfect dead-center alignment.

- Why not B: align-content only works if flex-wrap is applied and multiple lines exist.
- Why not C: These are legacy inline-text alignment properties, they don't apply to flexbox layout.

## Remember this

- `justify-content` aligns items along the Main Axis.
- `align-items` aligns items along the Cross Axis.
- If `flex-direction` changes to `column`, the axes swap (justify becomes vertical, align becomes horizontal).
- `space-between` pushes the first and last items to the far edges.
