CSS Grid: Align All Items Horizontally using justify-items
Sometimes you want all the items in your CSS Grid to share the same
alignment.
You can use the previously article on horizontally align elements using justify-self and align them individually, or you can align them all at once horizontally by using
This property can accept all the same values justify-self accepts, the difference being that it will move all the items in our grid to the desired alignment.
1.stretch
You can use the previously article on horizontally align elements using justify-self and align them individually, or you can align them all at once horizontally by using
justify-items
on your grid container.This property can accept all the same values justify-self accepts, the difference being that it will move all the items in our grid to the desired alignment.
stretch: default value that all divs will fill to the cells, start: aligns the content at the left of all cell,center: aligns the content in the center of all cell,end: aligns the content at the right of all cell.
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
<style> .item1{background:LightSkyBlue;} .item2{background:LightSalmon;} .item3{background:PaleTurquoise;} .item4{background:LightPink;} .item5{background:PaleGreen;} .container { font-size: 40px; min-height: 300px; width: 100%; background: LightGray; display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-gap: 10px; justify-items: ; } </style> <div class="container"> <div class="item1">1</div> <div class="item2">2</div> <div class="item3">3</div> <div class="item4">4</div> <div class="item5">5</div> </div>
source
Comments
Post a Comment