I\'m trying to create a 3d navbar using pure CSS with transforms, transitions and perspective.
Here is my code:
First of all, thank you to all that commented and answered to this question, especially Josh!
Josh, your example works perfectly for browsers that support preserve-3d. The update you posted without preserve-3d appears flat on IE so it was still not perfected for all browsers.
After three days of headaches, I realized the problem. The origin of the sides was not being set correctly. The sides need to rotate around a point that is half way in on the Z axis.
Once I've updated the origin to :
transform-origin: 25px 25px -25px;
Once this was correct, all you really need to do is update the rotation of the object. No need to use any transformation of the X,Y,Z coordinates.
Here's the fiddle and the solution for a 3D Navigation bar that rotates and works for all browsers including IE10+.
http://jsfiddle.net/tx0emcxe/
This is vastly different from where you started, but let me post my CSS and show you the fiddle, and then I'll edit in a longer explanation of how and why this works:
HTML
<section class="container">
<nav id="nav-box" class="show-front">
<div class="front">
<a href="#">Show Bottom</a>
</div>
<div class="bottom">
<a href="#">Show Back</a></div>
<div class="back">
<a href="#">Show Top</a></div>
<div class="top">
<a href="#">Show Front</a></div>
</nav>
</section>
CSS
.container {
position: relative;
perspective: 1000px;
transform: scale(0.95);
}
#nav-box {
width: 100%;
height: 50px;
position: absolute;
transform-origin: center center;
transform-style: preserve-3d;
transition: transform 0.5s;
}
#nav-box div {
width: 100%;
height: 50px;
display: block;
position: absolute;
transition: background-color 0.5s;
}
#nav-box .front { transform: rotateX( 0deg ) translateZ( 25px ); background-color: #ccc; }
#nav-box .back { transform: rotateX( 180deg ) translateZ( 25px ); background-color: #ccc; }
#nav-box .top { transform: rotateX( 90deg ) translateZ( 25px ); background-color: #ccc; }
#nav-box .bottom { transform: rotateX( -90deg ) translateZ( 25px ); background-color: #ccc; }
#nav-box.show-front { transform: rotateY( 0deg ); }
#nav-box.show-front .bottom { background-color: #a0a0a0; }
#nav-box.show-front .top { background-color: #e0e0e0; }
#nav-box.show-back { transform: rotateX( -180deg ); }
#nav-box.show-back .bottom { background-color: #e0e0e0; }
#nav-box.show-back .top { background-color: #a0a0a0; }
#nav-box.show-top { transform: rotateX( -90deg ); }
#nav-box.show-top .front { background-color: #a0a0a0; }
#nav-box.show-top .back { background-color: #e0e0e0; }
#nav-box.show-bottom { transform: rotateX( 90deg ); }
#nav-box.show-bottom .front { background-color: #e0e0e0; }
#nav-box.show-bottom .back { background-color: #a0a0a0; }
Setting up our box
You started thinking about this the wrong way, I hate to say. You approached this as "How can I treat these four sides like a box" rather than "How can I make a box in CSS?"
So let's learn how to make a box.
First, we establish a box
container. Since this is a navigation box, let's call it nav-box
. All the transforms we apply (save for the shading, which we'll get to later) will be done on our nav-box
.
The rules on our nav-box
will determine how it behaves as an object. Let's discuss two in particular: transform-origin
and transform-style
transform-origin
defaults to center center, but I wanted to call it out here. This is basically going to tell our box: Hey, we need you to pivot around your absolute center. If we set this up as transform-origin: center bottom' it would look like the box is spinning around its bottom edge.
center top` and it would spin around its top edge. I don't think that's what you want, though.
transform-style
needs to be set to preserve-3d
. What this does is instruct the browser to not fuss with the elements with transform
underneath it. Other options include flat
which tells the browser to ignore rotates underneath it. The reason we want to set preserve-3d
on our nav-box
here is to ensure the transforms
we applied to the box sides are preserved when we transform
the parent. Neat stuff, huh?
Setting up our sides
We're setting our sides as children of our nav-box
and just positioning them in the order that they should be in using rotateX
:
0
rotation for the front180deg
for the back-90deg
for the bottom90deg
for the topWe could also set a left and right side right now with .left { transform: rotateY(-90deg); } .right { rotateY(90deg); }
. Note that we used the Y
axis for those two examples.
Secondly, we set a translateZ
value of 25px
. So what the hell is this doing? It's telling our boxes they need to move 25px
from the center of the parent relative to their respective rotations. Why did we choose 25px
? Because it's exactly half the height of each of our boxes. This means that it will flush up nicely with the sides at either edge.
And then the fun part:
We shade the boxes based on their position and what is facing the screen. The background colors are relative to what side of the box we're showing with show-front
, show-back
, etc. The side on the bottom gets darker, the side on the top gets lighter. I just liked that – totally not necessary to accomplish this task but makes it look a little more realistic.
Hope that helps!
Fiddle Example
So, there's not much pretty about this once we get through fixing it up for IE, but here it is. All preserve-3d
is doing is applying the transforms for you when we rotate a container, instead of flattening them. If we can't use preserve-3d
, we have to calculate based on the amount of total rotation.
This solution does that. I won't go as in-depth on this one, rather than to highlight how much more JavaScript this requires, and to highlight the .rewind
class:
#nav-box.rewind div {
backface-visibility: hidden;
}
Because we have to manually rewind this solution, we'll have to prevent the z-index reordering to be applied at the wrong times. That's where backface-visibility
comes in.
Example showing depth in IE
Another example without the need for the rewind class
Hope that solves IE for you.