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>
相关推荐
之歆9 分钟前
DAY13_CSS3进阶完全指南 —— 背景、边框、文本、渐变、滤镜与 Web 字体(下)
前端·css·css3
剑神一笑26 分钟前
CSS 阴影生成器:从单层到多层叠加的艺术
前端·css·css3
lljss20201 小时前
1. NameServer 域名服务器---NS
linux·服务器·前端
anOnion1 小时前
构建无障碍组件之Tooltip Pattern
前端·html·交互设计
陈随易1 小时前
为什么今天还会有新语言?MoonBit 想解决什么问题?
前端·后端·程序员
西洼工作室1 小时前
unipp+vue3+python h5+app极验验证码集成全流程解析
前端·uni-app·全栈·极验
ZC跨境爬虫1 小时前
跟着 MDN 学 HTML day_15:(媒体缓冲、拖动与时间范围控制)
前端·笔记·ui·html·edge浏览器·媒体
李白的天不白2 小时前
webpack 与 webpack-cli 版本匹配问题
前端·webpack·node.js
tool2 小时前
Hermes Agent 从安装到生产:我的完整踩坑记录
前端