The order of transformations in CSS significantly impacts the visual result because each transformation is applied relative to the current coordinate system, which changes after each operation. Here's a breakdown of the two declarations:
1. transform: translate(-50%, 0%) rotate(45deg);
- Step 1: Translate horizontally by -50% of the element's width (leftward).
The element shifts left, but its coordinate system remains unchanged (axes are still horizontal/vertical).
- Step 2: Rotate 45 degrees around the element's center.
The rotation occurs after the translation, so the element ends up rotated at its new translated position.
2. transform: rotate(45deg) translate(-50%, 0%);
- Step 1: Rotate 45 degrees around the element's center.
The element and its local coordinate system rotate (axes are now tilted at 45°).
- Step 2: Translate by -50% of the element's width along the rotated x-axis.
Since the x-axis is now diagonal, the element moves diagonally (left-up or left-down, depending on rotation direction).
Key Difference:
- Translation direction differs:
- In the first case, translation is purely horizontal (along the original x-axis).
- In the second case, translation is diagonal (along the rotated x-axis).
Visual Comparison:
| Declaration | Step 1 | Step 2 | Final Position |
|---|
translate(-50%, 0%) rotate(45deg) | ⬅️ | ↖️ | Rotated at translated spot |
rotate(45deg) translate(-50%, 0%) | ↖️ | ↖️➡️ | Shifted diagonally from rotated center |
Practical Implications:
- Use
translate first to adjust position before rotation (e.g., centering an element horizontally, then rotating it).
- Use
rotate first if you need to move the element along its own rotated axes (e.g., sliding an element along its tilted orientation).
Fixing Order Issues:
If you need identical results after swapping order, adjust the translation values to account for the rotation. For example, to mimic translate(-50%, 0%) rotate(45deg) with rotation first, use:
transform: rotate(45deg) translate(-35.36%, -35.36%); /* ≈ -50% × cos(45°) */
(This compensates for the diagonal movement using trigonometry.)
Conclusion: Always consider transformation order—it fundamentally changes the outcome. Test visually to ensure the desired effect.