html - Firefox 在没有设置宽度/高度的情况下不遵守父级的最大宽度/高度

标签 html css cross-browser parent-child

我遇到了一个奇怪的问题,在 Chrome/Safari 中,图像遵循父级内部的最大宽度/高度样式,没有设置宽度/高度。但在 Firefox/Opera 中没有。

<div id="container">
    <figure>
        <img src="http://imaging.nikon.com/lineup/dslr/d90/img/sample/pic_001b.jpg">
    </figure>
</div>

这就是图像和容器。 CSS如下:

body {
    width: 100%;
    height: 100%;
}
#container {
    width: 90%;
    height: 90%;
    margin: auto;
}
figure {
    position: relative;
    display: inline-block;
    margin: 0;
    vertical-align: top;
    line-height: 0;
}
img {
    height: auto;
    width: auto;
    max-width: 100%;
    max-height: 100%;
}

figure 元素仅用于通过使用 :before 选择器作为图像的叠加层,但如果它具有设置的宽度则不起作用/高度。有没有办法让 Firefox 和 Opera 尊重祖 parent 的高度/宽度?​​

JSFiddle:http://jsfiddle.net/GUrkj/1/

最佳答案

参见 here on MDN

具体来说:

The max-height CSS property is used to set the maximum height of a given element. It prevents the used value of the height property from becoming larger than the value specified for max-height.

还有……

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the percentage value is treated as none.

最后一部分的意思是,至少在 Firefox 中,没有明确说明的 height 值,max-height 变为 none -导致观察行为。

因此,您至少需要将 heightwidth 添加到 figure 元素。

关于html - Firefox 在没有设置宽度/高度的情况下不遵守父级的最大宽度/高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22326481/

相关文章:

javascript - 使用 JavaScript 更新背景图像时的不一致行为

css - 如何在 Angular 应用程序中检查 CSS 加载大小

javascript - 如何在另一个图像链接之上隐藏显示图像链接

javascript - jQuery.ajax - 为什么它不起作用?

HTML5 CSS3 模态窗口,点击外部关闭

php - 使用 php 导出的文本文件不会更新

html - anchor 标记 “target” 属性不起作用

cross-browser - 跨浏览器网站的最佳实践是什么?

javascript - 如何使用 Javascript 根据元素的颜色更改 ID

javascript - 表单提交后隐藏动画