简体   繁体   English

Chrome浏览器无法按比例缩放图像,使其低于第一个渲染的尺寸

[英]Chrome doesn't scale images proportionally below first rendered size

I'm trying to scale images to the height of their parent which has a percentage height of its parent. 我正在尝试将图像缩放到其父级的高度,该父级的高度为其父级的百分比。 This works as expected except in Chrome where the image won't scale its width proportionally once the height is reduced below the size at which it was first rendered. 这项工作符合预期,但在Chrome中,一旦高度降低到首次渲染时的大小,图像就不会按比例缩放其宽度。 Any ideas on how to fix this? 有想法该怎么解决这个吗?

<div>
    <img src="http://placehold.it/100x100" alt="">
</div>

and the css: 和CSS:

html, body {
    height: 100%;
    margin: 0;
}

div {
    height: 70%;
    background-color: red;
}

img {
    height: 100%;
    width: auto;
}

JSFiddle 的jsfiddle

Removing the width property fixes this: 删除width属性可解决此问题:

img {
    height: 100%;
}

I'm not sure why this happens, but I'm guessing that making the width always at auto would fallback to the original width when the image is scaled down (this doesn't happen in most cases I've tried, but a certain combination might trigger it to happen that way). 我不确定为什么会发生这种情况,但是我猜测当图像按比例缩小时,始终将宽度设置为自动会退回到原始宽度(在我尝试过的大多数情况下不会发生这种情况,但是组合可能会触发它以这种方式发生)。 Not sure if it's by design or not, but I'll go ahead and try to report this somewhere. 不知道这是否是设计使然,但我将继续尝试在某处进行报告。

Fiddle 小提琴

Try using display: block; 尝试使用display:block; to make Chrome scale the image below the rendering-size: 使Chrome将图片缩放到渲染尺寸以下:

    display: block; 
min-height: 100%; 
height: 100%; 
width: auto;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM