|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: "CSS Transforms (2D and 3D)" |
| 3 | +description: "Learn how to use the CSS transform property with functions like translate, scale, rotate, and skew to visually manipulate elements without affecting the document flow. Explore 2D and 3D transforms, GPU acceleration, and performance tips for smooth animations." |
| 4 | +keywords: [CSS transform, translate, scale, rotate, skew, matrix, 3D transform, GPU acceleration, performance] |
| 5 | +tags: [CSS transform, translate, scale, rotate, skew, matrix, 3D transform, GPU acceleration, performance] |
| 6 | +sidebar_label: Transforms |
| 7 | +--- |
| 8 | + |
| 9 | +The `transform` property allows you to visually manipulate an element in 2D or 3D space. Crucially, these transformations are applied *after* the element has been laid out in the document flow, meaning they do **not** affect the position of surrounding elements. |
| 10 | + |
| 11 | +This non-disruptive nature, combined with their ability to be **GPU-accelerated**, makes transforms the preferred way to create smooth, high-performance animations and transitions in modern web development. |
| 12 | + |
| 13 | +<AdsComponent /> |
| 14 | +<br /> |
| 15 | + |
| 16 | +## 1. The `transform` Property and Performance |
| 17 | + |
| 18 | +All transform functions are applied via the single `transform` property. |
| 19 | + |
| 20 | +```css title="styles.css" |
| 21 | +.box { |
| 22 | + /* Apply multiple transformations in sequence */ |
| 23 | + transform: translateX(10px) rotate(45deg) scale(1.2); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +### GPU Acceleration (`translate3d` and `translateZ`) |
| 28 | + |
| 29 | +Transforms are highly performant because they can often be handled directly by the graphics processing unit (GPU). When transforming an element, it is promoted to its own **composited layer**. |
| 30 | + |
| 31 | +To explicitly ensure GPU acceleration, you can use the 3D translation functions, even if you only need 2D movement: |
| 32 | + |
| 33 | +```css title="styles.css" |
| 34 | +/* Triggers hardware acceleration for smoother animation */ |
| 35 | +transform: translate3d(50px, 0, 0); |
| 36 | +``` |
| 37 | + |
| 38 | +## 2. 2D Transform Functions |
| 39 | + |
| 40 | +These are the most common functions used to manipulate elements on the X and Y axes. |
| 41 | + |
| 42 | +### 2.1. `translate()` (Moving) |
| 43 | + |
| 44 | +Shifts the position of an element along the X and Y axes. |
| 45 | + |
| 46 | +| Function | Parameter | Description | |
| 47 | +| :--- | :--- | :--- | |
| 48 | +| `translateX(n)` | Length or `%` | Moves the element horizontally. | |
| 49 | +| `translateY(n)` | Length or `%` | Moves the element vertically. | |
| 50 | +| `translate(x, y)` | Length or `%` | Moves the element both horizontally and vertically. | |
| 51 | + |
| 52 | +```css title="styles.css" |
| 53 | +/* Moves the element 100px to the right and 50px down */ |
| 54 | +transform: translate(100px, 50px); |
| 55 | +``` |
| 56 | + |
| 57 | +<AdsComponent /> |
| 58 | +<br /> |
| 59 | + |
| 60 | +### 2.2. `scale()` (Resizing) |
| 61 | + |
| 62 | +Resizes the element by a given factor. A factor of `1` is the original size. |
| 63 | + |
| 64 | +| Function | Parameter | Description | |
| 65 | +| :--- | :--- | :--- | |
| 66 | +| `scaleX(n)` | Number | Stretches or compresses horizontally. | |
| 67 | +| `scaleY(n)` | Number | Stretches or compresses vertically. | |
| 68 | +| `scale(n)` | Number | Scales uniformly (affects both X and Y). | |
| 69 | + |
| 70 | +```css title="styles.css" |
| 71 | +/* Shrinks the element to half its original size */ |
| 72 | +transform: scale(0.5); |
| 73 | +/* Makes the element 1.5 times its original size */ |
| 74 | +transform: scale(1.5); |
| 75 | +``` |
| 76 | + |
| 77 | +### 2.3. `rotate()` (Turning) |
| 78 | + |
| 79 | +Rotates the element around its central origin point. |
| 80 | + |
| 81 | +```css title="styles.css" |
| 82 | +/* Rotates the element 45 degrees clockwise */ |
| 83 | +transform: rotate(45deg); |
| 84 | + |
| 85 | +/* Rotates counter-clockwise */ |
| 86 | +transform: rotate(-15deg); |
| 87 | +``` |
| 88 | + |
| 89 | +### 2.4. `skew()` (Distorting) |
| 90 | + |
| 91 | +Distorts the element along the X and Y axes, giving it a slanted appearance. |
| 92 | + |
| 93 | +| Function | Parameter | Description | |
| 94 | +| :--- | :--- | :--- | |
| 95 | +| `skewX(n)` | Angle | Skews the element horizontally. | |
| 96 | +| `skewY(n)` | Angle | Skews the element vertically. | |
| 97 | +| `skew(x-angle, y-angle)` | Angles | Skews both horizontally and vertically. | |
| 98 | + |
| 99 | +```css title="styles.css" |
| 100 | +/* Skews the element by 15 degrees horizontally */ |
| 101 | +transform: skewX(15deg); |
| 102 | +``` |
| 103 | + |
| 104 | +<AdsComponent /> |
| 105 | +<br /> |
| 106 | + |
| 107 | +## 3. Controlling the Origin (`transform-origin`) |
| 108 | + |
| 109 | +By default, all transformations (especially `rotate` and `scale`) occur around the exact center of the element (50% 50%). |
| 110 | + |
| 111 | +The `transform-origin` property allows you to change this pivot point. It accepts X and Y coordinates (lengths, percentages, or keywords like `top`, `bottom`, `left`, `right`). |
| 112 | + |
| 113 | +```css title="styles.css" |
| 114 | +.pinwheel { |
| 115 | + /* Sets the pivot point to the top-left corner */ |
| 116 | + transform-origin: 0 0; |
| 117 | + /* Now, the rotation will appear to spin from the top-left */ |
| 118 | + transform: rotate(90deg); |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## 4. 3D Transforms (Introduction) |
| 123 | + |
| 124 | +3D transforms extend these concepts into the Z-axis (depth). To properly perceive 3D effects, you must apply the `perspective` property to the **parent container** of the 3D element. |
| 125 | + |
| 126 | +### Key 3D Functions |
| 127 | + |
| 128 | + * `translateZ(n)`: Moves the element closer or further away from the viewer. |
| 129 | + * `rotateX(n)`: Rotates the element around its horizontal axis (flips top/bottom). |
| 130 | + * `rotateY(n)`: Rotates the element around its vertical axis (flips left/right). |
| 131 | + |
| 132 | +```css title="styles.css" |
| 133 | +/* Applied to the PARENT container */ |
| 134 | +.stage { |
| 135 | + perspective: 800px; /* How far away the viewer is */ |
| 136 | +} |
| 137 | + |
| 138 | +/* Applied to the CHILD element */ |
| 139 | +.cube-face { |
| 140 | + /* Rotates 45 degrees, giving the appearance of depth */ |
| 141 | + transform: rotateY(45deg); |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +<AdsComponent /> |
| 146 | +<br /> |
| 147 | + |
| 148 | +## Interactive Transforms Demo |
| 149 | + |
| 150 | +Hover over the box to see a combination of translation, scaling, and rotation applied simultaneously via a smooth CSS transition. |
| 151 | + |
| 152 | +<CodePenEmbed |
| 153 | + title="Interactive Transforms Demo" |
| 154 | + penId="ByKVZKR" |
| 155 | +/> |
| 156 | + |
| 157 | +In this demo, hovering over the blue box triggers a smooth transformation that moves, rotates, and scales the element, all while maintaining high performance through GPU acceleration. The skewed box below demonstrates the `skewX` function in action. |
0 commit comments