Flexboxes make it easy to make layouts that grow and shrink intelligently based on available space. I was using this feature to draw two images that each take up half of the width of the screen. In Firefox, the images render as expected and maintain their aspect ratio but in Chrome the images are horizontally squashed to 50% each and left at their full height vertically.
Here's a demo of what I'm talking about:
.flexbox {
width: 100%;
display: flex;
}
img {
width: 50%;
}
<div class="flexbox">
<img src="http://res1.windows.microsoft.com/resbox/en/windows/main/2b03b0c4-85f7-4c28-9c60-1c7af2a62fa8_5.jpg" />
<img src="http://res1.windows.microsoft.com/resbox/en/windows/main/b89eff70-398d-4b1b-8806-02458a185c5a_5.jpg" />
</div>
I've read in an answer to a similar question that this is a Chrome bug. How can I cleanly work around it?
Thanks
Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.
The ratio 9/16 is ease to change, no need to predefined 100:56.25 or 100:75 . If you want to ensure height first, you should switch width and height, e.g. height:100vh;width: calc(100vh * 9 / 16) for 9:16 portrait.
Sometimes, it is required to fit an image into a certain given dimension. We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container.
If your image doesn't fit the layout, you can resize it in the HTML. One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.
The items in a flexbox will stretch their size by default.
Seeing you want them to not stretch on the cross-axis (vertical), you can use the align-items
on the container, to change the default behaviour.
Fiddle: http://jsfiddle.net/g1vLff23/
CSS
.flexbox {
width: 100%;
display: flex;
align-items: flex-start;
}
img {
width: 50%;
}
Apply object-fit: scale-down;
to the images. Yes, adding height: auto;
does not do the trick, unfortunately. Similar issue solved here: max-width of img inside flexbox doesn't preserve aspect ratio
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