html - 将 2 个 div 相互重叠放置时,如果其中一个旋转 180 度,则它在可见性层次结构中处于次要位置。这是为什么?

标签 html css

在学习一点 HTML 时,我遇到了一些我无法理解的东西。

当我将 2 个 div 放在彼此之上时,我注意到如果第二个 div 旋转 180 度(即 transform:rotateY(180deg)),则会显示第一个,反之亦然(至少在 Chrome 上)。有人可以向我解释它是如何工作的或者规则是什么吗?它与 z-indexing 有什么关系吗?

这是一个说明我的问题的代码: https://jsfiddle.net/psgqktcn/3/

.card {
  height: 80px;
  width: 50px;
  line-height: 80px;
  font-size: 60px;
  position: absolute;
  text-align: center;
  transform-style: preserve-3d;
}

.card .front1 {
  background: red;
}

.card .back1 {
  background: green;
}

.card .front2 {
  background: red;
  transform: rotateY(180deg);
}

.card .back2 {
  background: green;
  transform: rotateY(180deg);
}

.card figure {
  position: absolute;
  height: 100%;
  width: 100%;
  color: white;
}
<div class=card class=flipped>
  <figure class="front1">1</figure>
  <figure class="back1">2</figure>
</div>

<div class=card class=flipped style="margin: 0 100px">
  <figure class="front2">1</figure>
  <figure class="back1">2</figure>
</div>

<div class=card class=flipped style="margin: 0 200px">
  <figure class="front1">1</figure>
  <figure class="back2">2</figure>
</div>

最佳答案

使用backface-visibility:

.card {
  height: 80px;
  width: 50px;
  line-height: 80px;
  font-size: 60px;
  position: absolute;
  text-align: center;
  transform-style: preserve-3d;
}

.card .front1 {
  background: red;
}

.card .back1 {
  background: green;
}

.card .front2 {
  background: red;
  transform: rotateY(180deg);
}

.card .back2 {
  background: green;
  transform: rotateY(180deg);
  backface-visibility: hidden;
}

.card figure {
  position: absolute;
  height: 100%;
  width: 100%;
  color: white;
}
<div class=card class=flipped>
  <figure class="front1">1</figure>
  <figure class="back1">2</figure>
</div>

<div class=card class=flipped style="margin: 0 100px">
  <figure class="front2">1</figure>
  <figure class="back1">2</figure>
</div>

<div class=card class=flipped style="margin: 0 200px">
  <figure class="front1">1</figure>
  <figure class="back2">2</figure>
</div>

关于html - 将 2 个 div 相互重叠放置时,如果其中一个旋转 180 度,则它在可见性层次结构中处于次要位置。这是为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091391/

相关文章:

html - 绝对定位 + 负边距随窗口大小调整而扭曲

html - 如何在CSS中制作过渡椭圆

css - html - 在 2 个内容 div 之间添加一个 div 分隔符

html - CSS <hr> 在文本旁边右对齐

html - 具有绝对定位的 Div 未占据 TD 的全高(IE 问题)

css - 一个div中两个水平div的流体高度

html - 为什么 react 中的边距无法正常工作?

CSS ajax 搜索框样式

html - 背景大小 polyfill 在 IE8 中不起作用?

iphone - <video> 在被点击前引起许多请求*