Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css-grid auto column width

Tags:

css

css-grid

I'm working on a UI with css grid and have been seeing a slightly odd behaviour, namely that if I define a grid with:

grid-template-columns: 50px auto 50px

I'm not seeing that grid expand to 100% of the width of the container as shown in this image: enter image description here

If I make the content in the auto area wider to the extend where it would force the grid to fill the space, the problem seems to go away. But my understanding and in all the examples i've seen suggests that this template should extend to 100% of the width of the viewport.

Am I missing something here? certainly feels like it, any thoughts?

like image 609
dougajmcdonald Avatar asked Dec 12 '17 19:12

dougajmcdonald


People also ask

How do you autofit in grid?

auto-fit behavior: “make whatever columns you have fit into the available space. Expand them as much as you need to fit the row size. Empty columns must not occupy any space. Put that space to better use by expanding the filled (as in: filled with content/grid items) columns to fit the available row space.”

How do you change the grid width of a column in CSS?

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.

What does auto do in grid-template-columns?

grid-template-columns: auto auto auto; You can use the keyword auto so that each column resizes itself automatically. grid-template-columns: 80px auto 1rem; You can mix the units.

What is auto fill in CSS grid?

Auto-fill: The auto-fill property fills the rows with as many columns as it can fit. The newly added column may be empty but it will still occupy a space in the given row. It is an important property in the CSS grid that make a responsive layout without writing a media query for each grid.


1 Answers

In your rule:

grid-template-columns: 50px auto 50px

The auto value sets the column width to the width of the content. auto means content-based.

So the total width of the columns ends up having no association with the width of the row.

50px + content width + 50px can be anything

If you want the columns to fill the width of the row, then use a fraction unit to tell the second column to consume all remaining space.

grid-template-columns: 50px 1fr 50px

More details here: Does CSS Grid have a flex-grow function?

like image 179
Michael Benjamin Avatar answered Oct 21 '22 23:10

Michael Benjamin