The justify-self
CSS property is a sub-property of the CSS Box Alignment module. It is used to set the alignment of an individual grid item along the inline (main) axis within a grid container.
Example 1: justify-self: start
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
width: 100%;
}
.item-1 {
justify-self: start; /* Aligns the item to the start of its grid area */
background-color: lightblue;
}
Explanation
This code aligns the grid item with the class .item-1
to the very beginning of its designated grid cell along the row or inline axis. The start
value positions the item at the leftmost edge of its area.
Example 2: justify-self: end
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.item-2 {
justify-self: end; /* Aligns the item to the end of its grid area */
background-color: lightcoral;
}
Explanation
Here, justify-self: end;
pushes the grid item (.item-2
) to the far right edge of its grid area. This is the opposite of the start
alignment, placing it at the end of the inline axis.
Example 3: justify-self: center
.grid-container {
display: grid;
grid-template-columns: auto auto;
}
.item-3 {
justify-self: center; /* Horizontally centers the item within its grid area */
background-color: lightgreen;
}
Explanation
The center
value perfectly centers the grid item horizontally within its cell. The item (.item-3
) will have equal space on its left and right sides inside its grid track.
Example 4: justify-self: stretch
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
}
.item-4 {
justify-self: stretch; /* Stretches the item to fill the grid area's width */
background-color: lightgoldenrodyellow;
}
Explanation
stretch
is the default value for justify-self
. It makes the grid item expand to completely fill the entire width of its grid cell along the inline axis.
Example 5: justify-self with auto
.grid-container {
display: grid;
justify-items: center; /* Center all items by default */
grid-template-columns: 1fr 1fr;
}
.item-5 {
justify-self: auto; /* Inherits the justify-items value from the container */
background-color: lightpink;
}
Explanation
The auto
value inherits the justify-items
property from the grid container. In this case, since the container has justify-items: center
, .item-5
will also be centered.
Example 6: Overriding container alignment
.grid-container {
display: grid;
justify-items: end; /* Default alignment for all items is 'end' */
grid-template-columns: repeat(2, 1fr);
}
.item-6 {
justify-self: start; /* Overrides the container's alignment */
background-color: lightseagreen;
}
Explanation
This demonstrates how justify-self
on an item can override the justify-items
property of its parent grid container. While other items would align to the end, .item-6
is aligned to the start.
Example 7: justify-self in a complex grid
.grid-container {
display: grid;
grid-template-columns: 100px 1fr 100px;
}
.item-7 {
grid-column: 2 / 3;
justify-self: center; /* Centers the item in the flexible middle column */
background-color: lightskyblue;
}
Explanation
Even in a more complex grid layout with fixed and flexible columns, justify-self: center;
precisely centers the content of .item-7
within the available space of its specified column.