CSS 3D Transformations offer a powerful way to manipulate elements in three-dimensional space, adding depth and interactivity to web pages. These transformations are hardware-accelerated in modern browsers, ensuring smooth animations. To enable a true 3D context, the parent element of the transformed object must have the transform-style: preserve-3d property along with a perspective property.
translate3d()
The translate3d(x, y, z) function repositions an element in 3D space without distorting it. It takes three values representing the translation along the X (horizontal), Y (vertical), and Z (depth) axes. Using translate3d is often more performant for animations than moving elements with top/left properties.
Example 1: Basic 3D Translation
/* style.css */
.card-container {
perspective: 800px; /* Defines the perspective for the 3D space. */
}
.card {
transition: transform 0.5s ease-in-out;
width: 200px;
height: 250px;
background-color: #8A2BE2; /* BlueViolet */
}
.card:hover {
/* Moves the card 20px to the right, 30px down, and 50px closer to the viewer. */
transform: translate3d(20px, 30px, 50px);
}
Explanation This code snippet defines a perspective on the parent container, which is essential for creating a 3D scene. When a user hovers over the .card element, it smoothly transitions by moving along the X, Y, and Z axes, creating a subtle pop-out effect.
Example 2: Pulling an Element Forward
/* style.css */
.modal-container {
perspective: 1000px;
}
.modal {
transform: translate3d(0, 0, -300px); /* Initially position the modal away from the viewer. */
transition: transform 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
background-color: #FF4500; /* OrangeRed */
opacity: 0.5;
}
.modal.is-active {
transform: translate3d(0, 0, 0); /* Bring the modal to its original z-index position. */
opacity: 1;
}
Explanation Here, the .modal is initially pushed back along the Z-axis, making it appear distant. When the .is-active class is added, translate3d brings the modal back to its default position (Z=0), creating a dynamic effect of it flying towards the user.
Example 3: Creating a Parallax Effect
/* style.css */
.parallax-bg {
transform: translate3d(0, 0, -2px) scale(1.2); /* Push background back and scale it up. */
/* This element would be behind the main content. */
}
.parallax-content {
transform: translate3d(0, 0, 0); /* Keep content at the forefront. */
}
Explanation This example demonstrates a simple parallax effect by positioning a background image further away on the Z-axis and scaling it up to fill the viewport. As the user scrolls, the background appears to move slower than the foreground content, creating a sense of depth.
Example 4: 3D Form Input Feedback
/* style.css */
.input-wrapper {
perspective: 500px;
}
.input-field:focus {
/* Lifts the input field towards the user on focus. */
transform: translate3d(0, -5px, 20px);
transition: transform 0.3s ease;
box-shadow: 0 15px 25px rgba(0,0,0,0.2);
}
Explanation This CSS provides interactive feedback on a form field. When the user clicks into the .input-field, it slightly elevates and moves closer along the Z-axis, enhancing the user experience by visually indicating the active element.
rotateX()
The rotateX(angle) function rotates an element around the horizontal (X) axis by a specified angle. Positive values cause the top edge of the element to move away from the viewer, while negative values bring it closer. This is ideal for creating flip effects or perspective shifts.
Example 1: Card Flip Effect (Top to Bottom)
/* style.css */
.flip-container {
perspective: 1000px;
}
.flipper {
transition: transform 0.7s;
transform-style: preserve-3d;
}
.flip-container:hover .flipper {
/* Rotates the flipper 180 degrees around the X-axis. */
transform: rotateX(180deg);
}
Explanation This classic example creates a vertical card flip. On hover, the .flipper element rotates 180 degrees around its horizontal axis, revealing content that would be on its back face (assuming a back-face element is styled and positioned).
Example 2: Unfolding Panel
/* style.css */
.panel {
transform-origin: top; /* Set rotation origin to the top edge. */
transform: rotateX(-90deg);
transition: transform 0.5s ease-out;
background-color: #20B2AA; /* LightSeaGreen */
}
.container:hover .panel {
/* Rotates the panel down into view. */
transform: rotateX(0deg);
}
Explanation The .panel is initially folded up and out of sight by rotating it -90 degrees around the X-axis with its origin at the top. On hover, it rotates back to 0 degrees, creating an animation of it unfolding downwards.
Example 3: 3D Dock Icons
/* style.css */
.dock-item {
transition: transform 0.3s;
}
.dock-item:hover {
/* Tilts the icon back slightly on hover. */
transform: rotateX(35deg);
}
Explanation This provides a subtle 3D interaction for dock or menu items. When hovered, the .dock-item tilts backward around the X-axis, giving it a three-dimensional feel as if it's being pressed down.
Example 4: Perspective Text Effect
/* style.css */
.title-container {
perspective: 300px;
}
.title {
/* Creates a text effect that looks like it's receding into the distance. */
transform: rotateX(60deg);
color: #6A5ACD; /* SlateBlue */
}
Explanation By applying a rotateX transformation to text within a container that has perspective, the text appears tilted away from the user. This creates a cinematic or "Star Wars" style introduction effect.
rotateY()
The rotateY(angle) function rotates an element around the vertical (Y) axis. A positive angle value makes the right side of the element move away from the viewer, while a negative value brings it closer. It's commonly used for horizontal flipping, like turning the page of a book.
Example 1: Side-to-Side Card Flip
/* style.css */
.scene {
perspective: 1200px;
}
.card-content {
transform-style: preserve-3d;
transition: transform 0.8s;
}
.scene:hover .card-content {
/* Rotates the card 180 degrees around the Y-axis. */
transform: rotateY(180deg);
}
Explanation This code rotates the .card-content element 180 degrees around the Y-axis on hover. This is the foundation for a horizontal flip animation, perfect for showing the front and back of a product or profile card.
Example 2: Creating a 3D Cube
/* style.css */
.cube-face-front { transform: rotateY(0deg) translateZ(100px); }
.cube-face-right { transform: rotateY(90deg) translateZ(100px); }
.cube-face-back { transform: rotateY(180deg) translateZ(100px); }
.cube-face-left { transform: rotateY(-90deg) translateZ(100px); }
Explanation This snippet demonstrates how rotateY is used to position the side faces of a 3D cube. Each face is first rotated around the central Y-axis and then pushed out along the Z-axis to form the sides of the cube.
Example 3: Interactive Image Gallery
/* style.css */
.gallery-image {
transition: transform 0.4s ease;
}
.gallery-image:hover {
/* Slightly rotates the image to give a sense of interaction. */
transform: rotateY(-15deg) scale(1.05);
box-shadow: 10px 10px 30px rgba(0,0,0,0.3);
}
Explanation When a user hovers over a .gallery-image, it slightly rotates on its Y-axis and scales up. This adds a dynamic, responsive feel to the gallery, making it more engaging for the user.
Example 4: Door Swing Animation
/* style.css */
.door {
transform-origin: left; /* Set the hinge on the left side. */
transition: transform 1s;
background-color: #8B4513; /* SaddleBrown */
}
.room:hover .door {
/* Swings the door open by rotating it around the Y-axis. */
transform: rotateY(-110deg);
}
Explanation By setting transform-origin to the left, we simulate a hinge. On hover, the .door element rotates around the Y-axis from its left edge, creating a realistic door-opening animation.
rotateZ()
The rotateZ(angle) function rotates an element around the Z-axis, which extends perpendicularly from the screen towards the viewer. This is functionally identical to the standard 2D rotate() function, as it spins the element clockwise or counter-clockwise on the flat plane of the screen.
Example 1: Spinning Loader
/* style.css */
.loader {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
/* The 'infinite' keyword makes the animation loop forever. */
animation: spin 1s linear infinite;
}
@keyframes spin {
from {
transform: rotateZ(0deg);
}
to {
transform: rotateZ(360deg);
}
}
Explanation This is a classic use case for Z-axis rotation. The @keyframes animation continuously rotates the .loader element 360 degrees, creating a common spinning loading indicator.
Example 2: Tilted Headings
/* style.css */
.special-offer {
/* Rotates the element -10 degrees for a stylized, off-kilter look. */
transform: rotateZ(-10deg);
background-color: #FFD700; /* Gold */
padding: 10px 20px;
}
Explanation A static rotateZ can be used for design purposes. Here, the .special-offer element is slightly tilted to draw attention and add a playful, dynamic feel to the design.
Example 3: Interactive Knob Control
/* style.css */
.knob {
transition: transform 0.2s;
}
.knob.active {
/* Rotates a knob element, e.g., via JavaScript on drag. */
transform: rotateZ(45deg);
}
Explanation This CSS can be used with JavaScript to create a UI knob. When the .active class is applied, the .knob element rotates, providing visual feedback as a user interacts with the control.
Example 4: Wobbly Animation on Hover
/* style.css */
.icon:hover {
animation: wobble 0.5s ease-in-out;
}
@keyframes wobble {
0%, 100% { transform: rotateZ(0); }
25% { transform: rotateZ(-15deg); }
75% { transform: rotateZ(15deg); }
}
Explanation This keyframe animation uses rotateZ to create a fun wobble effect. When hovering over the .icon, it quickly rotates back and forth, making the UI feel more alive and responsive.
scale3d()
The scale3d(x, y, z) function resizes an element in 3D space along its X, Y, and Z axes. Values greater than 1 enlarge the element, while values between 0 and 1 shrink it. The Z-scaling factor is less commonly used but can create an illusion of the element's depth changing.
Example 1: Uniform Scaling on Hover
/* style.css */
.action-button {
transition: transform 0.3s;
background-color: #32CD32; /* LimeGreen */
}
.action-button:hover {
/* Scales the button up uniformly on all axes. Z-axis scaling is not visible without other 3D transforms. */
transform: scale3d(1.1, 1.1, 1.1);
}
Explanation This code makes the .action-button grow uniformly in size when the user hovers over it. Even though the Z-axis is scaled, the effect isn't apparent without other 3D transformations like rotation.
Example 2: Squish and Stretch Effect
/* style.css */
.bouncy-ball:hover {
animation: squish 0.6s;
}
@keyframes squish {
50% {
/* Stretches vertically and squishes horizontally. */
transform: scale3d(0.8, 1.2, 1);
}
}
Explanation This @keyframes rule uses scale3d to create a cartoonish squish-and-stretch animation. The element becomes narrower and taller at the midpoint of the animation, simulating a bouncy material.
Example 3: "Breathing" or Pulsing Effect
/* style.css */
.live-indicator {
animation: pulse 2s infinite;
background-color: #DC143C; /* Crimson */
border-radius: 50%;
}
@keyframes pulse {
0%, 100% {
transform: scale3d(1, 1, 1);
}
50% {
transform: scale3d(1.15, 1.15, 1.15);
}
}
Explanation The pulse animation creates a "breathing" effect by smoothly scaling the .live-indicator up and back down in an infinite loop. This is an effective way to draw attention to an element without being overly distracting.
Example 4: Revealing an Element by Scaling
/* style.css */
.pop-up {
/* Element is initially scaled down to be invisible. */
transform: scale3d(0, 0, 0);
transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.pop-up.visible {
/* Scales the element up to its normal size. */
transform: scale3d(1, 1, 1);
}
Explanation The .pop-up element starts at a scale of zero, making it invisible. When the .visible class is added, it scales up to its full size, creating a "popping" entrance animation that is more dynamic than simply changing opacity.
perspective()
The perspective() CSS function is applied to a transformed element to give it a 3D appearance. It defines the distance between the user and the Z plane, creating a sense of depth; a smaller value creates a more dramatic 3D effect.
Example 1: Basic Perspective with Rotation
/* Applies a perspective and rotates the element on its Y-axis */
.element-one {
transform: perspective(500px) rotateY(45deg);
width: 150px;
height: 150px;
background-color: steelblue;
}
Explanation This code applies a 500px perspective to the element before rotating it 45 degrees around the vertical (Y) axis. This creates the illusion that the right side of the element is further away from the viewer.
Example 2: Intense Perspective Effect
/* Creates a strong 3D effect with a smaller perspective value */
.element-two {
transform: perspective(200px) rotateX(30deg);
width: 150px;
height: 150px;
background-color: coral;
}
Explanation By using a smaller perspective() value of 200px, the 3D effect is much more pronounced. The element appears to be viewed from a closer distance, exaggerating the 3D transformation.
Example 3: Perspective with 3D Translation
/* Moves the element closer in 3D space with perspective */
.element-three {
transform: perspective(600px) translateZ(100px);
width: 150px;
height: 150px;
background-color: mediumseagreen;
}
Explanation Here, perspective() is combined with translateZ() to move the element "out of the screen" towards the user. The perspective function makes this translation visible by scaling the element up as it gets closer.
Example 4: Subtle Perspective on Hover
/* Applies a subtle perspective and rotation on hover */
.element-four {
width: 150px;
height: 150px;
background-color: gold;
transition: transform 0.5s;
}
.element-four:hover {
transform: perspective(800px) rotateY(-25deg);
}
Explanation This example demonstrates a user interaction where the perspective effect is applied only on hover. The high 800px value creates a subtle, less distorted 3D rotation, offering a smoother user experience.