This property is used to align flex items along the main axis of the flex container. It distributes any leftover free space when either all the flex items on a line are inflexible or are flexible but have reached their maximum size.
Example 1: Aligning to the Start
HTML
<div class="flex-container justify-start">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.justify-start {
justify-content: flex-start; /* Groups items at the start of the line (default). */
}
Explanation
justify-content: flex-start
packs the items toward the start of the main axis (left side in a row, top in a column). This is the default behavior.
Example 2: Aligning to the Center
HTML
<div class="flex-container justify-center">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.justify-center {
justify-content: center; /* Centers the items along the main axis. */
}
Explanation
justify-content: center
groups the flex items together at the center of the main axis, with equal space on either side of the group.
Example 3: Aligning to the End
HTML
<div class="flex-container justify-end">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.justify-end {
justify-content: flex-end; /* Groups items at the end of the line. */
}
Explanation
justify-content: flex-end
packs the items toward the end of the main axis (right side in a row, bottom in a column).
Example 4: Distributing Space Between
HTML
<div class="flex-container justify-between">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.justify-between {
justify-content: space-between; /* Distributes space between the items. */
}
Explanation
justify-content: space-between
distributes the extra space evenly between the flex items. The first item is on the start line, the last item is on the end line.
Example 5: Distributing Space Around
HTML
<div class="flex-container justify-around">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.justify-around {
justify-content: space-around; /* Distributes space around each item. */
}
Explanation
justify-content: space-around
distributes space such that each item has equal space on both sides. This results in the space between any two items being double the space before the first and after the last item.
Example 6: Distributing Space Evenly
HTML
<div class="flex-container justify-evenly">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
border: 2px solid #000;
}
.justify-evenly {
justify-content: space-evenly; /* Distributes space so the spacing between any two items is equal. */
}
Explanation
justify-content: space-evenly
distributes the space so that the spacing between any two items, and the space to the container edges, is identical.
Example 7: Justify Content in a Column
HTML
<div class="flex-container column-justify">
<div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
</div>
CSS
.flex-container {
display: flex;
flex-direction: column; /* The main axis is now vertical. */
height: 300px;
border: 2px solid #4CAF50;
}
.column-justify {
justify-content: space-between; /* Distributes items along the vertical axis. */
}
Explanation
When flex-direction
is column
, justify-content
works along the vertical axis. In this case, space-between
places the first item at the top, the last at the bottom, and the second in the middle.