CSS - backface-visibility Property



The CSS property backface-visibility determines the visibility of the back face of an element when turned towards the user.

Back face of an element is nothing but the mirror image of the front face of the element. When an element is transformed and rotated in a three-dimensional space, the back face can be made visible. Since 2D transforms have no perspective, this property has no effect on two-dimensional space.

Possible values

The CSS property backface-visibility takes one of the following values:

  • visible: Back face is visible when turned towards the user.

  • hidden: Back face is hidden and is invisible when turned towards the user.

Applies to

All the transformable elements.

Syntax

backface-visibility = visible | hidden;

CSS backface-visibility - visible Value

Following example demonstrates the use of CSS property backface-visibility which is set to visible. The cube is given a perspective value thus it is visible in the 3D space.

<html>
<head>
<style>
   .show div {
      backface-visibility: visible;
   }

   .container {
      width: 150px;
      height: 150px;
      margin: 75px 0 0 75px;
      border: none;
   }

   .cube {
      width: 100%;
      height: 100%;
      perspective: 350px;
      perspective-origin: 120% 120%;
      transform-style: preserve-3d;
   }

   .face {
      position: absolute;
      width: 100px;
      height: 100px;
      border: 1px solid black;
      line-height: 80px;
      font-size: 3.5em;
      color: white;
      text-align: center;
   }

   .front {
      background: rgba(178, 0, 178, 0.5);
      transform: translateZ(50px);
   }

   .back {
      background: rgba(178, 178, 0, 0.5);
      color: black;
      transform: rotateY(180deg) translateZ(50px);
   }

   .right {
      background: rgba(0, 0, 178, 0.5);
      transform: rotateY(90deg) translateZ(50px);
   }

   .left {
      background: rgba(178, 0, 0, 0.5);
      transform: rotateY(-90deg) translateZ(50px);
   }

   .top {
      background: rgba(0, 178, 0, 1);
      transform: rotateX(90deg) translateZ(50px);
   }

   .bottom {
      background: rgba(0, 0, 0, 0.2);
      transform: rotateX(-90deg) translateZ(50px);
   } 
</style>
</head>
<body>
   <h1>backface-visibility - visible</h1>
   <p>
      The back faces (2, 4, 5) are visible through the front faces (1, 3, 6).
   </p>
   <div class="container">
   <div class="cube show">
      <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>
</body>
</html>

CSS backface-visibility - hidden Value

Following example demonstrates the use of CSS property backface-visibility which is set to hidden, making the structure's back face not visible to the user.

<html>
<head>
<style>
   .hide div {
      backface-visibility: hidden;
   }

   .container {
      width: 150px;
      height: 150px;
      margin: 75px 0 0 75px;
      border: none;
   }

   .cube {
      width: 100%;
      height: 100%;
      perspective: 350px;
      perspective-origin: 120% 120%;
      transform-style: preserve-3d;
   }

   .face {
      position: absolute;
      width: 100px;
      height: 100px;
      border: 1px solid black;
      line-height: 80px;
      font-size: 3.5em;
      color: white;
      text-align: center;
   }

   .front {
      background: rgba(178, 0, 178, 0.5);
      transform: translateZ(50px);
   }

   .back {
      background: rgba(178, 178, 0, 0.5);
      color: black;
      transform: rotateY(180deg) translateZ(50px);
   }

   .right {
      background: rgba(0, 0, 178, 0.5);
      transform: rotateY(90deg) translateZ(50px);
   }

   .left {
      background: rgba(178, 0, 0, 0.5);
      transform: rotateY(-90deg) translateZ(50px);
   }

   .top {
      background: rgba(0, 178, 0, 1);
      transform: rotateX(90deg) translateZ(50px);
   }

   .bottom {
      background: rgba(0, 0, 0, 0.2);
      transform: rotateX(-90deg) translateZ(50px);
   } 
</style>
</head>
<body>   
   <h1>backface-visibility - hidden</h1>
   <p>Back faces (2, 4, 5) are hidden.</p>
   <div class="container">
   <div class="cube hide">
      <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>   
</body>
</html>
Advertisements