如何自动调整 div z 索引内容的高度和宽度

how to auto height ,width of div z-indexed content

我正在尝试自动调整父级 div 的高度宽度,其中有 3 张图像一张叠放,但即使 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>

不要制作所有图像 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>