The justify-content property
justify-content is set on a flex or grid container and distributes free space between its items along the main axis: it packs them at the start or end, centers them or spreads them out evenly.
Syntax
justify-content: flex-start | center | flex-end | space-between | space-around | space-evenly | normal; - Initial value
normal- Inherited
- no
- Applies to
- flex, grid and multi-column containers
Values
| Value | What it does |
|---|---|
normal | the initial value; in a flex container it behaves as flex-start |
flex-start | items at the start of the main axis |
center | items in the center |
flex-end | items at the end of the main axis |
space-between | first and last items at the edges, the rest of the space shared equally between items |
space-around | equal space around each item (half as much at the edges as between items) |
space-evenly | equal gaps everywhere, edges included |
Example
.row {
display: flex;
gap: 8px;
padding: 8px;
background: #f1f5f9;
}
.row span {
padding: 6px 12px;
background: #0284c7;
color: #fff;
border-radius: 4px;
}
.start { justify-content: flex-start; }
.center { justify-content: center; }
.between { justify-content: space-between; }
.evenly { justify-content: space-evenly; } <p>flex-start</p><div class="row start"><span>1</span><span>2</span><span>3</span></div>
<p>center</p><div class="row center"><span>1</span><span>2</span><span>3</span></div>
<p>space-between</p><div class="row between"><span>1</span><span>2</span><span>3</span></div>
<p>space-evenly</p><div class="row evenly"><span>1</span><span>2</span><span>3</span></div> Result
Good to know
- It works along the main axis: with flex-direction: column the alignment becomes vertical.
- It needs free space. If items stretch (flex: 1 or flex-grow), there is nothing to distribute and the values look the same.
- To push just one item away (for example, a button to the end of a menu), margin-left: auto on that item is simpler.
- Alignment along the cross axis is set by another property, align-items.
Sources and specification
- MDN: the justify-content property — MDN Web Docs
- CSS Box Alignment Module Level 3 (W3C) — W3C
See also
CSS Flexbox Playground
Change flex-direction, justify-content, align-items, flex-wrap and gap and see how flexbox works right away. Copy the ready CSS code in one click.
The align-items property
The CSS align-items property aligns the items of a flex or grid container along the cross axis: stretch, center, flex-start, flex-end and baseline.
CSS Flexbox for beginners
A beginner's guide to CSS Flexbox: display: flex, axes, justify-content, align-items, flex-wrap, gap and flex: 1, with examples of a menu, cards and centering.
Flexbox: header menu and a row of cards
Flexbox practice exercise: put the logo and menu side by side in the header and line course cards up in an equal-height row that wraps on narrow screens.
Center an element horizontally and vertically
The simplest modern way to center a block using flexbox.
The display property
The CSS display property sets how an element takes part in layout: as a block, inline with text, as a flex or grid container, or not at all. Values, examples.
The margin property
The CSS margin property sets an element's outer spacing: the 1–4 value forms, margin: auto for centering, negative margins and how vertical margins collapse.