Search code examples
htmlcss3dtransformperspective

Reset perspective values inner DIV


I have some css 3D divs but im having some problems.. I want to rotate the outer div in perspective, but i don't want to rotate the inner div with it. How do I reset these styles to make the inner div flat again.

The HTML:

<section class="container" style="-webkit-perspective: 2000px; -webkit-perspective-origin-x: 0%; -webkit-perspective-origin-y: 0%;">
<div id="cube">
  <div class="bottom plutoring">
        <div class="planet pluto">
              <p> pluto </p>
        </div>
  </div>
</div>

The CSS

.container {
  width: 1000px;
  height: 900px;
  position: relative;
  margin: 0 auto 40px;
  -webkit-perspective: 1200px;
     -moz-perspective: 1200px;
       -o-perspective: 1200px;
          perspective: 1200px;
}

#cube {
  width: 100%;
  height: 100%;
  position: absolute;
  -webkit-transform-style: preserve-3d;
     -moz-transform-style: preserve-3d;
       -o-transform-style: preserve-3d;
          transform-style: preserve-3d;
}
.bottom {
     -webkit-transform: rotateX( 82deg ) rotateY( -5deg ) rotateZ( 197deg );
}

Any suggestions?


Solution

  • You need 2 things:

    • enable in the parent of the planet preserve3d
    • transform the planet in the oposite way.

    CSS

    .bottom {
         -webkit-transform: rotateX( 82deg ) rotateY( -5deg ) rotateZ( 197deg );
        background:green;
        -webkit-transform-style: preserve-3d;
    }
    
    .planet{
        background:blue;
        display:inline-block;
        font-size: 40px;
         -webkit-transform:  rotateZ( -197deg ) rotateY( 5deg ) rotateX( -82deg ) ;
    }
    

    Note that the inverse transform is the original with the signs changed and the order reversed.

    fiddle