# justify-content

> The `justify-content` CSS property distributes space between and around items of a flex or grid container along the main or inline axis.

## How do I center items horizontally with justify-content?

The property used in flexbox and grid layouts to align items along the main axis (horizontally in rows or vertically in columns). It can space items evenly, push them to the start, end or center, helping to control the overall layout and distribution of space.

- Topic: Alignment & Float
- Type: property
- Browser support: Chrome 29+, Chrome Android 29+, Edge 12+, Firefox 20+, Firefox Android 20+, Safari 9+, Safari iOS 9+

## Example

Wrong:

```css
.nav {
  display: flex;
}
.nav a {
  margin: 0 auto;
}
```

Right:

```css
.nav {
  display: flex;
  justify-content: center;
  gap: 16px;
}
```

`justify-content` and `gap` handle main-axis spacing on the container, with no per-item margins.

## Common mistakes

- `justify-content: center` does nothing because items have `flex: 1` and fill the row.
- Using `space-between` and the last row of wrapped items spreads out unevenly.

## Key points

- Even spacing uses `gap` with `justify-content`, not margins on each item.
- In a `flex-direction: column` container, `justify-content` is used for vertical alignment.

## Interview questions

### justify-content vs align-items: what is the difference?

`justify-content` aligns along the main axis. `align-items` aligns along the cross axis.

### space-between vs space-around vs space-evenly?

`space-between` puts no space at the edges, `space-around` puts half gaps at the edges, and `space-evenly` makes all gaps equal.

### Why is justify-content not working?

There is no free space: items fill the container because of `flex-grow`, or the parent is not a flex or grid container.

## Related cards

- [align-items](https://wonderium.dev/css/align-items.md)
- [place-content](https://wonderium.dev/css/place-content.md)
- [gap](https://wonderium.dev/css/gap.md)
- [flex-direction](https://wonderium.dev/css/flex-direction.md)
- [align-content](https://wonderium.dev/css/align-content.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/justify-content
- Last updated: 2026-11-25
