【问题标题】:Chrome doesn't scale images proportionally below first rendered sizeChrome 不会按比例缩放图像低于第一次渲染的大小
【发布时间】:2013-06-25 10:40:10
【问题描述】:

我正在尝试将图像缩放到其父级的高度,该高度具有其父级的百分比高度。这可以按预期工作,除非在 Chrome 中,一旦高度减小到低于首次渲染的大小,图像就不会按比例缩放其宽度。有关如何解决此问题的任何想法?

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

和css:

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

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

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

JSFiddle

【问题讨论】:

    标签: html css google-chrome


    【解决方案1】:

    删除 width 属性可以解决此问题:

    img {
        height: 100%;
    }
    

    我不确定为什么会发生这种情况,但我猜测当图像按比例缩小时,将宽度始终设置为 auto 会回退到原始宽度(在我尝试过的大多数情况下都不会发生这种情况,但是某种组合可能会触发它以这种方式发生)。不确定这是否是设计使然,但我会继续尝试在某处报告此问题。

    Fiddle

    【讨论】:

    • 你的小提琴不能为我解决问题。我在 Chrome 27 上
    • 那么显然这是一个错误 - 我在 Chrome 29 (dev) 上并且小提琴对我有用,所以显然他们修复了它。
    • 我正在进行的项目在 29 号发布之前到期,所以有什么临时修复的想法吗?
    • 对不起,我找不到):
    【解决方案2】:

    尝试使用 display: block;让 Chrome 将图像缩放到渲染大小以下:

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

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 1970-01-01
      • 2016-09-15
      • 2018-06-12
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多