Alignment within CSS grid cells along the horizontal axis

To align elements within grid cells, i.e. areas that arise when columns and rows intersect, the justify-items property is used, which is set in the parent element. The alignment can be most clearly seen 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 horizontal axis:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
css#parent {
  display: grid;
  justify-items: start;
  grid-template-columns: 100px 100px;
  grid-template-rows: repeat(3, 1fr);
  grid-gap: 10px;
  padding: 10px;
  height: 200px;
  width: 400px;
  border: 2px solid #696989;
}
#parent > div {
  grid-gap: 10px;
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #696989;
}

Результат:

Now let's look at our grid in the debugger:

At the center of the axis

Let's align our elements in the cells to the center of the horizontal axis:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
css#parent {
  display: grid;
  justify-items: center;
  grid-template-columns: 100px 100px;
  grid-template-rows: repeat(3, 1fr);
  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;
}

Результат:

Now let's look at our grid in the debugger:

At the end of the axis

Let's align our elements to the end of the horizontal axis:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
css#parent {
  display: grid;
  justify-items: end;
  grid-template-columns: 100px 100px;
  grid-template-rows: repeat(3, 1fr);
  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;
}

Результат:

Now let's look at our grid in the debugger:

Practical tasks

Задание 1

Create a grid of five elements arranged in three rows. Align the elements along the horizontal axis.

Задание 2

Now arrange the grid elements in two rows and set the alignment of the elements in the cells to the center of the horizontal axis.

Задание 3

Modify the previous task so that the elements are aligned at the end of the horizontal axis.