z-index is canceled by setting transform(rotate)

Viewed 72490

Using transform property, z-index is canceled and appeared in the front. (When commenting out -webkit-transform, z-index is properly working in below code)

.test {
  width: 150px;
  height: 40px;
  margin: 30px;
  line-height: 40px;
  position: relative;
  background: white;
  -webkit-transform: rotate(10deg);
}

.test:after {
  width: 100px;
  height: 35px;
  content: "";
  position: absolute;
  top: 0;
  right: 2px;
  -webkit-box-shadow: 0 5px 5px #999;
  /* Safari and Chrome */
  -webkit-transform: rotate(3deg);
  /* Safari and Chrome */
  transform: rotate(3deg);
  z-index: -1;
}
<html>

<head>
  <title>transform</title>
  <link rel="stylesheet" type="text/css" href="transformtest.css">
</head>

<body>
  <div class="test">z-index is canceled.</div>
</body>

</html>

How do transform and z-index work together?

7 Answers

Set the div you want to stay on top to position:relative

Had a similar problem where siblings were being transform: translate()'d and z-index wouldn't work.

Most straightforward solution is to set position: relative on all siblings, then z-index would work again.

Quick fix: You could just rotate the other element by 0 degrees as well.

For those who still looking for the solution, I found this article how to solve issue with transform and z-index here

Simple usage of it is by doing this:

.parent { transform-style: preserve-3d; }
.parent:before { transform: translateZ(-1em); }

Set the div you want to stay on top to position:absolute

Related