Search code examples
htmlcssz-index

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


i am trying to auto height width of parent div in which there are 3 images stacked one on the other but the parent is not taking the height and width of the content even when div and the last image are on same z-index; here is a illustration of what is happening and what i would like i cannot use static values as it is needed to be responsive here is the illustration

here is my code :

.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>


Solution

  • Don't make all the images position:absolute because you will remove all of them from the flow thus there is no in-flow content that will define the height of the container. keep at least one position:relative. You can also change the container to inline-block to make the width fit the content.

    As a side note, z-index has nothing to do here, so whataver the values you will use it will only affect the stack order and not height or width:

    .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>