CSS Grid: Align an Item Horizontally using justify-self

In CSS Grid, the content of each item is located in a box which is referred to as a cell.
You can align the content's position within its cell horizontally using the justify-self property on a grid item.
By default, this property has a value of stretch, which will make the content fill the whole width of the cell.
This CSS Grid property accepts other values as well: 

stretch: default value that will fill to the cell,
start: aligns the content at the left of the cell,
center: aligns the content in the center of the cell,
end: aligns the content at the right of the cell.

1. div .item2 default with justify-self:stretch; or without it
1
2
3
4
5
2.div .item2 with justify-self:start;
1
2
3
4
5
3.div .item2 with justify-self:center;
1
2
3
4
5
4.div .item2 with justify-self:end;
1
2
3
4
5
html code


<style>
  .item1{background: LightSkyBlue;}
  
  .item2 {
    background: LightSalmon;
    justify-self:center;
  }
  
  .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;
  }
</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>

Comments

Popular posts from this blog

ES6 : 5 Prevent Object Mutation

ES6: 1 Explore Differences Between the var and let Keywords

CSS Grid: Reduce Repetition Using the repeat Function