Repository navigation
[css-grid] Intrinsic size of grid containers #2303
Description
The spec text is very clear about this and has been around for a long time:
The max-content size (min-content size) of a grid container
is the sum of the grid container's track sizes (including gutters)
in the appropriate axis, when the grid is sized
under a max-content constraint (min-content constraint).
But it seems not all the browsers are doing it, or at least not all
are doing it properly.
Firefox is following the spec, and it runs the track sizing algorithm twice
(one under min-content constraint and another under max-content constraint)
to compute the min|max-content widths of the grid container.
However Blink and WebKit follow a different approach.
In this case they just run the track sizing algorithm once
with an available space of zero and use the sum of base sizes
as min-content size and the sum of the growth limits as max-content size.
The reason why these browsers do it that way is because
this was suggested back in 2013 by Julien Chaffraix in www-style
and never modified later.
Regarding Edge I don't know what's their implementation, but I'm finding
the same issues than in Blink and WebKit. So I guess Edge is either
not following the spec or not implementing it properly.
Let's go to the examples and why I'm bringing up this issue.
The key part of the track sizing algorithm that is affected by this
is when we resolve intrinsic track sizes
(for both non-spanning and spanning items we have a similar text):
For auto minimums:
If the track has an auto min track sizing function and the grid container
is being sized under a min/max-content constraint,
set the track's base size to the maximum
of its items' min/max-content contributions, respectively.Otherwise, set its base size to the maximum
of its items' min-size contributions.
Imagine the following example:
<div style="width: 20px; border: thick dotted red;">
<div style="display: inline-grid; border: solid thick;">
<div style="min-width: 0px; background: lime;">Lorem ipsum.</div>
</div>
</div>The intrinsic sizes should be:
- min-content size: The width of Lorem, let's say
50px. - max-content size: The width of Lorem ipsum., let's say
100px.
As the grid container is inline-grid, its sized as fit-content:
min(max-content size, max(min-content size, stretch-fit size)).
In this example, min(100px, max(50px, 20px)) = 50px.
Note: This also matches what happens in a similar case using inline-block
instead of inline-grid.
In Blink and WebKit we always go through the Otherwise paragraph,
so the min-content size is 0px and the final width is 20px.
The same result happens on Edge, though I don't know how is that implemented.
In any case it seems quite clear that this is a bug in Blink, WebKit and Edge
and their behavior should be modified.
Note that to fix this, we'll need to follow a similar approach than Firefox,
which means that the track sizing algorithm will be run 3 times:
- 2 times to calculate the min and max-content sizes
(one under min-content constraint and another under max-content constraint). - And one extra time during layout considering no constraints.
This is not all, now let's go to a different example:
<div style="display: inline-grid; grid-template-columns: minmax(auto, 0px); border: solid thick;">
<div style="background: lime;">Lorem ipsum.</div>
</div>To calculate the min-content size, all the browsers seem to apply the clamping
described in Automatic Minimum Size of Grid Items section
of the spec. So the min-content size is 0px.
However to calculate the max-content size, Firefox uses
the max-content contribution as requested by the spec, that's 100px.
This makes that the intrinsic size of the grid container is 100px.
However during layout the track sizing algorithm is run again,
and as during layout it doesn't know if it's under a min-content
or max-content constraint, it goes through the Otherwise sentence
and apply the clamping, so the track has 0px width.
This causes a weird effect as the track is 0px
but the grid container is 100px.
In Blink and WebKit the result is better, the grid container is 0px
as we always go through the Otherwise sentence and apply the clamping.
Edge has the same output too.
In a Firefox bug
@MatsPalmgren suggests that we apply the automatic minimum size clamping
when we are under a min-content constraint, max-content constraint
or no constraint.
That would make Firefox has the same output than the other browsers
in this last example.
It'd be nice to get the feedback from @atanassov about this issue,
as we don't know how the intrinsic size is being calculated in Edge.
Check the following codepen to see the examples live and play with them:
https://codepen.io/mrego/pen/xYdvbZ
Activity
fantasai commentedon Feb 14, 2018
The quoted spec text was added in this thread, here: https://lists.w3.org/Archives/Public/www-style/2016Jan/0163.html
The principle it's trying (and maybe failing?) to uphold is this one:
The idea is that if all our sizes are auto and we degenerate down to one grid track/one item, a shirnkwrapped (e.g. floated or abspos'd) grid should behave similar to a shrinkwrapped block or flexbox.
Bug 1427608 - [css-grid] Fix span=1 'auto' min-sizing for intrinsic s…
MatsPalmgren commentedon Mar 3, 2018
FYI, this is now fixed in the latest Firefox Nightly.
The algorithm we use in "For auto minimums" is:
clamped: means shrink the margin box size to fit a fixed track max-sizing function (if there is one), without making the content-box size negative (as usual per Grid §6.6)
With this definition of "behave as auto".
(ditto for
'min-height'/'height')So, the trick is to always clamp the size in "For auto minimums" (when AMS and clamping applies). This gives the grid container the desired intrinsic size. Then, in layout (no sizing constraint), we'll return a clamped min-content contribution there, but since the grid container now has a definite size (the intrinsic size), we'll grow the track in the "Maximize Tracks" step up to the desired size.
As far as I can tell, this gives the same layout as in Chrome for all the tests I looked at.
I found only one difference, but I tend to think that's simply a somewhat unrelated bug in Chrome.
[css-grid-1] Clamp 'auto' contributions by a fixed max track size eve…
fantasai commentedon Nov 1, 2018
OK, I've made the spec changes for non-spanning items. Haven't figured out the correct edits for spanning items yet.
fantasai commentedon Nov 1, 2018
[ For the archives, here's a parallel thread between me and Mats from around the time this issue was filed. I think my goal here is to make sure I catch all of the relevant edits brought up in both discussions on this issue. ]
[css-grid-1] Clamp 'auto' contributions by a fixed max track size eve…
fantasai commentedon Feb 7, 2019
Proposed edits for spanning items (accounting also for #3565):
Loirooriol commentedon Feb 7, 2019
And the fixed gutters between all the spanned tracks? This concept is also used in https://drafts.csswg.org/css-grid/#min-size-auto, you could name it and reference it.
Maybe the limited min-/max-content contribution should be fully defined in its own subsection (together with the minimum contribution, I guess). Providing a simpler definition in the place where it's defined, and then adding amendments in references to it is less clear. Because not all references will be next to the amendments, and following the link won't provide the full definition.
Loirooriol commentedon Feb 7, 2019
No need to mention
auto, because it behaves asmax-content. And the condition should excludemax-contentcoming fromfit-content.The
fit-contentargument should be summed here.I'm not convinced this is the right approach. Imagine you have an item with a min-content contribution of 25px spanning a track with
minmax(auto,min-content)and another withminmax(auto,50px). Then the limit will bemax(25px,50px) = 50px, but in fact it should be25px+50px = 75px.I think it should be more like:
fit-content()track, resolve the argument as an absolute length and increase x by that.min-content, increase x by the item's min-content contribution.max-content), increase x by the item's max-content contribution (or infinity since it will be no-op).Loirooriol commentedon Feb 13, 2019
OK, given the way space is distributed, a maximum as you said may be better.
However, it still doesn't seem completely right. The idea is that, when sizing under a max-content constraint, items shouldn't contribute more space than what they will end up using. But imagine you have a single grid item with a minimum contribution of 20px, a min-content contribution of 25px, and a max-content contribution of 100px. It spans two columns sized with
minmax(auto,min-content) minmax(auto,50px).When sizing the container under a max-content constraint, the limited max-content contribution will be 50px (100px clamped between 20px and 50px). This will be distributed equally, so both columns will end up with a base size of 25px. Then the minimum contribution, 20px, is distributed for growth limits. The 2nd column already has a growth limit of 50px, so it will remain that way, but the first column will change its growth limit from infinity to the base size, 25px. Then the 2nd column will be maximized and the grid container will be 75px wide.
The you do layout for real. This time the
autominimums only distribute the minimum contribution, 20px. So the base size of each column becomes 10px. 20px are distributed again for the growth limits, the first one becomes 10px and the 2nd one remains 50px. So this time the columns will sum 60px. This is smaller than the grid container.IMO the sizing algorithm seems too aggressive reducing the growth limit when it was infinity and becomes finite. If there is free space, the 1st column should probably be allowed to grow until it reaches 25px (the min-content contribution, as dictated by the max track sizing function). This can be an unrelated issue, though.
10 remaining items
Bug 1427608 - [css-grid] Fix span=1 'auto' min-sizing for intrinsic s…
MatsPalmgren commentedon Nov 4, 2019
@fantasai Can you explain what the goal of these spec changes are exactly? Otherwise, I can't tell whether the spec achieves that goal or not.
fantasai commentedon Nov 5, 2019
@MatsPalmgren The goal was to resolve all the issues discussed in your thread, linked above. I believe some amount of your confusion was resolved by @Loirooriol's comments earlier today... so let me know if you still need an explanation, because to be honest it's really difficult for me to page this issue back into my head, but if you need me to I will try harder tomorrow.
Wrt the the sample testcase you gave in your comments on 3b26eeb AFAICT it will render as you expect, because that changeset floors the item's contribution to the track size by its “minimum contribution”. In the case of auto, this is the min-content size clamped by the max track sizing function to zero. But in the other cases, it's equivalent to its min-content contribution, where the preferred size ('width') or explicit minimum size ('min-width') you've given controls that size (and the AWS is not invoked).
MatsPalmgren commentedon Nov 5, 2019
OK, no worries. I'll take a look at the spec anyway...
One thing that immediately stands out is that non-spanning/spanning items are treated differently for
autominimum tracks. The "floored by its minimum contribution" part is missing in the spanning case. Is that intentional? It seems a bit odd to me that aspan 1item in aminmax(auto, 0px)track would be treated differently than if the same item hadspan 2inrepeat(2, minmax(auto, 0px)).(Also, an unrelated nit: in the non-spanning case you say "Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored at zero.", but in the spanning case you say " to accommodate these items’ minimum contributions". The "floored at zero" is missing in the latter case. Fwiw, it's a bit silly that we need to handle this here - maybe css-sizing could floor it so that all size contributions are non-negative?)
Loirooriol commentedon Nov 5, 2019
For the spanning case, the spec says
Where the limited min-content contributions is
So the "floored by its minimum contribution" seems included.
The problem that I see is that the spec says the "limited min-content contribution" should be used even when sizing under a max-content constraint. Shouldn't it use the "limited max-content contribution" in that case?
I think it's not important, because https://drafts.csswg.org/css-grid/#extra-space says
So currently we use
max(0, contribution - somethingNonNegative), changing tomax(0, max(0, contribution) - somethingNonNegative)should have no effect.But contributions can actually be negative, right? Like if you add a box with a very negative margin, the containing block can shrink.
MatsPalmgren commentedon Nov 5, 2019
Good points.
Note that Step 1 is for
auto/min-content/max-contentmin-sizing functions. Step 3 (additionally) handles the "sized under a max-content constraint" case forauto/max-contentmin-sizing functions by distributing "limited max-content contributions" there, which seems reasonable. We don't want a max-content contribution for a min-content min-sizing track.Loirooriol commentedon Nov 6, 2019
Ah you are right, I already suspected I was missing something since when that text was written it looked good to me.
fantasai commentedon Jul 22, 2020
Closing per @Loirooriol's assessment that the spec is correct, though implementations still need fixing.
Bug 1427608 - [css-grid] Fix span=1 'auto' min-sizing for intrinsic s…