- CSS 2D Transforms
- Browser Support
- CSS 2D Transforms Methods
- The translate() Method
- Example
- The rotate() Method
- Example
- Example
- The scale() Method
- Example
- Example
- The scaleX() Method
- Example
- Example
- The scaleY() Method
- Example
- Example
- The skewX() Method
- Example
- The skewY() Method
- Example
- The skew() Method
- Example
- Example
- The matrix() Method
- Example
- CSS Transform Properties
- CSS 2D Transform Methods
- translate
- Try it
- Syntax
- Values
- Formal definition
- Formal syntax
- Examples
- Translating an element on hover
- HTML
- CSS
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- transform
- Try it
- Syntax
- Values
- Accessibility concerns
- Formal definition
- translate
CSS 2D Transforms
CSS transforms allow you to move, rotate, scale, and skew elements.
Mouse over the element below to see a 2D transformation:
In this chapter you will learn about the following CSS property:
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS 2D Transforms Methods
With the CSS transform property you can use the following 2D transformation methods:
- translate()
- rotate()
- scaleX()
- scaleY()
- scale()
- skewX()
- skewY()
- skew()
- matrix()
Tip: You will learn about 3D transformations in the next chapter.
The translate() Method
The translate() method moves an element from its current position (according to the parameters given for the X-axis and the Y-axis).
The following example moves the element 50 pixels to the right, and 100 pixels down from its current position:
Example
The rotate() Method
The rotate() method rotates an element clockwise or counter-clockwise according to a given degree.
The following example rotates the element clockwise with 20 degrees:
Example
Using negative values will rotate the element counter-clockwise.
The following example rotates the element counter-clockwise with 20 degrees:
Example
The scale() Method
The scale() method increases or decreases the size of an element (according to the parameters given for the width and height).
The following example increases the element to be two times of its original width, and three times of its original height:
Example
The following example decreases the element to be half of its original width and height:
Example
The scaleX() Method
The scaleX() method increases or decreases the width of an element.
The following example increases the element to be two times of its original width:
Example
The following example decreases the element to be half of its original width:
Example
The scaleY() Method
The scaleY() method increases or decreases the height of an element.
The following example increases the element to be three times of its original height:
Example
The following example decreases the element to be half of its original height:
Example
The skewX() Method
The skewX() method skews an element along the X-axis by the given angle.
The following example skews the element 20 degrees along the X-axis:
Example
The skewY() Method
The skewY() method skews an element along the Y-axis by the given angle.
The following example skews the element 20 degrees along the Y-axis:
Example
The skew() Method
The skew() method skews an element along the X and Y-axis by the given angles.
The following example skews the element 20 degrees along the X-axis, and 10 degrees along the Y-axis:
Example
If the second parameter is not specified, it has a zero value. So, the following example skews the element 20 degrees along the X-axis:
Example
The matrix() Method
The matrix() method combines all the 2D transform methods into one.
The matrix() method take six parameters, containing mathematic functions, which allows you to rotate, scale, move (translate), and skew elements.
The parameters are as follow: matrix(scaleX(), skewY(), skewX(), scaleY(), translateX(), translateY())
Example
CSS Transform Properties
The following table lists all the 2D transform properties:
Property | Description |
---|---|
transform | Applies a 2D or 3D transformation to an element |
transform-origin | Allows you to change the position on transformed elements |
CSS 2D Transform Methods
Function | Description |
---|---|
matrix(n,n,n,n,n,n) | Defines a 2D transformation, using a matrix of six values |
translate(x,y) | Defines a 2D translation, moving the element along the X- and the Y-axis |
translateX(n) | Defines a 2D translation, moving the element along the X-axis |
translateY(n) | Defines a 2D translation, moving the element along the Y-axis |
scale(x,y) | Defines a 2D scale transformation, changing the elements width and height |
scaleX(n) | Defines a 2D scale transformation, changing the element’s width |
scaleY(n) | Defines a 2D scale transformation, changing the element’s height |
rotate(angle) | Defines a 2D rotation, the angle is specified in the parameter |
skew(x-angle,y-angle) | Defines a 2D skew transformation along the X- and the Y-axis |
skewX(angle) | Defines a 2D skew transformation along the X-axis |
skewY(angle) | Defines a 2D skew transformation along the Y-axis |
translate
The translate CSS property allows you to specify translation transforms individually and independently of the transform property. This maps better to typical user interface usage, and saves having to remember the exact order of transform functions to specify in the transform value.
Try it
Syntax
/* Keyword values */ translate: none; /* Single values */ translate: 100px; translate: 50%; /* Two values */ translate: 100px 200px; translate: 50% 105px; /* Three values */ translate: 50% 105px 5rem; /* Global values */ translate: inherit; translate: initial; translate: revert; translate: revert-layer; translate: unset;
Values
Specifies that no translation should be applied.
Formal definition
Initial value | none |
---|---|
Applies to | transformable elements |
Inherited | no |
Percentages | refer to the size of bounding box |
Computed value | as specified, but with relative lengths converted into absolute lengths |
Animation type | a transform |
Creates stacking context | yes |
Formal syntax
Examples
Translating an element on hover
This example shows how to use the translate property to move an element in three axes. The first box is moved along the X axis and the second box is moved along the X and Y axes. The third box is moved along the X, Y and Z axes and has the appearance of moving toward the viewer because of the addition of perspective to the parent element.
HTML
div class="wrapper"> div id="box1">translate Xdiv> div id="box2">translate X,Ydiv> div id="box3">translate X,Y,Zdiv> div>
CSS
.wrapper perspective: 100px; display: inline-flex; gap: 1em; > .wrapper > div width: 7em; line-height: 7em; text-align: center; transition: 0.5s ease-in-out; border: 3px dotted; > #box1:hover translate: 20px; > #box2:hover translate: 20px 20px; > #box3:hover translate: 5px 5px 30px; >
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Note: skew is not an independent transform value
Found a content problem with this page?
This page was last modified on Feb 21, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
transform
The transform CSS property lets you rotate, scale, skew, or translate an element. It modifies the coordinate space of the CSS visual formatting model.
Try it
If the property has a value different than none , a stacking context will be created. In that case, the element will act as a containing block for any position: fixed; or position: absolute; elements that it contains.
Warning: Only transformable elements can be transform ed. That is, all elements whose layout is governed by the CSS box model except for: non-replaced inline boxes, table-column boxes, and table-column-group boxes.
Syntax
/* Keyword values */ transform: none; /* Function values */ transform: matrix(1, 2, 3, 4, 5, 6); transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); transform: perspective(17px); transform: rotate(0.5turn); transform: rotate3d(1, 2, 3, 10deg); transform: rotateX(10deg); transform: rotateY(10deg); transform: rotateZ(10deg); transform: translate(12px, 50%); transform: translate3d(12px, 50%, 3em); transform: translateX(2em); transform: translateY(3in); transform: translateZ(2px); transform: scale(2, 0.5); transform: scale3d(2.5, 1.2, 0.3); transform: scaleX(2); transform: scaleY(0.5); transform: scaleZ(0.3); transform: skew(30deg, 20deg); transform: skewX(30deg); transform: skewY(1.07rad); /* Multiple function values */ transform: translateX(10px) rotate(10deg) translateY(5px); transform: perspective(500px) translate(10px, 0, 20px) rotateY(3deg); /* Global values */ transform: inherit; transform: initial; transform: revert; transform: revert-layer; transform: unset;
The transform property may be specified as either the keyword value none or as one or more values.
If perspective() is one of multiple function values, it must be listed first.
Values
One or more of the CSS transform functions to be applied. The transform functions are multiplied in order from left to right, meaning that composite transforms are effectively applied in order from right to left.
Specifies that no transform should be applied.
Accessibility concerns
Scaling/zooming animations are problematic for accessibility, as they are a common trigger for certain types of migraine. If you need to include such animations on your website, you should provide a control to allow users to turn off animations, preferably site-wide.
Also, consider making use of the prefers-reduced-motion media feature — use it to write a media query that will turn off animations if the user has reduced animation specified in their system preferences.
Formal definition
Initial value | none |
---|---|
Applies to | transformable elements |
Inherited | no |
Percentages | refer to the size of bounding box |
Computed value | as specified, but with relative lengths converted into absolute lengths |
Animation type | a transform |
Creates stacking context | yes |
translate
The translate CSS property allows you to transfer an element from one place to another along the X (horizontal) axis, the Y (vertical) axis, and the Z (depth) axes, similar to how you might think of moving an element using offsets, like top , bottom , left , and right .
The translate property works exactly the same as the translate() function that’s used with the transform property,
…where
/* Keyword value */ translate: none; /* Single value */ translate: 100px; translate: 50%; /* Two values */ translate: 100px 200px; translate: 50% 105px; /* Three values */ translate: 50% 105px 5rem; /* Global values */ translate: inherit; translate: initial; translate: revert; translate: unset;
- Initial: none
- Applies to: transformable elements
- Inherited: no
- Percentages: refer to the size of bounding box
- Computed value: as specified, but with relative lengths converted into absolute lengths
- Animation type: a transform
- Creates stacking context: yes
- none : This is used to specify that no translation should be applied.
- : This is a numerical value that determines how much an element is translated along an axis.
When we make use of a percentage value in translate property, that percentage refers to the element’s size, rather than the available space within the parent container.
translate does not change affect layout flow
It is important to know that the translate property does not cause other elements to flow around it, which is different than what we expect when using a transform, i.e. transform:translate() ). Notice below that when the box translates, it does not distort or affect its surrounding elements.
Transitions and animations
We can use translate in CSS transitions and animations. That means we can transition from one place to another, say, when the element is hovered.
Or, we can combine rotate with individual transforms in a CSS animation using @keyframes to make even more interesting effects:
If you look at the browser support closely, you may want to consider a fallback solution that works with other browsers until translate property gets full browser support. The transform property is a viable fallback alternative for an independent transform property, such as rotate .
For example, we could drop the rotate animation into an @supports block. This way, the animation only runs if the browser supports the translate property:
/* Using transform by default */ @keyframes loader < 0% < transform: translate(0); >100% < transform: translate(100% 50%); >> /* Use translate instead, but only if it is supported */ @supports (translate: 0deg) < @keyframes loader < 0% < translate: 0; >100% < translate: 100% 50%; >> >