Aligning Content Along the Vertical Axis in CSS Grid
To align content along the vertical axis of the grid, we use the align-content property, which is set on the parent element.
At the beginning of the axis
Let's set the alignment for our grid items 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-content: 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;
}
:
At the center of the axis
Now let's set the alignment of the elements to 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-content: 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;
}
:
At the end of the axis
Let's set the alignment of elements to the end 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-content: 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;
}
:
Practical tasks
Create a grid of six elements arranged in two rows. Align the elements to the start of the grid's vertical axis.
Now arrange the grid elements in two rows and set the alignment of the elements to the center of the vertical axis of the grid.
Modify the previous task so that the elements are aligned at the end of the vertical axis.