stretch, the default, stretches each item to fill its entire cell's width, while start, end, and center align the item within its cell without stretching it, sizing it instead according to its own natural content size. This is easily confused with justify-content, but the two operate at entirely different levels: justify-content distributes space between and positions the grid's tracks as a whole within the container, while justify-items instead aligns each individual item horizontally within the specific cell that item already occupies — the related justify-self property overrides justify-items' default for one single, individual item, mirroring how align-self overrides align-items in flexbox.
1Understanding Justify-items
stretch, the default, stretches each item to fill its entire cell's width, while start, end, and center align the item within its cell without stretching it, sizing it instead according to its own natural content size. This is easily confused with justify-content, but the two operate at entirely different levels: justify-content distributes space between and positions the grid's tracks as a whole within the container, while justify-items instead aligns each individual item horizontally within the specific cell that item already occupies — the related justify-self property overrides justify-items' default for one single, individual item, mirroring how align-self overrides align-items in flexbox.
Don't confuse justify-items with justify-content — justify-items aligns each item within its own individual grid cell, while justify-content positions and distributes space around the grid's tracks as a whole within the container.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
justify-items: center;
}2Practical Example
Here is a real-world application of Justify-items showing how it is used in production CSS code.
.grid {
display: grid;
justify-items: stretch;
}
.special-item {
justify-self: end;
}3Best Practices
Follow these guidelines when working with Justify-items:
1. Use justify-items: center, or start/end, when items should align to a specific side within their own cell, rather than stretching to fill it, the default
2. Use justify-self on an individual grid item for a targeted alignment exception, rather than changing the whole grid's justify-items setting
3. Remember justify-items operates on items within their own cells, distinct from justify-content, which positions the grid's tracks as a whole within the container
Tip: Don't confuse justify-items with justify-content — justify-items aligns each item within its own individual grid cell, while justify-content positions and distributes space around the grid's tracks as a whole within the container.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
justify-items: center;
}