Image stretching in flexbox in Safari
Asked Answered
S

6

79

This is only an issue in Safari and looks like a Safari bug to me. Here is a fiddle with a simplified version of the issue.

When an image is in a nested flexbox element with a width set and height: auto it is being stretched... the auto height is not working. Does something extra need to be added for this to work in Safari?

.container {
  display: flex;
  flex-direction: column;
}

.container section:first-child {
  display: flex;
  margin-bottom: 25px;
}

.container img {
  width: 125px;
  height: auto;
}
<div class="container">
  <section>
    <img src="https://via.placeholder.com/250">
  </section>
  <section>
    <img src="https://via.placeholder.com/150">
  </section>
</div>

I expect the height of the image to automatically be adjusted to maintain aspect ratio. This works in all browsers except Safari. In Safari the image is stretched and the auto height does not work.

Selfassured answered 15/8, 2019 at 21:18 Comment(0)
T
148

It certainly appears to be a bug.

The default setting for the align-items property is stretch. Most major browsers handle this sensibly, stretching the image within the confines of the container.

For whatever reason, Safari stretches the image to its natural height, taking the container along for the ride.


flex-direction: row

To fix the problem, override the stretch default value with flex-start in the align-items property.

.container {
  display: flex;
  flex-direction: column;
}
.container section:first-child {
  display: flex;
  align-items: flex-start; /* new */
  margin-bottom: 25px;
}
.container img {
  width: 125px;
  height: auto;
}
<div class="container">
  <section>
    <img src="http://i.imgur.com/60PVLis.png">
  </section>
  <section>
    <img src="http://i.imgur.com/60PVLis.png">
  </section>
</div>

jsFiddle demo


flex-direction: column

Switching the direction of the flex container to column also fixes the problem. This works because align-items now applies to width and you've defined a width on the image.

If you reverse the image dimensions from

.container img {
   width: 125px;
   height: auto;
}

to

.container img {
   width: auto;
   height: 125px;
}

... you'll have the same problem in Safari as in flex-direction: row, and need align-items: flex-start for the correction.

.container {
  display: flex;
  flex-direction: column;
}

.container section:first-child {
  display: flex;
  /* align-items: flex-start; */
  margin-bottom: 25px;
}

.container img {
  width: auto;
  height: 125px;
}
<div class="container">
  <section>
    <img src="http://i.imgur.com/60PVLis.png">
  </section>
  <section>
    <img src="http://i.imgur.com/60PVLis.png">
  </section>
</div>

jsFiddle demo

Torpor answered 16/8, 2019 at 17:48 Comment(6)
Thanks for verifying and providing an explanation! Seems like Safari is quickly becoming the new IE.Selfassured
Does Safari track this issue? If yes, it would be great to add a link.Intercollegiate
Thank you for this! Does applying align-items: flex-start; permanently without caring about the flex-direction cause any problem if the flex-direction changes?Fructify
Sounds like the fix in WebKit has been merged in late 2020: bugs.webkit.org/show_bug.cgi?id=209983Parttime
@Parttime unfortunately this bug still persists as of 2021-05-21. Using align-self: flex-start is a nice way of getting around it.Trilobite
Thank you align-items: flex-start; worked for me :)Donella
P
15

Adding height: intrinsic; works for me to fix the stretched height in safari. Add it to the image itself. Not the wrapper. You will still need height: auto for the other browsers.

Pablopabon answered 28/9, 2020 at 19:43 Comment(3)
This helped me too, and it seems to be the least intrusive option.Calcium
Same here, this one's the ticket!Felspar
Whoa, this is a very weird behavior. Good luck finding that on your own 😨 Thanks @Kerry MurphyPhotothermic
I
6

For me neither of the solutions worked. I already had both flex-direction: column and align-items: center, although, in my case, I also had some other elements in the same flex container, alongside the image. Not sure if it had any impact.

What actually fixed the issue in my case was simply wrapping the image with a div:

<section>
    <div>
        <img src="https://via.placeholder.com/250">
    </div>
</section>
Imine answered 7/8, 2021 at 19:16 Comment(2)
As well as setting the img's width to 100%.Supernova
This solution fixed the problem for me either.Matron
K
5

See my demo for a working example, add flex-direction: column; to fix this issue.

.container {
  display: flex;
  flex-direction: column;
}

.container section:first-child {
  display: flex;
  flex-direction: column;
  margin-bottom: 25px;
}

.container img {
  width: 125px;
  height: auto;
}
<div class="container">
  <section>
    <img src="https://via.placeholder.com/250">
  </section>
  <section>
    <img src="https://via.placeholder.com/150">
  </section>
</div>
Kimberliekimberlin answered 16/8, 2019 at 6:33 Comment(2)
I noticed adding flex-direction: column; fixes the issue as well but to me this shouldn't be needed. Could you explain why this needed? Or is this just a workaround for a Safari bug?Selfassured
For Bootstrap 4 you could add class="flex-column flex-md-row" to restore it to horizontal on devices larger than a phone, but still get the fix in for iphones..Canebrake
C
1

if parent <img/> tag has display:flex; add align-items: center;

<div style="display:flex;align-items: center;">
    <img "img.jpg"/>
</div>
Cerecloth answered 8/12, 2020 at 19:25 Comment(0)
E
1

For anyone looking over this post in the future: I had this problem with a fixed height flex-direction:row flexbox and images inside of flex-children with height:100%. The highest-voted solution wasn't enough to fix it.

For whatever reason, what ultimately fixed it in my case was adding display:block directly to the images.

Eldoneldora answered 18/3, 2021 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.