CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Backface Visibility


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.