3d-transforms
ساخت عمق و حرکت سهبعدی در رابطهای وب با CSS transforms
این مهارت برای ساخت افکتهای سهبعدی در رابط کاربری با CSS طراحی شده است؛ از card flip و page turn تا tilt، لایهبندی عمق و حرکت فضایی با perspective، translateZ و rotateY.
با استفاده از الگوهای آماده، توکنهای عمق و نکات performance و accessibility میتوانید تجربههایی روان، قابلکنترل و سازگار با reduced motion بسازید.
چه چیزی میگیرید
- پیادهسازی card flip و page turn با `transform-style` و `backface-visibility`
- تنظیم perspective و z-positioning برای ایجاد عمق بصری کنترلشده
- ساخت tilt hover و لایههای فضایی با `translateZ`
- راهکارهای بهینهسازی GPU و جلوگیری از reflow
- پشتیبانی از `prefers-reduced-motion` برای تجربه دسترسپذیرتر
برای چه کاری خوب است
- طراحان و توسعهدهندگان رابط وب که به افکتهای تعاملی سهبعدی نیاز دارند
- ساخت کارتهای چرخان، hero sectionهای immersive و modalهای لایهدار
- پروژههایی که به عمق بصری بدون استفاده از WebGL نیاز دارند
برای چه کاری نیست
- ساخت صحنههای پیچیده سهبعدی، مدلهای واقعی یا تعاملات مبتنی بر WebGL
- صفحههایی که تعداد زیادی عنصر متحرک دارند و performance محدود است
- مواردی که افکتهای حرکتی باید کاملاً غیرفعال باشند
دربارهٔ این اسکیل
3D Transforms & Depth
Creating spatial depth through CSS 3D transforms. Adds dimensionality and visual hierarchy to UI elements.
Core Concepts
Perspective
Distance from viewer to z-plane. Lower values = more dramatic 3D effect.
.container {
perspective: 800px; /* Viewer 800px away */
perspective-origin: 50% 50%; /* Looking at center */
}
Transform Style
Whether children exist in 3D space or flatten.
.parent {
transform-style: preserve-3d; /* Children in 3D space */
}
Backface Visibility
Show or hide element when rotated away.
.card-face {
backface-visibility: hidden; /* Hide when facing away */
}
Token Reference
Load from: tokens/depth.tokens.json
Perspective Values
| Token | Value | Effect |
|---|---|---|
shallow | 1200px | Subtle depth, safe |
medium | 800px | Standard 3D |
deep | 500px | Dramatic perspective |
extreme | 300px | Maximum distortion |
Z-Offset Values
| Token | Value | Use Case |
|---|---|---|
subtle | 10px | Hover lifts |
small | 20px | Card hovers |
medium | 40px | Modals, overlays |
large | 80px | Hero elements |
Common Patterns
Card Flip
.flip-card {
perspective: 800px;
width: 200px;
height: 300px;
}
.flip-card-inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 600ms ease-in-out;
transform-style: preserve-3d;
}
.flip-card:hover .flip-card-inner {
transform: rotateY(180deg);
}
.flip-card-front,
.flip-card-back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.flip-card-back {
transform: rotateY(180deg);
}
3D Hover Tilt
.tilt-card {
perspective: 1000px;
transform-style: preserve-3d;
}
.tilt-card:hover {
transform: rotateX(-10deg) rotateY(10deg) translateZ(20px);
transition: transform 300ms ease-out;
}
Depth Layers
.layer-background { transform: translateZ(-100px) scale(1.1); }
.layer-base { transform: translateZ(0); }
.layer-elevated { transform: translateZ(30px); }
.layer-floating { transform: translateZ(60px) scale(1.02); }
.layer-overlay { transform: translateZ(100px) scale(1.05); }
Performance Considerations
/* Enable GPU acceleration */
.gpu-accelerated {
transform: translateZ(0);
will-change: transform;
}
/* Avoid layout thrashing */
.avoid-reflow {
transform: translate3d(0, 0, 0); /* Use 3D transform instead of top/left */
}
Accessibility
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Reduce 3D effects for accessibility
const perspective = prefersReducedMotion ? 'none' : '800px';
const rotation = prefersReducedMotion ? 0 : calculatedRotation;
// Use opacity transitions instead of 3D for reduced motion
const transition = prefersReducedMotion
? { opacity: [0, 1] }
: { rotateY: [-90, 0], opacity: [0, 1] };
Common Mistakes
- No perspective container - Children need parent with perspective
- Wrong perspective value - Too low = extreme distortion
- Forgetting backface-visibility - Causes bleed-through on flips
- Mixing 2D and 3D - Can cause unexpected flattening
- Performance issues - Too many 3D elements hurts FPS
Related Skills
entrance-patterns/- 3D entrance animationsexit-patterns/- 3D exit animationseasing/- Easing curves for 3D motionaccessibility/- Reduced motion alternativesremotion/- 3D transforms in video
See references/ for detailed implementation guides.