css - 容器内的最大高度 <img> 具有最大高度,行为不一致

标签 css

我有一个以像素为单位定义最大高度的容器,然后是一个以百分比为单位定义最大高度的 IMG。在 Chrome 中,这按预期工作,在其他浏览器中,img 只是扩展到容器之外。

有人知道哪种行为是正确的,为什么?

<figure>

    <img src="http://images.autostash.com/parts/img/medium/wix/24056.jpg" alt="no picture">     

</figure>

figure {
    max-width: 90px;
    max-height: 90px;
    border: 1px solid red;
}

img {
    max-height: 90%;
    display: block;
    margin: 0 auto;
}

http://jsfiddle.net/Dygerati/wWRJK/

最佳答案

根据MDN description of the CSS height property ,

The is calculated with respect to the height of the containing block. If the height of the containing block is not specified explicitly, the value computes to auto. A percentage height on the root element (e.g. ) is relative to the viewport.

因此,由于您仅声明了 max-heightmin-height,而不是 height,因此 img 高度默认为auto

关于css - 容器内的最大高度 <img> 具有最大高度,行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17198992/

相关文章:

javascript - 如何在 <pre> 标签中自动换行?

html - 如何用css或JS自定义iframe滚动条

javascript - Bootstrap 3 Navbar 切换 - 折叠但按钮不出现

css - 在 flexbox 布局中缩放背景图像而不影响相邻元素

javascript - 响应式图像 slider 以填充 div 高度

html - 使封面图像在移动设备上响应

CSS 不透明度属性?

javascript - jQuery 在缓存中预加载图像并在其他功能继续之前获取原始图像大小

javascript - 如何在示例中构建像这样的简单图像旋转效果?

html - 将 2 个 div 相互重叠放置时,如果其中一个旋转 180 度,则它在可见性层次结构中处于次要位置。这是为什么?