justify-content
Positions items along the main axis of a flex or grid container.
- Initial value
normal- Inherited
- No
- Animatable
- no
- Applies to
- flex containers, grid containers, and multi-column containers
- JavaScript
element.style.justifyContent = "center"
Example
#.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
}<nav class="toolbar stage">
<strong>CodeWorlds</strong>
<span>Courses · Documentation · Collections</span>
</nav>Definition and usage
#The justify-content property decides what to do with the free space on a container’s main axis. In Flexbox, with flex-direction: row the main axis runs horizontally, so items can move left, right, to the center, or spread out with equal spacing.
It only works when free space remains. If the items fill the whole container (for example they have flex-grow: 1), changing the value has no effect.
In CSS Grid, the same property positions entire columns inside the container when the grid is narrower than the container.
Syntax
#justify-content: normal | flex-start | flex-end | center | space-between | space-around | space-evenly | start | end;Values
#- Items line up at the start of the main axis. With the normal row direction, that’s the left edge.
- Items move to the end of the main axis.
- Items group together in the center, and the free space splits evenly on both sides.
- The first item sits at the start, the last at the end, and the remaining space is split evenly between the items.
- Each item gets the same space on both sides, so the gaps at the edges are half the size of the gaps between items.
- All the gaps are identical: between items and at the edges of the container.
Values in practice
#flex-start
.demo { justify-content: flex-start; }flex-end
.demo { justify-content: flex-end; }center
.demo { justify-content: center; }space-between
.demo { justify-content: space-between; }space-around
.demo { justify-content: space-around; }space-evenly
.demo { justify-content: space-evenly; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- To center a single item both horizontally and vertically,
display: flex; justify-content: center; align-items: center;is all you need. - Use
gapinstead of margins between items.justify-contentwill only have to handle the remaining free space.
Related entries
#- align-itemsAligns items along the cross axis of a flex or grid container.
- flex-directionSets the direction of the main axis: flex items line up in a row or in a column.
- gapSets the spacing between items in a flex, grid, or multi-column container.
- align-contentPositions entire lines of items along a container’s cross axis.
- displayDetermines how an element behaves in the page layout and how it lays out its children.
Spotted a mistake or missing an example? Write to us.