Number and width of rows in CSS grids

Grid items can also be placed in rows. The grid-template-rows property is used for this. This property takes the row sizes separated by a space. The same values ​​that we studied for columns can be used here.

Example

For example, let's arrange the blocks in four rows:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>
css#parent {
  display: grid;
  grid-template-rows: 50px 100px 50px 50px;
  border: 2px solid #696989;
  padding: 10px;
  width: 600px;
}
#parent > div {
  padding: 10px;
  border: 1px solid #696989;
}

Результат:

Example

Let's specify a fixed width in pixels for the first and third rows, and let the second row take up the remaining space:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
css#parent {
  display: grid;
  grid-template-rows: 100px auto 60px;
  border: 2px solid #696989;
  padding: 10px;
  width: 600px;
  height: 400px;
}
#parent > div {
  padding: 10px;
  border: 1px solid #696989;
}

Результат:

Example

We use the repeat function to specify the row sizes:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
css#parent {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  border: 2px solid #696989;
  padding: 10px;
  width: 600px;
  height: 200px;
}
#parent > div {
  padding: 10px;
  border: 1px solid #696989;
}

Результат:

Example

Let's assign the rows a height of 50px, and set the number of rows automatically using the value auto-fill:

html<div id="parent">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>
css#parent {
  display: grid;
  grid-template-rows: repeat(auto-fill, 50px);
  border: 2px solid #696989;
  padding: 10px;
  width: 400px;
  height: 200px;
}
#parent > div {
  padding: 10px;
  border: 1px solid #696989;
}

Результат:

Practical tasks

Задание 1

Place the blocks in three rows. Let the first block be 100px, the second 150px, and the third 200px.

Задание 2

Let's say you have three rows in your grid. Make them the same width.