[英]chrome doesn't respect image aspect ratio in flexbox
Flexboxes make it easy to make layouts that grow and shrink intelligently based on available space. Flexbox可以轻松地根据可用空间进行智能增长和缩小的布局。 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.
在Firefox中,图像按预期呈现并保持其纵横比,但在Chrome中,图像被水平压扁至50%,并垂直留在其全高。
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. 我已经阅读了类似问题的答案 ,这是一个Chrome错误。 How can I cleanly work around it?
我怎样才能干净利落地工作呢?
Thanks 谢谢
The items in a flexbox will stretch their size by default. flexbox中的项目默认会拉伸它们的大小。 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. 看到您希望它们不在横轴(垂直)上伸展,您可以使用容器上的
align-items
来更改默认行为。
Fiddle: http://jsfiddle.net/g1vLff23/ 小提琴: http : //jsfiddle.net/g1vLff23/
CSS CSS
.flexbox {
width: 100%;
display: flex;
align-items: flex-start;
}
img {
width: 50%;
}
Apply object-fit: scale-down;
适用
object-fit: scale-down;
to the images. 到图像。 Yes, adding
height: auto;
是的,添加
height: auto;
does not do the trick, unfortunately. 不幸的是,并没有做到这一点。 Similar issue solved here: max-width of img inside flexbox doesn't preserve aspect ratio
类似的问题在这里得到解决: flexbox内的img的max-width不保持纵横比
You can solve this by adding the height
property to your img
rule and giving it a value of auto
: 您可以通过将
height
属性添加到img
规则并为其赋值auto
来解决此问题:
img{
height:auto;
width:50%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.