28

Recently, I created a layout using CSS grid. While this works well, I'm confused by how it works. Specifically, I'm confused about the line grid-template-rows: auto auto 1fr auto;.

What ended up happening here is that the header and footer sized according to the content, which makes sense since they spanned the width of the page. The article itself sized according to its content. But, the "title" and "nav" were split such that the title sized according to content and nav took the rest of the space.

Had I used grid-template-rows: auto auto auto auto; instead, the title and nav would've shared equal spacing, which was not what I wanted.

How was the auto auto 1fr auto interpreted so that it gave me the sizing such that title took the minimum required space and nav took the rest? How does "fr" and "auto" interact in this scenario?

main {
    display: grid;
    grid-template-columns: 10rem auto;
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas: "header header" "title article" "nav article" "footer footer";
}
    
header {
  grid-area: header;
  background: lightblue;
}

div {
  grid-area: title;
  background: orange;
}

nav {
  grid-area: nav;
  background: pink;
}

article {
  grid-area: article;
  background: yellow;
}
  
footer {
  grid-area: footer;
  background: lightblue;
}
<main>
<header>This is the header</header>
<div>This is the title</div>
<nav>This is the nav</nav>
<article>
  This is the article. This is the article. This is the article. This is the article.
  This is the article. This is the article. This is the article. This is the article.
  This is the article. This is the article. This is the article. This is the article.
  This is the article. This is the article. This is the article. This is the article.
  This is the article. This is the article. This is the article. This is the article.
  This is the article. This is the article. This is the article. This is the article.
</article>
<footer>This is the footer</footer>
</main>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • You have 4 rows in your grid - all (except for 3rd) are `auto`-sized. So first, second and fourth rows `auto`-sized, and third (containing `nav`) took all the remaining space. Try setting `grid-template-rows: auto 1fr 2fr auto;` to see what happens. – fen1x Mar 07 '18 at 07:04

4 Answers4

46

As a thumb rule,

  • fr is greedy,
  • auto is shy.

When the browser renders your grid, first it calculates the necessary sizes of auto elements - they all get the minimum they can live with -, and then, after all other sizes are known, will it distribute the remaining space among fr cells. Distribution works the obvious way: sum the numbers, slice the pie, give everyone the amount requested. Like, you have 1fr, 1fr, 3fr and 2fr in the definitions - the numbers add up to 7, therefore the remaining space will be cut to 7 equal slices, and then everyone gets their share.

Splitting horizontal space

Remember this one and grids will be your best friends:

  • 1fr 1fr 1fr --------> 3 equal columns,
  • auto auto auto ----> 3 adaptive-width columns
dkellner
  • 8,726
  • 2
  • 49
  • 47
13

With CSS grid layout, the grid-template-rows value auto means:

The size of the rows is determined by the size of the container, and on the size of the content of the items in the row

and 1fr is a new flexible unit in GRID css. [Fr] is a fractional unit and 1fr is for 1 part of the available space.

so thats why your 3rd grid item is taking the remaining space and all remaining grid items are taking space according to its content

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
2

1fr means "take 1 fraction of the available space", and since there are no other element defined as fr, it also means "take all available space"

auto means "take whatever value the grid-auto-rows property has", which is by default auto as well, in that case meaning to be sized accordingly to content... but tracks can also get stretched if need be to match the size of contents on other columns

Facundo Corradini
  • 3,825
  • 9
  • 24
0

The fr unit represents a fraction of the leftover space in the grid container.

The leftover space is calculated after auto etc.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90