Section 30: 3D Transforms and Accessible Motion
CSS 3D transforms use X, Y, and Z axes to change an element's apparent position and orientation in three-dimensional space.
1. Common 3D functions
.move-forward { transform: translateZ(30px); }
.rotate-3d { transform: rotateY(35deg); }
.scale-depth { transform: scaleZ(1.5); }
.combined { transform: perspective(600px) rotateY(20deg); }rotateX() tilts around the X axis; rotateY() turns around the Y axis; rotateZ() rotates in the plane. translateZ() changes depth, while scaleZ() scales depth.
2. Perspective
.scene {
perspective: 700px;
}
.card {
transform: rotateY(15deg);
transform-style: preserve-3d;
}The perspective property on a parent establishes a shared perspective for its transformed children. The perspective() transform function applies perspective within an individual transform list.
3. Backface visibility
.card-face {
backface-visibility: hidden;
}backface-visibility controls whether the reverse side of a 3D-transformed element is visible.
4. Runnable 3D card example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>3D CSS Card</title>
<style>
body { font-family: Arial, sans-serif; padding: 30px; }
.scene { perspective: 700px; width: 240px; }
.card { padding: 28px; border-radius: 16px; background: linear-gradient(135deg, #2563eb, #7c3aed); color: white; transition: transform 300ms ease; }
.scene:hover .card { transform: rotateY(12deg) rotateX(4deg) translateZ(12px); }
@media (prefers-reduced-motion: reduce) {
.card { transition: none; }
.scene:hover .card { transform: none; }
}
</style>
</head>
<body>
<div class="scene">
<div class="card">
<h2>3D Card</h2>
<p>Hover to see a subtle depth effect.</p>
</div>
</div>
</body>
</html>5. Accessible motion
The prefers-reduced-motion media feature detects a user's operating-system preference for reduced motion. Reduce or remove nonessential transitions, animations, and movement when that preference is active.
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
scroll-behavior: auto !important;
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}This broad reset is an example, not a universal rule for every project. Prefer targeted overrides when possible, and ensure content remains available without motion. Avoid automatically starting large, distracting animations.
Common mistakes
- Applying 3D transforms without understanding the parent's perspective.
- Using strong 3D movement that makes text difficult to read.
- Assuming reduced-motion preferences only affect animations; transitions and smooth scrolling can also matter.
- Hiding essential content until an animation completes.
Practice exercise
Create a product card that tilts subtly on hover. Add a prefers-reduced-motion rule that removes the tilt and transition while keeping the content fully visible.
Knowledge check: 10 MCQs
- Which function rotates an element around the Y axis? Answer: rotateY(). Explanation: It rotates around the vertical axis.
- Which function moves an element along the Z axis? Answer: translateZ(). Explanation: It changes the element's depth position.
- What does perspective provide? Answer: A depth effect for 3D transformations. Explanation: It controls how depth is visually projected.
- Where can perspective be applied to create a shared scene? Answer: On a parent element. Explanation: Its transformed children share the parent's perspective.
- What does transform-style: preserve-3d do? Answer: Preserves 3D positioning of descendants. Explanation: It lets descendants remain positioned in the same 3D space where applicable.
- What does backface-visibility: hidden do? Answer: Hides the reverse side of an element. Explanation: It is useful for card-flip effects.
- What does prefers-reduced-motion detect? Answer: A user's reduced-motion preference. Explanation: It allows styles to adapt to that preference.
- Should essential content require an animation to finish before it can be accessed? Answer: No. Explanation: Content should remain accessible and usable without motion.
- Can transitions also need reduced-motion handling? Answer: Yes. Explanation: Transitions can cause motion just like keyframe animations.
- What is a good default for interactive 3D cards? Answer: Subtle motion with a reduced-motion alternative. Explanation: This supports usability, readability, and accessibility.