[css-grid] Stretching image grid items in both dimensions #523
|
We were discussing that yesterday at TPAC (@fantasai, @jensimmons and me). |
|
Well, actually manually setting the width won't cause the aspect ratio to be observed: it just sets the width in that dimension, and since the initial value of align-self is Squishing images seems a little odd, but as a starting point it's useful: the |
|
@mrego I don't think that solution works, because setting width:100% My proposal doesn't have that problem. |
|
@fantasai, I don't think object-fit:contain works either since |
|
Cover example, image proportionally sized and clipped to fit:
Contain example, margin/border/padding fitted to grid slot and image proportionally sized into it:
Contain example, margin/border/padding fitted to proportionally scaled-down image:
Contain example with future syntax, margin/border/padding fitted to proportionally scaled-down image:
|
As you say, this leads to the image being clipped and is thus
This leads to the image overflowing when the window is very wide
Suggesting magic future tech to solve this problem isn't serious.
I've made a demo of I see the following problems:
My proposal doesn't have any of the above problems and it provides |
|
To be clear, what I'm proposing is that spec says something like this
|
|
It seems that the proposed text is a bit underdefined in terms of how min and max sizes interact with the "ratio-preserving way". Also I'm not sure that the current definition of min sizes in grid actually allow you to preserve the aspect ratio if the image is too big in both axes and does not have a width or height explicitly specified on the image. |
There are some pros and cons with that, but I don't have a preference. |
Presumably using the rules in CSS2 section 10.4 that apply to this case. |
|
Just discussed in today's teleconference (I'll try to link minutes later). The conclusion is that we can change the mapping of |
|
OK, so |
|
I really like the proposed solution to make Could you please clarify which behavior you want when the *-self value I think it should fill the grid area in the axis that has |
@fantasai / @tabatkins, I think this ^ is a question for you. Mats' suggestion here makes sense to me, FWIW (and I can't come up with anything that's more sensible). |
|
Checked in an attempt at fixing this per above discussion. :) Please let me know if anything is askew. I took Mats's suggestion for handling Wrt basing on |
|
Agenda+ for certification of edits and the details wrt |
|
Oh, just wanted to check that we're all understanding that |
|
No, I would summarize what we have implemented as:
IOW, the intrinsic ratio is preserved if at least one of the values is It gets more complicated when one or both axis also has min-/max-size constraints. I think we're following the CSS2 rules[1] there -- we're re-using the code that already implements that. The stretching occurs before applying those rules. Also, when the track max-sizing functions are definite, clamping the item to fit the grid area (per §6.6 in CSS Grid https://drafts.csswg.org/css-grid/#min-size-auto) also affects the above rules (the "may overflow..." above should not be allowed when clamping in that axis IMO, but let's sort out the clamping effect on this in #767) Here's a couple of testcases to illustrate our stretching behavior (I intentionally Testcase for shrinking an image to fit a smaller grid area: (You need a Nightly build for these tests to display correctly: https://nightly.mozilla.org/ ) |
|
OK, so, what I want to point out is that from an authoring point of view, the intrinsic aspect ratio should be preserved unless there's specific instructions not to. Maintaining the intrinsic aspect ratio trumps preserving the intrinsic size in any single dimension. Always. Changing a dimension effectively modifies the user-perceived "intrinsic size" in the other. Internally, we can define terms however we want, but I'm pretty sure that as far as the author is concerned, anywhere that we are failing to do this is an error. An alignment value of To address your feedback that the default behavior should be to preserve the aspect ratio, we decided to add an independent behavior for p.s. Fwiw, I'm a little concerned that the new |
|
What you are suggesting is that That seems a lot less useful to authors, IMHO. As you can see in the testcase: I think your suggestion is a big mistake and I would like to hear what others think about this before changing our implementation. Web developers in particular. |
|
I agree with @MatsPalmgren . In any case, we should not rely on |
|
I think this would be a regular use by developers, centering and preserving the aspect ratio(demo). Equivalent to: grid > img {
width: 100%;
max-height: 100%;
object-fit: cover; // or contain
}And I was surprised to find that Chrome was not in the same height(line-height), |
Yes, this makes sense to me having just re-read this discussion. As an author you don't expect the ratio of an image to be changed unless you specifically request that to happen. I can see situations occurring where in development test data doesn't highlight the ratio destroying behaviour. However once the site is live and real data is added via a CMS etc. images start to look weirdly stretched. By making the default ratio preserving then in that scenario images might overflow a box or have unwanted gaps but I think that is closer to how authors expect things to behave. |
|
Just trying to clarify the issue, the part in which there is disagreement is in the difference between By default, everything is But for
Note that if you use Also see the difference in the example above, using the current implementation by Firefox that follows option 2): |
|
I think I support @fantasai's proposal, as I cannot think of any situation where I would want any default behavior to alter the dimensions of an image, unless I explicitly required this, using stretch on both the x and y axis. As a side note, I would like the ability to choose a focal point on an image that would center the image if the viewport in changed in size in any way smaller than the original image, while preserving the image dimensions and cropping any aspect of the image that falls outside of the viewport. Not sure I'm explaining that too well, though. thegrid.io adversities that it does this, although i have not used it in practice yet. But anyway, a +1 for @fantasai's suggestion :-) |
It seems to me you have misunderstood this issue. The default value is |
|
As a reminder, the current CSSWG resolution is supporting my view on this:
so that is what we will ship in Gecko, unless there is a new CSSWG resolution on the matter that says otherwise. |
|
The resolution was based on a discussion of the value specified in both dimensions. Prior to the introduction of 'normal' as distinct from 'stretch', it seemed to me quite obvious that specifying "align-axis: center" would not cause distortion, but cause the vertical axis to trigger "shrink to fit" behavior (which preserves the aspect ratio) and use the extra space for centering. I don't see why introducing special behavior for 'normal' -- special behavior whose sole purpose was to solve the default distortion brought on by the 'stretch stretch' combined default, should cause previously-existing combinations to change behavior. |
|
I also want to remind everyone that the purpose of the alignment properties is to manage alignment, not sizing. Stretch is a bit of an anomaly, but it does fit within the description of "specify what to do with the extra space after sizing". |
Fair enough, do you mind bringing it up again in a CSSWG meeting to discuss whether all values involving Fwiw, note that Firefox has always implemented ratio-destroying resizing for |
|
Sure, I'll bring that up. It didn't even occur to me that the "shrink-to-fit" sizing of the non-stretchy alignment values would be interpreted as aspect-ratio-destroying. (The only reason that 'stretch stretch' distorts the image is because you specifically requested a size: in CSS we otherwise always preserve the aspect ratio on auto-sized images, if we can.) Agenda+ to decide what happens if |
|
I like Mats proposal better. The default is normal. I switch to stretch in the dimension where the image does not yet fill the space, and I get the result of breaking aspect ratio and having it stretch. If I want to 'fix' the aspect ratio, I can start telling the browser what I want it to do (likely with |
|
removing agenda+ because we discussed this on the last call and decided to keep the conversation going here. |
|
Ok, so Tab and I discussed this today and came up with the following conclusions. Things to consider:
Reasoning:
Proposal:
Related improvements that need to be made:
|
|
Is Chrome committed to making |
|
@MatsPalmgren |
|
Well, I wanted to know to be able to properly critique your current proposal to solve this issue. It's still not clear to me what the suggested "workaround" is to get the desired behavior. I don't see how |
|
So, to recap, the problem at hand is that stretching images in a ratio-preserving way would fill the grid area in only one axis but (likely) not the other and that an author might want to align (start/center/end) the image in the non-filled axis. IMHO, this seems like an edge case and a fairly minor problem. @fantasai's proposal to solve this problem is to not stretch images at all by default. This is a very drastic and invasive change to solve such a minor problem. I think that stretching all grid items to fill their grid area by default is a fundamental part of Grid layout and any proposed solution must preserve that. To single out images to not do that is inconsistent and unexpected. Also, I think your reasoning is flawed:
Well, there is a I agree with:
Yes, this is the problem at hand. Let's solve that in a reasonable way without destroying Grid layout completely. Here's my proposal to solve the align-the-non-filled-axis problem: The drawback with A is that it is two keywords and thus can't be used with the I think this solution is much more in proportion to this relatively minor problem. So, I'd like the CSSWG to reconsider my proposal that image grid items should stretch in a ratio-preserving way by default. Authors will expect all grid items to stretch by default, and as you agree, images are expected to preserve the ratio by default. The remaining alignment problem can be solved by adding some minor CSS Box Alignment feature as I suggested above (there are likely other Align solutions too). |
|
I also believe that this proposal has some issues:
|
|
@mrego Regarding Flexbox - see my original comment at the top. The CSSWG actually agreed at the time that it's undesirable to stretch images without preserving ratio, but there were some other issues in Flexbox that made it impossible to implement that. Those issues don't apply to Grid, which is why I think we can and should implement the optimal behavior here. |
|
@fantasai I have already thoroughly debunked the myth that |
|
The CSSWG discussed this issue again, and stands by the conclusion in #523 (comment) This ensures that the alignment properties do not devolve further into sizing controls, and that behavior in grid is consistent with behavior in block layout, etc. We will however add a @MatsPalmgren Wrt #523 (comment) I listed |
|
|
triple-underscore |
d0e4c06
|
That's not what the CSWG actually decided according to the IRC log at https://log.csswg.org/irc.w3.org/css/2017-01-13/#e761792
@fantasai's proposal above says "things with an aspect ratio" should use |
|
We actually did mean "things with an aspect ratio" - this was very clear from the discussion in-person, but didn't make it into the minutes very well. (Not helped by our continued use of "replaced element" as the term.) So we'll need the WG to amend the resolution to clarify that it was about things with an aspect ratio. Additionally, we then need to verify what behavior we want for replaced elements without aspect ratios. In particular:
|
|
Fwiw, I agree that an intrinsic size should also make |
|
Adding myself to the conversation since it seems we might change the spec again. Also, here is a test case:
|
|
Also variant https://jsfiddle.net/1fd948nz/1/ |







I'm wondering about how we should stretch grid items that has
an intrinsic ratio, such as images. The Grid spec just refers to
css-align which says:
https://drafts.csswg.org/css-align-3/#justify-self-property
"the stretch keyword sets the box’s used size to the length necessary
to make its outer size as close to filling the alignment container
as possible while still respecting the constraints imposed by
min-height/min-width/max-height/max-width."
which I think means that having 'stretch' in both dimensions
will resize the image to fill the grid area without respecting
the image aspect ratio. I think this is a rather unfortunate
default behavior for Grid.
Some background; this topic was discussed for flex items here:
https://lists.w3.org/Archives/Public/www-style/2012Oct/0781.html
where everyone seems to agree that respecting the ratio is desirable,
but for various flex layout specific reasons, this could not be
achieved and it was decided to ignore the ratio (IIUC).
As far as I can tell, those reasons do not apply to Grid, so I see
no reason why we can't respect the ratio when stretching grid items
in both dimensions.
I think web authors generally prefer to preserve aspect ratios,
so that's what I think we should do for grid items.