HTML(29)——立体呈现

作用:设置元素的子元素是位于3D空间中还是平面中

属性名:transform-style

属性值:

  • flat:子级处于平面中
  • preserve-3d:子级处于3D空间

步骤:

  1. 父级元素添加 transform-style:preserve-3d
  2. 子级定位
  3. 调整子盒子的位置(位移或旋转)

演示:

复制代码
  <style>
    .cube {
      width: 200px;
      height: 200px;
      margin: 100px auto;
      background-color: rgb(106, 220, 116);
      transition: all 2s;
    }

    .cube div {
      width: 200px;
      height: 200px;
    }

    .front {
      background-color: #2180d4;
    }

    .back {
      background-color: #dd1e1e;
    }
  </style>
</head>

<body>
  <div class="cube">
    <div class="front"></div>
    <div class="back"></div>
  </div>
</body>

按照步骤父级添加transform-style: preserve-3d; 为了更好的视觉效果我又添加了视距transform-style: preserve-3d; 然后根据子绝父相 原则给它们定位,最后**transform: translateZ(80px)**将两个盒子分开,添加一个旋转的效果使其更明显。

复制代码
  <style>
    .cube {
      position: relative;
      width: 200px;
      height: 200px;
      margin: 100px auto;
      background-color: rgb(106, 220, 116);
      transition: all 2s;
      transform-style: preserve-3d;
      transform-style: preserve-3d;
    }

    .cube div {
      position: absolute;
      left: 0;
      top: 0;
      width: 200px;
      height: 200px;
    }

    .front {
      background-color: #2180d4;
      transform: translateZ(80px);
    }

    .back {
      background-color: #dd1e1e;
    }

    .cube:hover {
      transform: rotateY(180deg);
    }
  </style>
</head>

<body>
  <div class="cube">
    <div class="front">前面</div>
    <div class="back">后面</div>
  </div>
</body>
相关推荐
kyriewen7 小时前
面试官问你:“AI 能写 80% 的代码了,公司为什么还需要你?”
前端·javascript·面试
甲维斯8 小时前
又升级咯!坦克大战2026,科技与复古并存!
前端·人工智能·游戏开发
搬砖的码农10 小时前
(08)为什么我的 Agent 一跑后台服务就卡死
前端·agent·ai编程
飘尘10 小时前
前端转全栈(Java 后端)必须要知道的:开发中的锁机制与分布式并发控制
前端·后端·全栈
亲亲小宝宝鸭10 小时前
前端性能监控:web-vitals
前端·性能优化·监控
陆枫Larry11 小时前
可滚动页面背景填不满:`height: 100vh` vs `min-height: 100vh`
前端
Patrick_Wilson11 小时前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员
kyriewen11 小时前
今天的科技圈,全在抢英伟达的饭碗
前端·面试·ai编程