The backface-visibility
property is used in 3D-transformed elements to define whether the back face of an element is visible or hidden when it is facing the user. This property is most commonly used in card-flipping animations where you want the back of the front face to be invisible as it turns away. By default, the back face is visible.
Example 1: Basic Card Flip with backface-visibility
/* style.css */
.card-container { perspective: 800px; }
.card {
width: 150px; height: 200px;
position: relative;
transform-style: preserve-3d;
transition: transform 0.7s;
}
.card:hover { transform: rotateY(180deg); }
.card-face {
position: absolute;
width: 100%; height: 100%;
/* Hide the back of the element when it's facing away */
backface-visibility: hidden;
}
.front { background: #3498db; }
.back { background: #e74c3c; transform: rotateY(180deg); }
Explanation Setting backface-visibility: hidden
on the card faces is what makes this effect work properly. When the card rotates, the back of the blue .front
element becomes hidden instead of showing a mirrored version, allowing the red .back
element to be seen correctly.
Example 2: Default Visible Behavior
/* style.css */
.flipper {
width: 100px;
height: 100px;
background-color: #f1c40f;
transition: transform 0.8s;
/* backface-visibility is 'visible' by default */
}
.flipper:hover {
transform: rotateY(180deg);
}
Explanation In this example, since backface-visibility
is not set, it defaults to visible
. When you hover over the element, it rotates, and you can see its mirrored back side, which is often undesirable for creating distinct front/back interfaces.
Example 3: Creating a Single-Sided Element
/* style.css */
.single-sided {
width: 120px;
height: 120px;
background: #16a085;
transition: transform 0.6s;
/* Make the element disappear when it's flipped over */
backface-visibility: hidden;
}
.single-sided:hover {
transform: rotateX(180deg);
}
Explanation Here, the element has backface-visibility: hidden
applied directly to it. When the element is rotated 180 degrees on the X-axis, it seems to disappear because its back face is not rendered.
Example 4: Flipping Content Vertically
/* style.css */
.vertical-flipper {
width: 200px; height: 100px;
perspective: 500px;
}
.flipper-content {
width: 100%; height: 100%;
transform-style: preserve-3d;
transition: transform 0.8s;
}
.vertical-flipper:hover .flipper-content {
transform: rotateX(180deg);
}
.side {
position: absolute; width: 100%; height: 100%;
backface-visibility: hidden;
}
.top { background: #2980b9; }
.bottom { background: #c0392b; transform: rotateX(180deg); }
Explanation This code demonstrates the same principle but with a vertical flip (rotateX
). The backface-visibility: hidden
property is equally effective for rotations on any axis, ensuring a clean transition between the top and bottom faces.
Example 5: Affecting Text Readability
/* style.css */
.readable-text {
transition: transform 1s;
backface-visibility: hidden;
}
.readable-text:hover {
transform: rotateY(180deg);
}
Explanation When backface-visibility
is visible
(the default), rotated text appears mirrored and unreadable. By setting it to hidden
, the text simply disappears when rotated away, which is usually a better user experience than showing confusing, reversed content.
Example 6: Combining with Opacity
/* style.css */
.fade-flip {
width: 150px; height: 150px;
position: relative;
perspective: 600px;
}
.face {
position: absolute;
width: 100%; height: 100%;
background: #8e44ad;
transition: transform 1s;
/* Hiding the back is crucial for this effect */
backface-visibility: hidden;
}
.fade-flip:hover .face {
transform: rotateY(180deg);
}
Explanation In this scenario, backface-visibility
ensures that as the element flips, its reversed side doesn't show through. This prevents visual glitches and ensures a smooth, clean animation where the element appears to turn over completely.
Example 7: Cube Face Visibility
/* style.css */
.cube-container { perspective: 800px; }
.cube {
width: 100px; height: 100px;
transform-style: preserve-3d;
transform: rotate3d(1, 1, 0, -45deg);
}
.cube-face {
position: absolute;
width: 100px; height: 100px;
border: 2px solid black;
/* Hiding backfaces makes the cube look solid */
backface-visibility: hidden;
}
.front { transform: translateZ(50px); background: rgba(255, 0, 0, 0.7); }
.back { transform: rotateY(180deg) translateZ(50px); background: rgba(0, 0, 255, 0.7); }
Explanation When building a 3D object like a cube, setting backface-visibility: hidden
on the faces is important. It prevents you from seeing the inside surfaces of the back faces through the front faces, making the object appear solid rather than transparent.