Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the maximum width of a column in CSS Grid Layout?

What I want to achieve:

Using CSS Grid Layout, to have a page with a right column which size is derived from its content, but only up to 20% of the window width.

How I thought it would work:

div {    border-style: solid;  }    #container {    width: 300px;    height: 300px;    display: grid;    grid-template-columns: 1fr minmax(auto, 20%);  }
<div id="container">    <div>      some content    </div>    <div>      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus eu leo ac ultrices. Vivamus ornare, orci sed pretium sollicitudin    </div>  </div>

It looks good, but then when I remove the content of the second div, the left column does not collapse:

div {    border-style: solid;  }    #container {    width: 300px;    height: 300px;    display: grid;    grid-template-columns: 1fr minmax(auto, 20%);  }
<div id="container">    <div>      some content    </div>    <div></div>  </div>

My question:

I was under the impression that since minmax()

(...) defines a size range greater than or equal to min and less than or equal to max.

it would mean that in my case the width is set from auto (= 0 when the div is empty) to 20%. It however stays at 20%. Why is it so?

like image 209
WoJ Avatar asked Aug 02 '17 11:08

WoJ


People also ask

How do you change the column width in CSS grid?

Set the following on the grid container: grid-template-columns: auto 1fr; This sets the width of the first column to equal the width of the widest item in that column, and the width of the second column to get the remaining width of the grid.

How do you set the width of a grid?

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.

What is Minmax in CSS grid?

The minmax() CSS function defines a size range greater than or equal to min and less than or equal to max. It is used with CSS Grids.

How do you create a grid with 4 columns of equal width?

If you really need the columns to be the exact same width you should use: grid-template-columns: repeat(3, minmax(0, 1fr)); minmax(0, 1fr) allows the grid tracks to be as small as 0 but as large as 1fr , creating columns that will stay equal.


1 Answers

"The fit-content function accepts one param, the maximum value. A grid column/row with this property set will still take up as little space as necessary, according to its content, but no more than the maximum value."

See this article: Becoming a CSS Grid Ninja!

You can solve your case while still making use of percentage-based maximums:

div {    outline: 1px dotted gray;  }    .container {    width: 300px;    height: 300px;    display: grid;    grid-template-columns: 1fr fit-content(20%);  }    .container div {    overflow: hidden; /* this needs to be set so that width respects fit-content */  }
<div class="container">    <div>      some content    </div>    <div>      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus eu leo ac ultrices. Vivamus ornare, orci sed pretium sollicitudin    </div>  </div>    <div class="container">    <div>      some content    </div>    <div></div>  </div>
like image 63
WDyar Avatar answered Oct 11 '22 11:10

WDyar