Why does flexbox stretch my image rather than retaining aspect ratio?

HtmlCssFlexbox

Html Problem Overview


Flexbox has this behaviour where it stretches images to their natural height. In other words, if I have a flexbox container with a child image, and I resize the width of that image, the height doesn't resize at all and the image gets stretched.

div {
  display: flex; 
  flex-wrap: wrap;
}

img {
  width: 50%
}

<div>
    <img src="https://i.imgur.com/KAthy7g.jpg" >
    <h1>Heading</h1>
    <p>Paragraph</p>
</div>

What causes this?

Html Solutions


Solution 1 - Html

It is stretching because align-self default value is stretch. Set align-self to center.

align-self: center;

See documentation here: align-self

Solution 2 - Html

The key attribute is align-self: center:

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

img {
  max-width: 100%;
}

img.align-self {
  align-self: center;
}

<div class="container">
    <p>Without align-self:</p>
    <img src="http://i.imgur.com/NFBYJ3hs.jpg" />
    <p>With align-self:</p>
    <img class="align-self" src="http://i.imgur.com/NFBYJ3hs.jpg" />
</div>

Solution 3 - Html

I faced the same issue with a Foundation menu. align-self: center; didn't work for me.

My solution was to wrap the image with a <div style="display: inline-table;">...</div>

Solution 4 - Html

Adding margin to align images:

Since we wanted the image to be left-aligned, we added:

img {
  margin-right: auto;
}

Similarly for image to be right-aligned, we can add margin-right: auto;. The snippet shows a demo for both types of alignment.

Good Luck...

div {
  display:flex; 
  flex-direction:column;
  border: 2px black solid;
}

h1 {
  text-align: center;
}
hr {
  border: 1px black solid;
  width: 100%
}
img.one {
  margin-right: auto;
}

img.two {
  margin-left: auto;
}

<div>
  <h1>Flex Box</h1>
  
  <hr />
  
  <img src="https://via.placeholder.com/80x80" class="one" 
  />
  
  
  <img src="https://via.placeholder.com/80x80" class="two" 
  />
  
  <hr />
</div>

Solution 5 - Html

I had a similar issue while making my navigation bar, but none of the above worked for me.

My solution was adding height: 100% for the image.

If you're aligning the items horizontally, add width: 100% instead.

EDIT: Chrome seems to add this value by default now, but you'll need to add this for compatibility.

Solution 6 - Html

It is stretching because align-self default value is stretch. there is two solution for this case :

  1. set img align-self : center OR
  2. set parent align-items : center
    img {

align-self: center } OR

.parent {
align-items: center
}

Solution 7 - Html

Use one of the CSS settings below: contain or cover is most popular

.my-img {
  object-fit: contain;
}

or

.my-img {
  object-fit: cover;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsjbuysseView Question on Stackoverflow
Solution 1 - Htmlpradeep1991singhView Answer on Stackoverflow
Solution 2 - HtmlDorianView Answer on Stackoverflow
Solution 3 - HtmlisapirView Answer on Stackoverflow
Solution 4 - HtmlAakashView Answer on Stackoverflow
Solution 5 - HtmlJongwoo LeeView Answer on Stackoverflow
Solution 6 - Htmlmohamed aliView Answer on Stackoverflow
Solution 7 - HtmlXinView Answer on Stackoverflow