The transform
property allows you to visually manipulate an element by skewing, rotating, translating, or scaling:
.element {
width: 20px;
height: 20px;
transform: scale(20);
}
Even with a declared height and width, this element will now be scaled to twenty times its original size:
Giving this function two values will stretch it horizontally by the first and vertically by the second. In the following example, the element will now be twice the width but half the height of the original element:
.element {
transform: scale(2, .5);
}
Or you can be more specific without using the shorthand function:
transform: scaleX(2);
transform: scaleY(.5);
But scale()
is just one of many transform functions that are available.
Values
scale()
: Affects the size of the element. This also applies to thefont-size
,padding
,height
, andwidth
of an element, too. It’s also a a shorthand function for thescaleX
andscaleY
functions.skewX()
andskewY()
: Tilts an element to the left or right, like turning a rectangle into a parallelogram.skew()
is a shorthand that combinesskewX()
andskewY
by accepting both values.translate()
: Moves an element sideways or up and down.rotate()
: Rotates the element clockwise from its current position.matrix()
: A function that is probably not intended to be written by hand, but combines all transforms into one.perspective()
: Doesn’t affect the element itself, but affects the transforms of descendent elements’ 3D transforms, allowing them all to have a consistent depth perspective.
Skew
/* Skew points along the x-axis */
.element {
transform: skewX(25deg);
}
/* Skew point along the y-axis */
.element {
transform: skewY(25deg);
}
/* Skew points along the x- and y-axis */
.element {
transform: skew(25deg, 25deg);
}
The skewX
and skewY
transform functions tilt an element one way or the other. Remember: there is no shorthand property for skewing an element, so you’ll need to use both functions. In the example below, we can skew a 100px x 100px square to the left and right with skewX
:
Whilst in this example we can skew an element vertically with with skewY
:
Now let’s use skew()
to combine the two:
Rotate
.element {
transform: rotate(25deg);
}
This rotates an element clockwise from its original position, whilst a negative value would rotate it in the opposite direction. Here’s a simple animated example where a square continues to rotate 360 degrees every three seconds:
We can also use the rotateX
, rotateY
, and rotateZ
functions, like so:
Translate
.element {
transform: translate(20px, 10px);
}
This transform function moves an element sideways, or up and down. Why not just use top/left/bottom/right? Well, it’s a bit confusing at times. I would think of those as layout/positioning (they have better browser support anyway) and this as a way to move those things around as part of a transition or animation.
These values would be any length value, like 10px or 2.4em. One value will move the element to the right (negative values to the left). If a second value is provided, that second value will move it down (negative values up). Or, you can get specific:
transform: translateX(value);
transform: translateY(value);
It’s important to note that an element using transform
will not cause other elements to flow around it. By using the translate
function below and nudging the green square out of its original position, we’ll notice how the surrounding text will remain fixed in place, as if the square is a block element:
It’s also worth noting that translate
will be hardware accelerated if you want to animate that property, unlike position: absolute
.
Multiple values
With a space-separated list you can add multiple values to the transform
property:
.element {
width: 20px;
height: 20px;
transform: scale(20) skew(-20deg);
}
It’s worth noting that there is an order in which these transforms will be carried out, in the example above `skew` will be performed first and then the element will be scaled.
Matrix
The matrix
transform function can be used to combine all transforms into one. It’s a bit like transform shorthand, only I don’t believe it’s really intended to be written by hand. There are tools out there like The Matrix Resolutions, which can convert a group of transforms into a single matrix declaration. Perhaps in some situations this can reduce file size, although author-unfriendly micro optimizations like that are likely not worth your time.
For the curious, this:
rotate(45deg) translate(24px, 25px)
…can also be represented as:
matrix(0.7071067811865475, 0.7071067811865476, -0.7071067811865476, 0.7071067811865475, -0.7071067811865497, 34.648232278140824)
3D Transforms
Most of the above properties have 3D versions of them.
translate3d(x, y, z)
translateZ(z)
The third value in translate3d
or the value in translateZ
moves the element toward the viewer, negative values away.
scale3d(sx, sy, sz)
scaleZ(sz)
The third value in scale3d
or the value in scaleZ
affects the scaling along the z-axis (e.g. the imaginary line coming straight out of the screen).
rotateX(value)
rotateY(value)
rotate3d(x, y, z)
rotateX
and rotateY
will rotate an element in 3D space around those axises. rotate3d
allows you to specify a point in 3D space in which to rotate the element around.
matrix3d(…)
A way to programmatically describe a 3D transform in a 4×4 grid. Nobody will ever hand write one of these, ever.
perspective(value)
This value doesn’t affect the element itself, but it affects the transforms of descendent elements’ 3D transforms, allowing them to all have a consistent depth perspective.
More Information
- MDN Docs on transform and usage.
- David DeSandro’s documentation on 3D transforms
- Surfin’ Safari: 3D transforms
- W3C spec on CSS3 transforms
- Intro to CSS 3D transforms
- Tricks for Using CSS
translateZ()
andperspective()
(DigitalOcean)
Related properties
backface-visibility
.element { backface-visibility: hidden; }
perspective
.element { perspective: 1000px; }
perspective-origin
.element { perspective-origin: 25% 75%; }
transform-origin
.element { transform-origin: top left; }
transform-style
.element { transform-style: preserve-3d; }
will-change
.element { will-change: transform; }
Random fact: a transform creates a positioning context just like position: relative;
does.
Browser support
Support for 2D Transforms
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
4* | 3.5* | 9* | 12 | 3.1* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
123 | 124 | 2.1* | 3.2* |
Support for 3D Transforms
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
12* | 10* | 11 | 12 | 4* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
123 | 124 | 3* | 3.2* |
thanks a lot.
3D Transforms Will be usefull…
https://codepen.io/uiswarup/full/WPEZqd
3d rotate
Hi, I dont understand the ScaleZ, in transform, if u change the scaleX(2) it will scale the width, but we have not the depth value of the element so scaleZ(3) meanse 3 * 0 = 0
so scaleZ has no effect on element
scalez(3) is equivalent to z-index:3
A quick note that
rotate3d()
actually takes 4 arguments like so:Where the first three arguments describe a point in 3d space around which you wish to rotate; and the fourth an angle by which to rotate the element.
In actual fact the first three arguments are a vector describing the axis of rotation. ie they take a unitless
<number>
not a<length>
woow thanks
http://www.ituto.org
spam…
Spammer
-webkit-transform: rotate(-2deg);
This will not occupy its original space in Webkit-Browsers… Causes some problems for me
Is there a way to scale the text inside that box as well?
Yeah we can just take an element like p. then generate code.
p{
-moz-transform: skewX(12deg) skewY(32deg);
-webkit-transform: skewX(12deg) skewY(32deg);
-o-transform: skewX(12deg) skewY(32deg);
-ms-transform: skewX(12deg) skewY(32deg);
transform: skewX(12deg) skewY(32deg);
}
Facing problem while applying transform(translation) on button:hover ?? Does anyone have solution or faced similar problem.??
This is coding of my css file
Transform is not available in all browsers becouse its new css property. So you must do it cross browser.
Use -moz-transform for Mozila FireFox, -webkit-transform for Safari,Chrome…
You got a “`” in before your code, that’s why.
Why this 2 codes are the same? I agree about rotate in both but tanslate in matrix must be 24,25 instead of -0.7071067811865497, 34.648232278140824.
1) rotate(45deg) translate(24px, 25px)
2) matrix(0.7071067811865475, 0.7071067811865476, -0.7071067811865476, 0.7071067811865475, -0.7071067811865497, 34.648232278140824)
The matrix function will take this values that represent another values like this.
matrix( x scale, y skew, x skew, y scale, x translation (position), y translation (position) )
Extra:
If you want to download free logo this is the place.
http://www.bestfreelogo.com/vector-logos/abstract/
2D Transforms work in IE 9+ , works through prefix -ms-
Can you give me a way to 3d rotate in Adobe Flash CS3 .It can be some code , etc..
really cool :)
thanks for your time
It seems that a positive value of skewX is counter-clockwise, as opposed to the values for rotateX, which are clockwise. Very strange I think, so your example of skewX(25deg) is wrong.
Hello Ruud, do you have the answer why positive value of skewX is counter-clockwise instead of clockwise. I cudnt get it why it works that way. And Hello Chris, hoping for ur answer.
Getting the logic of it all I also came to this conclusion. Does anyone know the reason for this? It seems like an exception and makes it hard to understand what will happen when some values are applied.
The scale property makes the image scale from wish and heighthe zero.
I am using scroll to scale up or down. But as soon as I start scrolling the image goes to probably one pixel and scales according to that.
Is there a 0 to 100 property to manipulate “scale” to make it like 100 to 110?
It’s worth noting that there is now a
skew
shorthand, à laskew(10deg, 10deg)
.As shown in Skew section above, skewX(25deg) tilts it towards the right. And that makes sense to me(But we are both wrong here Chris). Because actually it tilts it towards the left. I could not relate it to the co-ordinate system, positive deg should have actually tilted it towards the right.
Hello..
Actually my qusetion is off topic but can u help me with that arrow in your active menu…
The arrow that comes down when HTML or SCSS or Result is active…
Plz help..
Thank you in advance… :)
skewX()
andskewY()
: tilts an element to the left or right, like turning a triangle into a parallelogram.=> a square into a parallelogram, not a triangle (or I don’t get it).
Or a rectangle in fact.
wao
Yep that’s for sure )) You got it. There is suppose to be not a triangle but a square into a parallelogram.
It’s strange how scale affects translate if you add them in the wrong order.
If I write:
transform: scale(.05, .05) translateX(100px);
…it only moves 5 pixels
It’s like the distance has increased or the translate pixels are scaled as well.
However if I write like this:
transform: translateZ(-2000px) translateX(400px);
…it gets smaller but still moves 400px from it’s original place at the screens depth.
In case 2 I would have understood if translateX was a horizontal movement in relation to the new distance from the screen, but scale doesn’t have anything to do with perspective, right?
Since this thread is still alive I might as well try once more to see if anyone has a logical explanation for the question above.
How come the translate value get “scaled” if I scale down an element before I translate it, but not if I move it way back on the Z axis?
I’ll take a stab at this. I think, in case 1, its because you’ve scaled down to 5% and then applied a transformation of 100px. so 5% of it is 5px. if you changed the order of the two transformations, you’ll get your desired effect.
I’m pretty sure it has something to do with the order of transformations. They mentioned it briefly in the article above, but only stated that skew is applied before scale. I think the order is [skew -> scale -> translate].
Ok. Thank you Scott and Carry, for answering. I realize that is what causes it. Just have to remember it I guess
Cary, i don’t think there is a defined order, it depends on which instruction is written first. I just learnt today that ‘translate (x, y) scale(z)’ != ‘scale(z) translate(x, y)’
A really useful npm plugin gulp-autoprefixer, gulp-autoprefixer saves you having to manually add browser specific properties to your css.
Hey guys! Anyone care to tell me why this doesn’t rotate from the center like the default specification states…. I’m stumped
FYI, Changing transform-origin has no effect either….
Just guessing here, but it’s probably applying the scaling first, then rotating it. The change in scale is probably changing the center of rotation.
That’s odd, how does Opera have no 3d support when it is based on Chrome?
I have a strange problem. In edge, I’m getting an unnecessary scroll on one of my kendo windows. Using -webkit-transform on the icon in the window strangely makes the scroll go away. Not sure if this is the right solution. What am I missing?
Using
Scale3d(transform(rotateY: ): 20px): 10px);
doesn’t seem to work for me. Any ideas?
What if I want to remove the transform property? How do I do that?
You definitely can write the matrices by hand. All the other transformations are just shorthands for matrices. Learning matrix math will teach you why differently ordered transformations work like they do.
The 4×4 matrix has 3 parts:
the 3×3 matrix in the top left describes a linear transformation
the right side is the amount of translation
the bottom is for perspective. (Usually you’ll want to have just 0 0 0 1 at the bottom, which does nothing.)
The linear transformation matrix contains three vertical 3D vectors. They describe the how the x, y and z-axes are transformed, respectively. Imagine grabbing each axis and turning and stretching it until it until it is the corresponding vector and imagine that the space in between is rubber attached to the axes. For a better explanation, see this video: https://www.youtube.com/watch?v=kYB8IZa5AuE
Hello all,
Is it mandatory to use below things to support all browsers?
-webkit-transform: value;
-moz-transform: value;
-ms-transform: value;
-o-transform: value;
transform: value;
I believe that it’s probably worth mentioning that transform only applies to certain
display
types. I’ve read through this article, and wrote this simple code:and
It seems ok, but it does not work, because the
<span>
is a inline element. This problem can be solved by understanding this, and just addingdisplay: inline-block
at the.close
rule. So, I believe that this article would be improved if it mentioned which elements can be target by each transformation function.Transform appears to break effects/actions within an element. Eg background-attachement:fixed; or :hover does not work in a div that has had a transform applied to it, nor children thereof (not fully tested).
You refer to the transform functions three different ways:
1) “But scale() is just one of many transform functions that are available:”
2) “With a space-separated list you can add multiple values to the transform property:”
3) “Most of the above properties have 3D versions of them.”
Is the correct terminology just function?
Does the article needs an update? There is skew shorthand property!
https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/skew
Thanks for the heads up! Got that in there. :)
You don’t mention the order in which CSS transforms will be activated in a list.
“It’s worth noting that there is an order in which these transforms will be carried out, in the example above
skew
will be performed first and then the element will be scaled.”There’s a case where you’d have to animate using position:top — when you want to slide content down and there is a fixed header, because https://stackoverflow.com/a/15256339/452587.
Why does not working with Javascript rotate from 0 degree to 360 degree?
Hello Thanks for your article
I am using “transform” to make the title font that does not have a condensed version more compact.
But I have problem resizing the whole container.
How do I indicate only text compression while leaving the containers at their normal adaptive size?
h3.name-class {
transform: scale(0.8,1);
transform-origin: left;
}
Thank you
Just found SkewJS: https://github.com/wiserim/Skew
Everyone who has ever searched a way to have skewed elements with a reliable pixel with should have a look.
I have no idea why this repo is that unknown. Handling skewed contents with deg-values is pure hell on earth, especially when it comes to responsive design. Hopefully CSS will support pixel values in the future.