backface-visibility
**backface-visibility
** CSS 属性设置元素背面的可见性,当它转向用户时。
试一试
元素的背面是其正面的镜像。虽然在 2D 中不可见,但当变换导致元素在 3D 空间中旋转时,背面可能会变得可见。(此属性对 2D 变换没有影响,2D 变换没有透视。)
语法
css
/* Keyword values */
backface-visibility: visible;
backface-visibility: hidden;
/* Global values */
backface-visibility: inherit;
backface-visibility: initial;
backface-visibility: revert;
backface-visibility: revert-layer;
backface-visibility: unset;
backface-visibility
属性指定为以下列出的关键字之一。
值
visible
-
当背面转向用户时,它可见。
-
背面隐藏,当它背离用户时,有效地使元素不可见。
正式定义
正式语法
backface-visibility =
visible |
hidden
示例
带有透明和不透明面的立方体
此示例显示一个带有透明面的立方体,以及一个带有不透明面的立方体。
HTML
html
<table>
<tr>
<th><code>backface-visibility: visible;</code></th>
<th><code>backface-visibility: hidden;</code></th>
</tr>
<tr>
<td>
<div class="container">
<div class="cube showbf">
<div class="face front">1</div>
<div class="face back">2</div>
<div class="face right">3</div>
<div class="face left">4</div>
<div class="face top">5</div>
<div class="face bottom">6</div>
</div>
</div>
<p>
Since all faces are partially transparent, the back faces (2, 4, 5) are
visible through the front faces (1, 3, 6).
</p>
</td>
<td>
<div class="container">
<div class="cube hidebf">
<div class="face front">1</div>
<div class="face back">2</div>
<div class="face right">3</div>
<div class="face left">4</div>
<div class="face top">5</div>
<div class="face bottom">6</div>
</div>
</div>
<p>The three back faces (2, 4, 5) are hidden.</p>
</td>
</tr>
</table>
CSS
css
/* Classes that will show or hide the
three back faces of the "cube" */
.showbf div {
backface-visibility: visible;
}
.hidebf div {
backface-visibility: hidden;
}
/* Define the container div, the cube div, and a generic face */
.container {
width: 150px;
height: 150px;
margin: 75px 0 0 75px;
border: none;
}
.cube {
width: 100%;
height: 100%;
perspective: 550px;
perspective-origin: 150% 150%;
transform-style: preserve-3d;
}
.face {
display: block;
position: absolute;
width: 100px;
height: 100px;
border: none;
line-height: 100px;
font-family: sans-serif;
font-size: 60px;
color: white;
text-align: center;
}
/* Define each face based on direction */
.front {
background: rgb(0 0 0 / 30%);
transform: translateZ(50px);
}
.back {
background: rgb(0 255 0 / 100%);
color: black;
transform: rotateY(180deg) translateZ(50px);
}
.right {
background: rgb(196 0 0 / 70%);
transform: rotateY(90deg) translateZ(50px);
}
.left {
background: rgb(0 0 196 / 70%);
transform: rotateY(-90deg) translateZ(50px);
}
.top {
background: rgb(196 196 0 / 70%);
transform: rotateX(90deg) translateZ(50px);
}
.bottom {
background: rgb(196 0 196 / 70%);
transform: rotateX(-90deg) translateZ(50px);
}
/* Make the table a little nicer */
th,
p,
td {
background-color: #eeeeee;
margin: 0px;
padding: 6px;
font-family: sans-serif;
text-align: left;
}
结果
规范
规范 |
---|
CSS 变换模块级别 2 # backface-visibility-property |
浏览器兼容性
BCD 表格仅在浏览器中加载