100%的父图像.父级是固定位置的

Image 100% of parent. Parent is position fixed

本文关键字:定位 位置 图像 100%      更新时间:2023-09-26

有人能帮忙吗。我正在尝试使图像具有max-heightauto宽度。问题是这些规则被忽略了,因为它的父级是fixed

演示http://jsfiddle.net/gop4jhm9/5/

#myDiv {
    position: fixed;
    top: 100px;
    left: 50%;
    width: auto;
    height: auto;
    max-height: 100%;
    background: red;
    display: inline-block;
    padding: 10px;
    max-height: 30%;
}
#myDiv img {
    width: auto;
    height: 100%; // being ignored
}

有没有什么方法可以通过CSS、JS、jquery来做到这一点?

问题是#myDiv具有两个max-height属性。去掉第二个,它将div限制在30%,它将以固定方式工作。

这是因为它打算保持图像的纵横比,因为您将边距设置为auto。如果希望设置最大高度,请在#myDiv.imgmax-height属性中进行设置。

#myDiv {
    position: fixed;
    top: 100px;
    left: 50%;
    width: auto;
    height: auto;
    background: red;
    display: inline-block;
    padding: 10px;
}
#myDiv.img
{
     width: auto;
     max-height:90%;
}