Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

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

like image 299
Chris Avatar asked Jan 29 '17 01:01

Chris


People also ask

How do I change row height in CSS?

CSS Table Row height. You can set Row Height through CSS line-height property it set to each tr. When you set this property, each row has heigh of 50 pixel.

How do I set grid size in CSS?

The grid-template-columns Property The value is a space-separated-list, where each value defines the width of the respective column. If you want your grid layout to contain 4 columns, specify the width of the 4 columns, or "auto" if all columns should have the same width.

How do I reduce grid size in CSS?

To adjust the gap size, use grid-column-gap, grid-row-gap or grid-gap property in CSS.

How do I change my position in CSS grid?

Using transform. The translate() CSS function moves an element along the x- and y-axes. Using it together with the transform property allows you to change the position of a grid item.


1 Answers

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); } 
like image 196
Chris Avatar answered Sep 24 '22 16:09

Chris