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

好久不见. 提交于 2019-11-30 01:04:27

问题


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?


回答1:


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>



回答2:


Short answer: No. View demo which works as of time of posting

Longer answer: It's not supposed to, but sometimes, such as when one element has a transform when its sibling doesn't, some browsers don't handle the situation well, resulting in the z-index being ignored.

Generally, however, this is because the transform itself is applied, not because of the translateZ. The solution in such a case it to give all relevant elements transform: translate3d(0px, 0px, 0px) or something similar which makes the browser render the elements more carefully



来源:https://stackoverflow.com/questions/17977220/can-css3-translatez-be-used-instead-of-z-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!