The Simple Solution Using CSSBy setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.
In that situation we can use CSS max-width or width to fit the image. Use max-width: 100% to limit the size but allow smaller image sizes, use width: 100% to always scale the image to fit the parent container width.
img {max-width:100%;}
is one way of doing this. Just add it to your CSS code.
http://jsfiddle.net/89dtxt6s/
I came here looking for an answer to my distorted images. Not totally sure about what the op is looking for above, but I found that adding in align-items: center
would solve it for me. Reading the docs, it makes sense to override this if you are flexing images directly, since align-items: stretch
is the default. Another solution is to wrap your images with a div first.
.myFlexedImage {
display: flex;
flex-flow: row nowrap;
align-items: center;
}
You might want to try the very new and simple CSS3 feature:
img {
object-fit: contain;
}
It preserves the picture ratio (as when you use the background-picture trick), and in my case worked nicely for the same issue.
Be careful though, it is not supported by IE (see support details here).
I suggest looking into background-size
options to adjust the image size.
Instead of having the image in the page if you have it set as a background image you can set:
background-size: contain
or
background-size: cover
These options take into account both the height and width when scaling the image. This will work in IE9 and all other recent browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With