To understand this you need to understand a tricky behavior related to 1fr
. 1fr
is equivalent to minmax(auto,1fr)
as explained here: Why does minmax(0, 1fr) work for long elements while 1fr doesn't? which means "the track cannot be smaller than the size of the grid item"
You are wondering how this answer your question so Let's start with an example to illustrate the issue.
.gizim{
display: grid;
grid-template-columns: 0.3fr 0.4fr 0.3fr;
margin: 5px auto;
grid-gap: 1em;
width: 400px;
border:1px solid;
}
.gizim div {
border: 1px solid red;
min-height:20px;
}
.alt {
grid-template-columns: minmax(0,0.3fr) minmax(0,0.4fr) minmax(0,0.3fr);
}
<div class="gizim">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<div class="gizim">
<div>1</div>
<div>2</div>
<div>333333333333333</div>
</div>
<div class="gizim alt">
<div>1</div>
<div>2</div>
<div>333333333333333</div>
</div>
The first case will behave like you want because all your xfr
are bigger than auto
. Now let's understand the second case.
First we need to identify the "leftover" (https://www.w3.org/TR/css3-grid-layout/#leftover-space). It's the space left if we first consider the column sized to their default content. Something like below:
.gizim{
display: grid;
grid-template-columns: auto auto auto;
justify-content:start;
margin: 5px auto;
grid-gap: 1em;
width: 400px;
border:1px solid;
}
.gizim div {
outline: 1px solid red;
min-height:20px;
}
<div class="gizim">
<div>1</div>
<div>2</div>
<div>333333333333333</div>
</div>
You can see the "leftover" space at the right. If we do some calculation we can find 400px - 2x16px(gap) - 120px(width of 3) = 248px
so 1fr = 248px
*. This will give us the logical result of:
0.3fr = 74.4px
0.4fr = 99.2px
It's important to note that the third element has an initial width bigger than .3fr
so he will keep that width while the other will increase in width following the above calculation:
.gizim{
display: grid;
grid-template-columns: 0.3fr 0.4fr 0.3fr;
justify-content:start;
margin: 5px auto;
grid-gap: 1em;
width: 400px;
border:1px solid;
}
.gizim div {
outline: 1px solid red;
min-height:20px;
}
<div class="gizim">
<div>1</div>
<div>2</div>
<div>333333333333333</div>
</div>
*In reality the calculation is more complex to explain in detail but the important part here is that the content play a role in defining how the fr
will behave but if we use minmax(0,xfr)
you will get the expected result because we are no more considering the content.
Such issue doesn't occur when using more than 1fr
because we are going to use more than the "leftover" space so we will for sure fill all the columns but still the content play a role:
.gizim{
display: grid;
grid-template-columns: 3fr 4fr 3fr;
margin: 5px auto;
grid-gap: 1em;
width: 400px;
border:1px solid;
}
.gizim div {
border: 1px solid red;
min-height:20px;
}
.alt {
grid-template-columns: minmax(0,3fr) minmax(0,4fr) minmax(0,3fr);
}
<div class="gizim">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<div class="gizim">
<div>1</div>
<div>2</div>
<div>333333333333333</div>
</div>
<div class="gizim alt">
<div>1</div>
<div>2</div>
<div>333333333333333</div>
</div>