The spacing between flex items can be controlled using the justify-content property in CSS Flexbox. The justify-content property is used to align and distribute flex items along the main axis of a flex container. It allows us to control the spacing between flex items both horizontally and vertically.
To adjust the spacing between flex items horizontally, we can use the following values for the justify-content property:
1. flex-start: This value aligns the flex items to the start of the flex container, creating no spacing between them. The items will be packed towards the start of the main axis.
2. flex-end: This value aligns the flex items to the end of the flex container, also creating no spacing between them. The items will be packed towards the end of the main axis.
3. center: This value centers the flex items along the main axis of the flex container. It creates equal spacing between the items, pushing them towards the center of the container.
4. space-between: This value distributes the flex items evenly along the main axis, with the first item aligned to the start and the last item aligned to the end. This creates equal spacing between all the items.
5. space-around: This value distributes the flex items evenly along the main axis, with equal spacing before the first item and after the last item. This creates equal spacing between all the items, including the space before the first and after the last item.
6. space-evenly: This value distributes the flex items evenly along the main axis, including the space before the first and after the last item. This creates equal spacing between all the items, including the space before the first and after the last item.
To demonstrate the usage of the justify-content property, consider the following example:
css
.container {
display: flex;
justify-content: space-between;
}
In this example, the flex items within the container will be distributed evenly along the main axis, with equal spacing between them. The first item will be aligned to the start of the container, and the last item will be aligned to the end.
To adjust the spacing between flex items vertically, we can combine the justify-content property with the align-items property. The align-items property is used to align flex items along the cross axis of a flex container. By default, it aligns the items to the center of the container.
For example, to center the flex items both horizontally and vertically within the container, we can use the following CSS:
css
.container {
display: flex;
justify-content: center;
align-items: center;
}
In this example, the flex items will be centered both horizontally and vertically within the container, creating equal spacing between them.
The justify-content property in CSS Flexbox allows us to control the spacing between flex items along the main axis of a flex container. By using different values for this property, we can achieve various arrangements and spacing effects for our flex items.
Other recent questions and answers regarding CSS Flexbox:
- What are the possible values for the flex-direction property and how do they affect the arrangement of flex items?
- What are the possible values for the flex-wrap property and how do they affect the wrapping behavior of content?
- How can we make a container section a flexbox in CSS?
- What are the differences between CSS grid and flexbox in terms of responsive design and browser support?

