53

I'm trying create template for rows in my grid block:

grid-template-rows: repeat(3, 150px);

I know, this template should be work for first 3 rows. However, from 4 row this template is not work.
Can i make template for all rows?

P.S. This template work only for 1st row.

grid-template-rows: 150px;
1
  • You can just set height: 150px on rows. Commented Jul 31, 2017 at 9:59

1 Answer 1

100

Use grid-auto-rows (automatically generated rows) instead of grid-template-rows (manually generated rows). In current case grid-auto-rows: 150px will do the trick. Demo:

.grid {
  display: grid;
  grid-auto-rows: 150px;
  /* space between columns for demo */
  grid-gap: 10px;
}

/* just styles for demo */
.grid__item {
  background-color: tomato;
  color: white;
}
<div class="grid">
  <div class="grid__item">One</div>
  <div class="grid__item">Two</div>
  <div class="grid__item">Three</div>
  <div class="grid__item">Four</div>
  <div class="grid__item">Five</div>
  <div class="grid__item">Six</div>
  <div class="grid__item">Seven</div>
  <div class="grid__item">Eight</div>
  <div class="grid__item">Nine</div>
</div>

3
  • 4
    why grid-template-rows: repeat(auto-fit, 200px) and grid-template-columns: repeat(auto-fit, 200px) works differently? I mean grid-template-rows only repeats first row, but grid-template-columns repeats all columns.
    – NanoNova
    Commented Aug 29, 2020 at 6:28
  • 3
    @NanoNova Because your grid have default width of 100% of its container but default height is auto. Hope it makes sense to you. Commented Aug 29, 2020 at 19:12
  • idk why, but it was so difficult to find this simple property (grid-auto-rows). Thank you! +1 Commented Jul 17, 2022 at 7:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.