This may not work in your case, but it's worth mentioning this lesser-known difference between justify-content: space-between
and justify-content: space-around
.
From the flexbox specification:
8.2. Axis Alignment: the justify-content
property
The justify-content
property aligns flex items along the main axis
of the current line of the flex container.
There are five values that apply to justify-content
. Here are two of them:
space-between
Flex items are evenly distributed in the line.
If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to flex-start
.
This explains why your items align left on wrap with space-between
.
Now look at space-around
:
space-around
Flex items are evenly distributed in the line, with half-size spaces
on either end.
If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to center
.
Hence, to center-align flex items on wrap, consider using space-around
.
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around; /* ADJUSTMENT */
border: 1px solid black;
}
.item {
width: 200px;
height: 100px;
background-color: blue;
margin: 25px;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…