Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Grid - Auto height rows, sizing to content

Tags:

css

css-grid

I have two grids nested within a grid. Unfortunately the right nested grid .grid-3 sets the height of the rows so that both the left and right grid are the same height, the extra space is shared among the divs with the class .right. How can I set the rows of the right nested grid to adjust to the size of the content, so they are the same height as the left nested rows?

div {    border: 1px dotted black;  }  .grid-2 {    display: grid;    grid-template-columns: repeat(2, 1fr);    grid-auto-rows auto;  }    .grid-3 {    display: grid;    grid-template-columns: 1fr 1fr 1fr;    grid-auto-rows auto;  }    .left {    background-color: red;  }     .right {    background-color: green;  }
<div class="grid-2">        <div class="grid-2">          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>       </div>       <div class="grid-3">           <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>       </div>  </div>
like image 924
CalAlt Avatar asked Apr 06 '18 21:04

CalAlt


People also ask

How do I make my row height fit content?

Change the row height to fit the contents Select the row or rows that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Row Height.

How do you set auto height on grid?

To allow the grid to auto-size it's height to fit rows, set grid property domLayout='autoHeight' .

What does grid template rows Auto do?

auto. The auto keyword behaves similarly to minmax(min-content, max-content) in most cases. Since auto track sizes can be stretched by the align-content and justify-content properties, they will take up any remaining space in the grid container by default auto -matically.


1 Answers

You can try minmax(min-content, max-content) ref

div {    border: 1px dotted black;  }  .grid-2 {    display: grid;    grid-template-columns: repeat(2, 1fr);    grid-auto-rows: minmax(min-content, max-content);  }    .grid-3 {    display: grid;    grid-template-columns: 1fr 1fr 1fr;    grid-auto-rows: minmax(min-content, max-content);  }    .left {    background-color: red;  }     .right {    background-color: green;  }
<div class="grid-2">        <div class="grid-2">          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>       </div>       <div class="grid-3">           <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>       </div>  </div>

You can also use only max-content or min-content

div {    border: 1px dotted black;  }  .grid-2 {    display: grid;    grid-template-columns: repeat(2, 1fr);    grid-auto-rows: max-content; /* min-content*/  }    .grid-3 {    display: grid;    grid-template-columns: 1fr 1fr 1fr;    grid-auto-rows: max-content; /* min-content*/  }    .left {    background-color: red;  }     .right {    background-color: green;  }
<div class="grid-2">        <div class="grid-2">          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>          <div class="left">L</div>       </div>       <div class="grid-3">           <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>          <div class="right">R</div>       </div>  </div>
like image 120
Temani Afif Avatar answered Sep 18 '22 11:09

Temani Afif