1

For the purposes of this question:

  • dimension = `height` or `width`.
  • FC = Flex-container.
  • FI = Flex-item.

Scenario 1

It seems, in both Chrome & Safari, when an img's 'container' is specified in 1 dimension, the other dimension'll try to become a size so that the 'container' retains the aspect ratio:

Here's a demonstration of this concept:

img {
 object-fit: cover;
 width: 200px;
}
<img src="https://cdn.britannica.com/20/154120-050-78DE15C0/lemur.jpg"/>

Scenario 2

However, Safari's ability to do this seems to break when the img is a FI.

#fc {
  display: flex;
}

img {
  object-fit: cover;
  width: 200px;
}

p {
 margin: 0;
}
<div id="fc">
 <img src="https://cdn.britannica.com/20/154120-050-78DE15C0/lemur.jpg">
 <p>
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis congue iaculis arcu sit amet aliquam. Quisque at auctor diam. Proin quis sem sed magna feugiat euismod non non ante. Sed at orci ac felis molestie pretium eget at nisl. Nam nec quam sit amet arcu molestie molestie. Maecenas sed eros non sem interdum egestas vitae eu sem. Quisque quis auctor sapien. Fusce risus odio, semper sed mattis quis, fringilla id arcu. Proin tempus finibus neque id ultrices. Nunc condimentum posuere ex, ut aliquam purus tempus quis.
 </p>
</div>

Presuming my understanding of what's gone on above is correct...

Is there a work-around? (i.e. a solution that works for Safari?). If not, I should be able to write up the original problem and perhaps you can suggest another way to achieve my goal (my goal's basically to have an img side-by-side to a block of text - and if the vw gets small, and the text height shoots down and gets longer, I want the imgs 'container''s height to similarly increase and match the text's height / height of the FC (when there's only 1 flex-line).

P.S. There's some other Safari troubleshooting I have to do on my website. It seems to concern imgs. If anyone was aware of any resource they could pass on that notes some of the ways in which we're confined to write code (in order to take into account Safari - particularly imgs) that'd be appreciated.

tonitone120
  • 1,920
  • 3
  • 8
  • 25
  • 2
    always make your image inside a div. An image as a flex item is a *nightmare*, you will always have headaches and different behavior across browsers – Temani Afif Apr 04 '21 at 19:06
  • 3
    safari is the new ie – BoltClock Apr 04 '21 at 19:25
  • This solved my problem. I don't know what you guys do but I actually do document things like this in my notes. For instance, I will have put something like '`img`s ought not to be FIs - they should be the child of FIs (this is due to browser inconsistencies when `img`s are FIs).' Do you guys remember/document/or is there a website you know to go to which explains these sorts of idiosyncrasies (in case you forget)? – tonitone120 Apr 05 '21 at 17:30
  • 2
    @TemaniAfif et al: Chrome, Firefox, and Safari teams worked together the last few months specifically on flex item images. (We'd heard loud and clear that cross-browser compatibility sucked.) We think we're almost finished. The WebKit fixes started rolling out in like STP 120, but some were released in STP 123 last week. If this example doesn't work in 123, OP, can you file a bug with minimal repro at https://bugs.webkit.org/ , reply here with a link, at-me, and I'll triage. But of course Safari only has major releases every 6 months so who knows how long until users get these fixes... – dgrogan Apr 07 '21 at 02:58
  • @dgrogan I recommend making your comment an answer as it can be helpful for anyone to know that there is a work in progress. I might also use it as duplicate target in case new similar questions are asked. – Temani Afif Apr 08 '21 at 11:05
  • @TemaniAfif I added a modified version. Is this roughly what you had in mind? – dgrogan Apr 08 '21 at 20:56
  • @dgrogan yes, that's perfect. Now we have a good answer that can cover/explain all the different cases. I might need to recheck some of my old answers on this subject (I remember did a lot of them where images are getting crazy) – Temani Afif Apr 08 '21 at 21:01

1 Answers1

1

Blink, Firefox, and WebKit contributors worked together the last few months on improving flex item images.

  • Chrome 90+ and Firefox 89+ have very few known incompatibilities (sometimes when images have % width; compare the code snippet below in Chrome vs Firefox).
  • Safari Tech Preview 123 made a lot of progress compared to the previous Safari stable release but still has a few more known issues.

If you have a flexbox with image items that renders differently across browsers, please let us know. Don't worry about duplicate issues, but shorten the demonstration cases if you can. If you think the buggy browser is

  • Safari: add a comment with example on this bug
  • Chrome or Firefox: add a comment with example on this issue

If your comment is not acknowledged after 2-3 days, respond with a link and I'll triage.

<div style="display: flex; width: 100px;">
  <img src="//placekitten.com/100" style="width:100%;">
  <div style="width:100px; background: orange"></div>
</div>
dgrogan
  • 2,587
  • 1
  • 17
  • 21
  • Some answers I just found: https://stackoverflow.com/q/54973109/8620333 / https://stackoverflow.com/q/55949254/8620333 / https://stackoverflow.com/a/58905047/8620333 . They are quite old so not sure if the behavior has changed since then – Temani Afif Apr 08 '21 at 21:09
  • I think we discovered another bug here: https://stackoverflow.com/a/67332926/8620333 you can probably have a look to confirm if it's really a bug or not (CSS grid by the way, not flexbox) – Temani Afif May 01 '21 at 10:47
  • @TemaniAfif Yeah, grid is still quite buggy. It is currently being rewritten in Blink-based browsers. – dgrogan May 01 '21 at 23:57