By modifying the coordinate space, CSS transforms change the shape and position of the affected content without disrupting the normal document flow. This guide provides an introduction to using transforms.
CSS transforms are implemented using a set of CSS properties that let you apply affine linear transformations to HTML elements. These transformations include rotation, skewing, scaling, and translation both in the plane and in the 3D space.
Two major properties are used to define CSS transforms: transform and transform-origin
transform-origintransformHere is an unaltered image of the MDN logo:
Here is the MDN logo in an iframe, rotated 90 degrees from its bottom-left corner.
<img style="transform: rotate(90deg);
transform-origin: bottom left;"
src="https://mdn.mozillademos.org/files/12539/Screen%20Shot%202016-02-16%20at%2015.53.54.png">
Here is the MDN logo, skewed by 10 degrees and translated by 150 pixels on the X axis.
<img style="transform: skewx(10deg) translatex(150px);
transform-origin: bottom left;"
src="https://mdn.mozillademos.org/files/12539/Screen%20Shot%202016-02-16%20at%2015.53.54.png">
Performing CSS transformations in 3D space is a bit more complex. You have to start by configuring the 3D space by giving it a perspective, then you have to configure how your 2D elements will behave in that space.
The first element to set is the perspective. The perspective is what gives us the 3D impression. The farther from the viewer the elements are, the smaller they are.
How quick they shrink is defined by the perspective property. The smaller its value is, the deeper the perspective is.
perspective:0; | perspective:250px; |
|---|---|
| 1 2 3 4 5 6 | 1 2 3 4 5 6 |
perspective:300px; | perspective:350px; |
|---|---|
| 1 2 3 4 5 6 | 1 2 3 4 5 6 |
The second element to configure is the position of the viewer, with the perspective-origin property. By default the perspective is centered on the viewer, which is not always adequate.
perspective-origin:150px 150px; | perspective-origin:50% 50%; | perspective-origin:-50px -50px; |
|---|---|---|
| 1 2 3 4 5 6 | 1 2 3 4 5 6 | 1 2 3 4 5 6 |
Once you have done this, you can work on the element in the 3D space.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transforms/Using_CSS_transforms