[html] How do I specify row heights in CSS Grid layout?

I have a CSS Grid Layout in which I want to make some (middle 3) rows stretch to their maximum size. I'm probably looking for a property similar to what flex-grow: 1 does with Flexbox but I can't seem to find a solution.

Note: This is intended for an Electron app only, so browser compatibility is not really a concern.

I have the following CSS Grid Layout:

_x000D_
_x000D_
.grid {_x000D_
  display: grid;_x000D_
  grid-template-columns: 1fr 1.5fr 1fr;_x000D_
  grid-gap: 10px;_x000D_
  height: calc(100vh - 10px);_x000D_
}_x000D_
_x000D_
.grid .box {_x000D_
  background-color: grey;_x000D_
}_x000D_
_x000D_
.grid .box:first-child,_x000D_
.grid .box:last-child {_x000D_
  grid-column-start: 1;_x000D_
  grid-column-end: -1;_x000D_
}_x000D_
_x000D_
/* These rows should 'grow' to the max height available. */_x000D_
.grid .box:nth-child(n+5):nth-child(-n+7) {_x000D_
  grid-column-start: 1;_x000D_
  grid-column-end: -1;_x000D_
}
_x000D_
<div class="grid">_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
  <div class="box"></div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Which creates the following grid:

Current Grid




When none of the boxes contain any content I would like the grid to look something like this:

Preferred Grid

This question is related to html css grid-layout css-grid

The answer is


One of the Related posts gave me the (simple) answer.

Apparently the auto value on the grid-template-rows property does exactly what I was looking for.

.grid {
    display:grid;
    grid-template-columns: 1fr 1.5fr 1fr;
    grid-template-rows: auto auto 1fr 1fr 1fr auto auto;
    grid-gap:10px;
    height: calc(100vh - 10px);
}

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to grid-layout

Bootstrap 4, how to make a col have a height of 100%? Equal height rows in CSS Grid Layout Prevent content from expanding grid items How do I specify row heights in CSS Grid layout? Load arrayList data into JTable Grid of responsive squares Flex-box: Align last row to grid GridView VS GridLayout in Android Apps

Examples related to css-grid

CSS Grid Layout not working in IE11 even with prefixes Centering in CSS Grid Equal height rows in CSS Grid Layout force css grid container to fill full screen of device Prevent content from expanding grid items CSS grid wrapping How do I specify row heights in CSS Grid layout?