我正在尝试自动设置父div的高度宽度,在该高度中,三张图像彼此堆叠,但是即使div和最后一张图像位于相同的z-index上,父级也不会采用内容的高度和宽度; 这是正在发生的事以及我想要的事的例证 我不能使用静态值,因为它需要响应 here is the illustration
这是我的代码:
.image_holder {
margin-top: 5em;
overflow: visible;
background-color: red;
display:block;
z-index: -12;
position:relative;
border-style: solid;
border-width: 5px;
border-color:red;
}
.image_preview_parent {
position: absolute;
}
/*----------layers start---------*/
.layer_Back {
z-index: -12;
}
.layer_Camera {
z-index: -11;
}
.layer_Logo {
z-index: -10;
}
<div class="image_holder">
<img class="image_preview_parent layer_Logo" src="https://www.transparencyatwork.org/assets/fallback/employers/logo/thumb_default-9fbd6d06cb43649ddc8bfd34eb4b1192396a73474ce3c27cb5830b9edf86ae23.png" />
<img class="image_preview_parent layer_Camera" src="https://freepngimg.com/thumb/sunglasses/14-2-sunglasses-transparent-thumb.png" />
<img class="image_preview_parent layer_Back" src="https://d33wubrfki0l68.cloudfront.net/673084cc885831461ab2cdd1151ad577cda6a49a/92a4d/static/images/favicon.png" />
</div>
答案 0 :(得分:0)
不要制作所有图像position:absolute
,因为您将所有图像从流中删除,因此没有任何流入内容会定义容器的高度。至少保留一个position:relative
。您还可以将容器更改为inline-block
,以使宽度适合内容。
请注意,z-index
与此处无关,因此无论使用什么值,它只会影响堆栈顺序,而不会影响高度或宽度:
.image_holder {
margin-top: 5em;
overflow: visible;
background-color: red;
display: block;
z-index: 10;/*this is the parent element so any value should be enough, you don't need to make it lower that the child */
position: relative;
border-style: solid;
border-width: 5px;
border-color: red;
display:inline-block; /*to fit the width*/
}
.image_preview_parent {
position:relative;
}
.image_preview_parent:not(:first-child) {
position: absolute;
left:0;
top:0;
}
/*----------layers start---------*/
.layer_Back {
z-index: -2;
}
.layer_Camera {
z-index: -1;
}
.layer_Logo {
z-index: 0;
}
<div class="image_holder">
<img class="image_preview_parent layer_Logo" src="https://www.transparencyatwork.org/assets/fallback/employers/logo/thumb_default-9fbd6d06cb43649ddc8bfd34eb4b1192396a73474ce3c27cb5830b9edf86ae23.png" />
<img class="image_preview_parent layer_Camera" src="https://freepngimg.com/thumb/sunglasses/14-2-sunglasses-transparent-thumb.png" />
<img class="image_preview_parent layer_Back" src="https://d33wubrfki0l68.cloudfront.net/673084cc885831461ab2cdd1151ad577cda6a49a/92a4d/static/images/favicon.png" />
</div>