CSS Grid: Justification and Alignment

With CSS grid layout, the grid itself within its container as well as grid items can be positioned with the following 6 properties: justify-items, align-items, justify-content, align-content, justify-self and align-self. These properties are part of the CSS box alignment module and they define a standard way to position elements with either flexbox or CSS grid.

Most of the alignment properties are applied on the grid container, but some are for grid items, for when you want to specify values that apply only for specific grid items. Let’s breakdown these properties and their effect one by one.

For the examples to follow, we’re using the following markup and CSS rules for our grid:

<div class="container">   <div class="item"></div>   <div class="item"></div>   <div class="item"></div>   <div class="item"></div>   <div class="item"></div>   <div class="item"></div> </div> 
.container {   display: grid;   border: 2px dashed rgba(114, 186, 94, 0.35);   height: 250px;   grid-template-columns: 1fr 1fr 1fr;   grid-template-rows: auto;   background: rgba(114, 186, 94, 0.05); } .item {   width: 50%;   height: 50%;   background: rgba(255, 213, 70, 0.4);   border: 2px dashed rgba(236, 198, 48, 0.5); } 

And here’s how our base grid looks like:

Properties on the Grid Container


justify-items is used to justify the grid items along the row axis. The possible values are start, end, center and stretch. Here are examples with center and end:

.container {   /* ... */   justify-items: center; } 
.container {   /* ... */   justify-items: end; } 


Similar to justify-items, but aligns the grid items along the column axis:

.container {   /* ... */   align-items: center; } 
.container {   /* ... */   align-items: end; } 

For the following justify-content and align-content examples, our base styles differ a bit in order to be able to demonstrate a grid that’s smaller than its container:

.container {   /* ... */   grid-template-columns: 100px 100px 100px;   grid-template-rows: 75px;   height: 300px; } .item {   width: 60px;   height: 60px;   /* ... */ } 


When the entire grid is smaller than the space for the grid container, use justify-content to justify the grid along the row axis. You can use the following values: start, end, center, stretch, space-around, space-between or space evenly:

.container {   /* ... */   justify-content: end; } 
.container {   /* ... */   justify-content: space-evenly; } 


Similar to justify-content, but align-content aligns the grid along the column axis:

.container {   /* ... */   align-content: end; } 
.container {   /* ... */   align-content: space-evenly; } 

Properties on the Grid Items

Both justify-self and align-self are analogous to the equivalent properties available on the container (justify-items and align-items), but they apply on specific items that should be positioned differently than the rest of the grid items.

justify-self (row axis)

<div class="container">   <div class="item"></div>   <div class="item"></div>   <div class="item" style="justify-self: end;"></div>   <div class="item"></div>   <div class="item"></div>   <div class="item"></div> </div> 

align-self (column axis)

<div class="container">   <div class="item"></div>   <div class="item"></div>   <div class="item"></div>   <div class="item" style="align-self: end;"></div>   <div class="item"></div>   <div class="item"></div> </div>