How to apply multiple transforms in CSS?
P粉066224086
P粉066224086 2023-08-31 14:39:52
0
3
495

How do I apply multiple transform using CSS?

Example: The following only applies translation, not rotation.

li:nth-child(2) { transform: rotate(15deg); transform: translate(-20px,0px); }


P粉066224086
P粉066224086

reply all (2)
P粉081360775

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 canbuild the 4x4 matrix yourself

I showed the rotation matrix fromsome random site I found while googling:

Rotate around the x-axis:

Rotate around y-axis:
Rotate around z-axis:

I couldn't find a good translation example, so assuming I remember/understand it correctly, the translation:

[1 0 0 0] [0 1 0 0] [0 0 1 0] [x y z 1]
See the

Wikipedia article on transformationsand thePragamatic CSS3 tutorialexplains this very well. Another guide I found that explains arbitrary rotation matrices isEgon 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.

This gives you more freedom to get it just right, and also makes it almost completely impossible for anyone to understand what it's doing, including you for five minutes.

But, you know, it works.

Edit: I just realized that I missed probably the most important and practical use, which is to step-by-step create complex 3D transformations via JavaScript, where things would make a lot more sense.

    P粉231112437

    You have to put them on one line like this:

    li:nth-child(2) { transform: rotate(15deg) translate(-20px,0px); }

    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 areappliedfrom right to left.

    This:Transformation: scale(1,1.5)rotate(90deg);
    And:Transformation: Rotation (90deg) Scale (1,1.5);

    Replacingwill notproduce the same result:

    .orderOne, .orderTwo { font-family: sans-serif; font-size: 22px; color: #000; display: inline-block; } .orderOne { transform: scale(1, 1.5) rotate(90deg); } .orderTwo { transform: rotate(90deg) scale(1, 1.5); }
    A
    A
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!