Aligning Content Across Both Axes in CSS Grid
Often in a grid you may want to align content along both the horizontal and vertical axes of the grid. For this purpose, we can use together the two properties you covered in the previous lesson: justify-content and align-content.
At the beginning of the vertical and the end of the horizontal
html<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>css#parent {
display: grid;
justify-content: flex-end;
align-content: 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;
}
Результат:
At the end of the vertical and the beginning of the horizontal
html<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>css#parent {
display: grid;
justify-content: flex-start;
align-content: flex-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;
}
Результат:
Ppo center of vertical axis and horizontal
html<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>css#parent {
display: grid;
justify-content: flex-end;
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;
}
Результат:
Practical tasks
Задание 1
Create a grid of six elements and arrange them in two columns. Align the elements to the start of the horizontal axis and the center of the vertical axis of the grid.
Задание 2
Now arrange the grid items across three columns and set the alignment of the items to the center of the horizontal axis and the end of the vertical axis of the grid.
Задание 3
Modify the previous task so that elements are aligned to the end of the horizontal and center of the vertical grid axes.