【问题标题】:CSS Image height restrict to parent div height. Parent div width and height are specified in percentages*** [duplicate]CSS 图像高度限制为父 div 高度。父 div 的宽度和高度以百分比指定*** [重复]
【发布时间】:2014-04-22 01:10:37
【问题描述】:

如何将图像的高度限制为以百分比指定高度的父容器?

如果图像宽度/高度 > 父级宽度/高度,我希望图像跨越父级。 如果图像宽度/高度

html, body{
    width:100%;
    height:100%;
}

#top{
    height:10%;
}

#middle{
    height:80%;
}

#bottom{
    height:10%;
}

img{
    width: how much?
    height: how much?
}

<html>
    <body>
        <div id="top">I am top</div>
        <div id="middle">
            <img src="/images/abc.jpg" />
        </div>
        <div id="bottom">I am bottom</div>
    </body>
</html>

【问题讨论】:

  • 我如何请求将此问题取消标记为重复??
  • 重新开始投票。但它一个骗局:stackoverflow.com/a/19193223/2888561
  • 这是一个重复的问题吗?我希望父级是一个通用容器[没有固定的宽度或高度!] 此外,我批准的解决方案是使用 width:"auto" 和 height:100% 来避免图像拉伸。建议的问题为固定宽度和高度的父容器提供了解决方案。
  • 容器无所谓,问题是容器。 width: auto应该无论如何都是默认的。
  • 我很害怕,但这不是我想要的解决方案而且宽度自动不是 Chrome 最新浏览器的默认设置。

标签: html image css


【解决方案1】:
height: 100%;
width: auto;

只需将图像高度定义为百分比,自动宽度将保持比例。

【讨论】:

  • 这在一定程度上有所帮助。图像宽度被拉伸。例如:我的浏览器是 1366*768。我的中间 div 是 80% 高度 = 614px。我的图像是 1600*1600 像素。当我设置 height:100% 时,宽度会被拉伸。如何避免这种情况?
  • 完美!宽度:自动;创造了奇迹!
  • 就我而言,没有width: auto; 也没问题。
  • 不适合我(
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
相关资源
最近更新 更多