Alignment within CSS grid cells along the vertical axis
To align items within grid cells along the vertical axis, we use the align-items property, which is set in the parent element. You can see this property in action when viewing the grid in the browser debugger.
At the beginning of the axis
Let's align our elements inside the cells to the start of the vertical axis:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
#parent {
display: grid;
align-items: flex-start;
grid-template-columns: 100px 100px;
grid-gap: 10px;
padding: 10px;
border: 2px solid #696989;
height: 200px;
width: 400px;
}
#parent > div {
grid-gap: 10px;
padding: 10px;
box-sizing: border-box;
border: 1px solid #696989;
}
:
Let's look at our grid in the browser debugger:
At the center of the axis
Now let's align our elements in the cells along the center of the vertical axis:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
#parent {
display: grid;
align-items: center;
grid-template-columns: 100px 100px;
grid-gap: 10px;
padding: 10px;
border: 2px solid #696989;
height: 200px;
width: 400px;
}
#parent > div {
grid-gap: 10px;
padding: 10px;
box-sizing: border-box;
border: 1px solid #696989;
}
:
Let's look at our grid in the browser debugger:
At the end of the axis
Let's change the alignment of the elements again, this time along the end of the vertical axis:
<div id="parent">
<div id="elem1">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
#parent {
display: grid;
align-items: end;
grid-template-columns: 100px 100px;
grid-gap: 10px;
padding: 10px;
border: 2px solid #696989;
height: 200px;
width: 400px;
}
#parent > div {
grid-gap: 10px;
padding: 10px;
box-sizing: border-box;
border: 1px solid #696989;
}
#elem1 {
}
:
Let's look at our grid in the browser debugger:
Practical tasks
Create a grid of six elements arranged in two columns. Align the elements within the cells to the start of the vertical axis.
Now arrange the grid elements across three columns and set the alignment of the elements in the cells to be centered on the vertical axis of the grid.
Modify the previous task so that the elements are aligned at the end of the vertical axis.