HTML Element Style How to - Create Negative z-index on CSS generated content








Question

We would like to know how to create Negative z-index on CSS generated content.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
html {<!--from   w  w w.  j a  v  a2s .co  m-->
  background-color: #282828
}

div {
  width: 320px;
  height: 240px;
  margin: -120px 0 0 -160px;
  position: absolute;
  top: 50%;
  left: 50%;
  background: #18f;
  background: rgba(17, 136, 255, .9);
}

div:before, div:after {
  display: block;
  width: 300px;
  height: 80px;
  position: absolute;
  top: 50%;
  margin-top: -40px;
  font: bold 32px/80px Arial;
  text-align: center;
}

div:before {
  left: -160px;
  z-index: -1;
  background: white;
  content: "Negative z-index";
}

div:after {
  right: -160px;
  color: white;
  background: #555;
  content: "Positive z-index";
}
</style>
</head>
<body>
  <div></div>
</body>
</html>

The code above is rendered as follows: