Home > Web Front-end > CSS Tutorial > How Do I Apply Multiple CSS Transforms Correctly?

How Do I Apply Multiple CSS Transforms Correctly?

Susan Sarandon
Release: 2024-12-25 05:49:39
Original
149 people have browsed it

How Do I Apply Multiple CSS Transforms Correctly?

Applying Multiple CSS Transforms

CSS transforms allow developers to manipulate elements in various ways, including translation, rotation, and scaling. However, when applying multiple transforms to an element, only the last transformation is typically executed.

Problem:

In the following example, the translation transform is not applied because the rotation transform is placed after it:

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

Solution:

To apply multiple transforms, they must be placed on a single line, separated by spaces. The transforms will be applied from right to left:

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

Therefore, the rotation transform will be applied first, followed by the translation.

Order of Execution:

When applying multiple transforms, the order of the transforms matters. For instance, rotating an element 90 degrees and then scaling it by 1.5 will not produce the same result as scaling it first and then rotating it.

Consider the following demonstration:

.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);
}
Copy after login

In this example, ".orderOne" will appear rotated 90 degrees and scaled by 1.5, while ".orderTwo" will appear rotated 90 degrees and then scaled by 1.5. This demonstrates that the transform order matters and must be carefully considered when applying multiple transforms.

The above is the detailed content of How Do I Apply Multiple CSS Transforms Correctly?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template