HTML Element Style How to - Set z-index in inner div higher than parent div








Question

We would like to know how to set z-index in inner div higher than parent div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.parentdiv {<!--from ww  w  .j a v a 2  s.  c om-->
  position: relative;
  top: 0;
  left: 0;
  z-index: 10;
  height: 500px;
  width: 400px;
  background-color: black;
}

.childiv {
  position: absolute;
  z-index: 12;
  top: 0;
  height: 300px;
  width: 400px;
  background-color: red;
}
</style>
</head>
<body>
  <div class="parentdiv">
    <div class="childiv"></div>
  </div>
</body>
</html>

The code above is rendered as follows: