How to apply multiple transforms in CSS?
P粉066224086
2023-08-31 14:39:52
<p>How do I apply multiple <code>transform</code> using CSS? </p>
<p>Example: The following only applies translation, not rotation. </p>
<pre class="brush:php;toolbar:false;">li:nth-child(2) {
transform: rotate(15deg);
transform: translate(-20px,0px);
}</pre>
<p><br /></p>
I'm adding this answer not because it might be helpful, but because it's true.
In addition to using the existing answers explaining how to do multiple translations via links, you can build the 4x4 matrix yourself
I showed the rotation matrix from some random site I found while googling:
Rotate around the x-axis:Rotate around y-axis:
I couldn't find a good translation example, so assuming I remember/understand it correctly, the translation:Rotate around z-axis:
Wikipedia article on transformations and the Pragamatic CSS3 tutorial explains this very well. Another guide I found that explains arbitrary rotation matrices is Egon Rath's Notes on Matrices
Of course, matrix multiplication is possible between these 4x4 matrices, so to perform rotations and translations you need to create the appropriate rotation matrix and multiply it by the translation matrix.You have to put them on one line like this:
When you have multiple transformation directives, only the last one is applied. Just like any other CSS rule.
Remember that multiple transformations in one line are applied from right to left.
This:
Transformation: scale(1,1.5)rotate(90deg);
And:
Transformation: Rotation (90deg) Scale (1,1.5);
Replacing will not produce the same result: