π§ justify-content β Aligning the Entire Grid in CSS Grid
π What is justify-content?
justify-content is a CSS Grid property used to control thehorizontal alignment of the entire grid inside its container. Unlike justify-items (which aligns items inside cells),justify-content moves the whole grid as a block.
>>βjustify-content doesnβt move grid items inside cells β it moves the whole grid.β
π When does justify-content work?
justify-content only works when:
- The gridβs total width is smaller than the container width
- You have extra free space horizontally
Note
π‘ If your grid spans the full width (e.g., 1fr columns),justify-content will have no visible effect.
π― Available Values
| Value | Description |
|---|---|
| start | Aligns the grid to the left |
| center | Centers the grid horizontally |
| end | Aligns the grid to the right |
| space-between | Max spacing between columns |
| space-around | Equal spacing around each track |
| space-evenly | Even spacing between all tracks |
π§ͺ Basic Example
Center the entire grid
.container {
display: grid;
grid-template-columns: 150px 150px 150px;
justify-content: center;
}The grid is centered inside the container, not stretched.
π Visual Example

π§± Example Using All Values
justify-content Variations
.start { justify-content: start; }
.center { justify-content: center; }
.end { justify-content: end; }
.spaceBetween { justify-content: space-between; }
.spaceAround { justify-content: space-around; }
.spaceEvenly { justify-content: space-evenly; }π§ justify-content vs justify-items
| Property | What It Aligns |
|---|---|
| justify-items | Items inside each cell (horizontal) |
| justify-content | The entire grid structure (horizontal) |
Note
β‘ Donβt confuse the two β
β’ justify-items = item alignment
β’ justify-content = grid alignment
β’ justify-items = item alignment
β’ justify-content = grid alignment
π¦ Real-World Use Cases
- Centering a fixed-width grid inside a wide screen
- Aligning product cards to the left or right
- Creating evenly spaced navigation grids
- Aligning dashboard widgets horizontally
π§© Responsive Example
Responsive justify-content
.container {
grid-template-columns: repeat(3, 200px);
justify-content: center;
}
@media (min-width: 1024px) {
.container {
justify-content: space-between;
}
}On smaller screens β items are centered
On large screens β spacing is distributed evenly
β οΈ Common Mistakes
- Trying to use justify-content when grid columns use fr units
- Expecting it to align individual items (use justify-items instead)
- Using justify-content on grid items instead of the container
Note
π§ If nothing happens when you use justify-content: Your grid is likely already full width!
π₯ Summary
justify-content controls how the entire grid is positioned horizontally inside its container. Itβs essential for centered layouts, evenly spaced tracks, and responsive design patterns.
>>βjustify-content moves the grid as a whole β like sliding the entire layout left, right, or center.β
Learn more here βMDN Docs π