Distribute flex items along the main axis.
justify-content places the items along the main axis using any leftover space. It works on each flex line after the items have grown or shrunk.
.actions {
display: flex;
justify-content: center;
gap: 1rem;
}Assume the direct children are three buttons. In the initial row direction, they form a centered group, with a 1rem gap between neighbors.
With a 600-pixel content width, three 100-pixel items, and two 16-pixel gaps, 268 pixels remain. Centering puts 134 pixels before the group and 134 pixels after it. This example assumes no margins, borders, or padding affect those measurements.
| Value | Effect along the main axis |
|---|---|
flex-start | Packs items toward main-start. |
flex-end | Packs items toward main-end. |
center | Centers the group. |
space-between | Places remaining space between items, with none added at the outer edges. |
space-around | Adds an equal share on each side of an item. At an outer edge this contributes one share, between items it contributes two. |
space-evenly | Distributes equal shares of spare space before, between, and after the items. A declared gap still adds extra room between neighbors. |
gap still applies. With distributed alignment, the visible space between items can be larger than the declared gap.
.stack {
display: flex;
flex-direction: column;
min-height: 20rem;
justify-content: space-between;
}In horizontal text, the main axis now runs vertically. The extra height provides room for the first and last items to move apart. Without extra main-axis space, changing the distribution may have little visible effect.
For a wrapped container, each line performs its own main-axis distribution. A line containing one item will not reproduce the spacing of a line containing three.
.toolbar {
display: flex;
gap: 1rem;
}
.toolbar > .account-link {
margin-inline-start: auto;
}In a normal row, the auto margin takes the spare space before the account link. The earlier items stay together and the account link moves to the far end.
justify-self does not align individual flex items on the main axis. Use an auto margin for this layout.
In the 600px example, what happens to the 268px of spare space if center becomes space-between? Keep the two 16px gaps.
The 268px is divided between the two spaces: 134px is added to each existing 16px gap, for 150px between neighbors. No extra space is added at the edges.
If the buttons grow until they fill the row, why might changing justify-content make no visible difference?
There is no positive free space left to distribute after sizing and gaps.
justify-content follows the chosen flex direction.Try it yourself
HTML + CSS