CSS Justify Content
The CSS justify-content property controls how Flex Items are positioned along the main axis of a Flex Container.
When the Flex Direction is row, the main axis is horizontal. Therefore, justify-content controls the horizontal distribution of the Flex Items.
When the Flex Direction is column, the main axis becomes vertical, so justify-content controls the vertical distribution.
Syntax
.container {
display: flex;
justify-content: value;
}
Understanding the Main Axis
The main axis is the primary direction in which Flex Items are arranged.
With flex-direction: row;, the main axis runs horizontally.
With flex-direction: column;, the main axis runs vertically.
Justify Content Diagram
justify-content controls the distribution of items along the main axis.
Values of justify-content
| Value | Purpose |
|---|---|
| flex-start | Places items at the beginning of the main axis. |
| flex-end | Places items at the end of the main axis. |
| center | Places items in the center of the main axis. |
| space-between | Places equal space between items. |
| space-around | Places space around each item. |
| space-evenly | Creates equal space between items and the container edges. |
1. justify-content: flex-start
flex-start places the Flex Items at the beginning of the main axis.
.container {
display: flex;
justify-content: flex-start;
}
Output
2. justify-content: flex-end
flex-end places the Flex Items at the end of the main axis.
.container {
display: flex;
justify-content: flex-end;
}
Output
3. justify-content: center
center places the Flex Items in the center of the main axis.
.container {
display: flex;
justify-content: center;
}
Output
4. justify-content: space-between
space-between places the first item at the beginning and the last item at the end. The remaining space is distributed between the items.
.container {
display: flex;
justify-content: space-between;
}
Output
5. justify-content: space-around
space-around gives each Flex Item space around it.
.container {
display: flex;
justify-content: space-around;
}
Output
6. justify-content: space-evenly
space-evenly distributes equal space between the Flex Items and the edges of the container.
.container {
display: flex;
justify-content: space-evenly;
}
Output
Comparing justify-content Values
flex-start
Items are grouped at the beginning of the main axis.
flex-end
Items are grouped at the end of the main axis.
center
Items are grouped in the center of the main axis.
space-between
Equal space is placed between the items.
space-around
Space is distributed around each item.
space-evenly
Equal spacing is maintained between items and container edges.
Example 🌍🤓
A website navigation bar can use justify-content to control the position of navigation items.
.navbar {
display: flex;
justify-content: space-between;
}
With space-between, one group can stay at the beginning while another group can stay at the opposite end of the container.
This is commonly useful for layouts containing a logo on one side and navigation or action items on the other side.
Summary
The justify-content property controls the distribution of Flex Items along the main axis. Common values include flex-start, flex-end, center, space-between, space-around, and space-evenly. Its behavior depends on the direction of the Flex Container.