csscss-transformstranslate3d

Can css3 translateZ() be used instead of z-index?


For example having 2 div's positioned absolute, one can put first div upon second by setting first div's z-index higher than second one's. Can we achieve such behaviour using translateZ() or translate3d?


Solution

  • The answer now, 3 years after, is that you can. You need to use transform-style: preserve-3d; on the parent, but it's possible.

    .container {
      transform-style: preserve-3d;
    }
    .test1 {
      width: 500px;
      height: 500px;
      background: red;
      transform: translate3d(0, 0, 1px);
    }
    .test2 {
      width: 500px;
      height: 500px;
      background: green;
      left: 250px;
      top: 250px;
      position: absolute;
      transform: translate3d(0, 0, 0);
    }
    <div class="container">
      <div class="test1">
        test
      </div>
    
      <div class="test2">
        test #2
      </div>
    </div>