# Flexbox

> The CSS layout mode for arranging items in a row or a column, with full control over spacing and alignment.

Subject: CSS | Lesson 1 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/flexbox
Next: [How CSS Works: Syntax & Linking](https://codewithanimation.in/css/how-css-works.md)

## What it is

Flexbox is a layout mode in CSS for placing items in a single direction, either a row or a column. You turn it on with `display: flex` on a container, and its direct children become flexible items that share the space along one line.

Before flexbox, lining boxes up and centering them meant floats, inline-block quirks, and margin hacks. Flexbox replaces all of that with a small set of properties that say what you actually mean: pack these to the center, push them to the edges, let this one grow.

## Why it matters

You reach for flexbox constantly on real work: navigation bars, button toolbars, a row of cards, a label next to an icon, centering a thing both ways. It is the everyday tool for one-dimensional layout.

Knowing flexbox well is the difference between fighting CSS and directing it. Most layout bugs juniors hit are really a misunderstanding of which axis a property acts on.

## How it works

Setting `display: flex` on a container creates a flex context. Inside it there are two axes: the main axis (the direction items flow) and the cross axis (perpendicular to it). `flex-direction` decides which is which: `row` (default) runs the main axis left to right, `column` runs it top to bottom.

`justify-content` positions items along the main axis. Common values are `flex-start`, `center`, `space-between`, and `space-around`. `align-items` positions them along the cross axis, so `align-items: center` vertically centers a row of items.

`gap` adds consistent space between items without margin hacks. For sizing, `flex-grow` lets an item expand to fill spare space, `flex-shrink` lets it give space back, and `flex-basis` sets its starting size. The shorthand `flex: 1` is the one you will type most, meaning grow to share space equally.

## Common mistake: Watch the axes when you switch direction

`justify-content` works along the main axis and `align-items` works along the cross axis, so the moment you switch `flex-direction` to `column`, those two swap meaning. People set `align-items: center` expecting horizontal centering, then it stops working after they change direction, and the confusion begins.

## Tip: The reliable way to center anything

Set `display: flex` then `justify-content: center` and `align-items: center` on the parent and the child sits dead center, both ways. Reach for `gap` instead of margins between items, and use `flex: 1` to make an item fill the leftover space.

## Try it: Try it

Change a property and watch the boxes move to match. Notice how flipping the direction to `column` changes what `justify-content` does.

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

## Check yourself

### Question 1

In a row, which property moves items left and right along the main axis?

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

Correct answer: A

justify-content positions items along the main axis. In the default row direction, the main axis runs left to right, so it controls horizontal placement.

- Why not B: align-items works on the cross axis, so in a row it moves items up and down.
- Why not C: flex-direction sets which axis is the main one; it does not position items along it.

### Question 2

You set flex-direction: column. What does justify-content now control?

- A. Vertical placement (top to bottom)
- B. Horizontal placement (left to right)
- C. Nothing, it only works on rows

Correct answer: A

justify-content always acts along the main axis. In a column the main axis is vertical, so it now spaces items top to bottom. This is the axis swap that trips people up.

- Why not B: That was true for a row. Switching to column rotates the main axis to vertical.
- Why not C: justify-content always works, it just follows the main axis wherever it points.

### Question 3

What is the cleanest way to add even space between flex items?

- A. gap
- B. A margin on every item
- C. Empty spacer divs

Correct answer: A

gap adds consistent spacing between items only, with no leftover edge margins and no extra markup. It is the modern, intended tool.

- Why not B: Margins leave an extra gap on the outer edges and need fiddly resets.
- Why not C: Spacer elements add markup that has nothing to do with the content.

## Remember this

- `display: flex` turns a container into a one-dimensional layout, row or column.
- `flex-direction` sets the main axis; `row` is the default.
- `justify-content` aligns along the main axis; `align-items` aligns along the cross axis.
- `gap` spaces items cleanly; `flex: 1` makes an item grow to share space.
