CSS transform Property
The CSS transform property changes an element's visual appearance by translating, rotating, scaling, or skewing it in two-dimensional or three-dimensional space.
Transforms modify how an element is rendered without changing its original position in normal document flow. Multiple transform functions can be combined to create complex visual effects, animations, and interactive interfaces.
transform Syntax
The transform property accepts the keyword none, one or more transform functions, or a CSS-wide keyword.
Basic Syntax
.example {
transform: rotate(30deg);
}
Available Values
.example {
transform: none;
}
.example {
transform: translate(20px, 10px);
}
.example {
transform: rotate(45deg);
}
.example {
transform: scale(1.5);
}
.example {
transform: skew(15deg, 5deg);
}
.example {
transform: perspective(500px) rotateY(30deg);
}
Multiple Transform Functions
.example {
transform: translateX(30px) rotate(20deg) scale(1.2);
}
Transform functions are separated by spaces. Their order affects the final result.
CSS-Wide Keywords
.example {
transform: initial;
}
.example {
transform: inherit;
}
.example {
transform: unset;
}
.example {
transform: revert;
}
.example {
transform: revert-layer;
}
Invalid Examples
.example {
transform: rotate(45);
}
.example {
transform: translate(20);
}
.example {
transform: scale(50%);
}
.example {
transform: rotate(20deg), scale(1.2);
}
Rotation angles require valid angle units or zero, translation lengths require appropriate units unless zero, scaling factors are numbers, and transform functions must not be separated by commas.
Property Values
| Value | Description |
|---|---|
none |
Applies no transformation. |
translate() |
Moves an element horizontally, vertically, or both. |
rotate() |
Rotates an element around its transform origin. |
scale() |
Changes the rendered size of an element. |
skew() |
Slants an element along the horizontal or vertical axis. |
matrix() |
Represents a 2D transformation using six numerical values. |
translate3d() |
Moves an element along the X, Y, and Z axes. |
rotate3d() |
Rotates an element around a specified 3D axis. |
scale3d() |
Scales an element along the X, Y, and Z axes. |
matrix3d() |
Represents a 3D transformation using 16 numerical values. |
perspective() |
Applies perspective to the following 3D transform functions. |
| Multiple functions | Combines transformations in the specified order. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Transformable elements |
| Computed value | As specified, with lengths made absolute |
| Animation type | Transform list |
| Percentages | Relative to the dimensions of the transform reference box where applicable |
| Initial transform origin | Usually the center of the element's reference box |
| Specification | CSS Transforms Module Level 1 and Level 2 |
Note: Transformable elements generally include block-level and other supported layout boxes. Non-replaced inline elements, such as ordinary inline span elements, are not transformable.
How It Works
A transformation changes an element's visual coordinate system. The browser first calculates the element's layout and then applies the transformation when rendering it.
Step 1: Create an Element
<div class="transform-box">Transform Me</div>
Step 2: Apply a Transformation
.transform-box {
transform: translateX(30px);
}
The element is rendered 30 pixels to the right of its original position.
Step 3: Rotate the Element
.transform-box {
transform: rotate(20deg);
}
The element rotates 20 degrees around its transform origin.
Step 4: Combine Transformations
.transform-box {
transform: translateX(30px) rotate(20deg);
}
Step 5: Change the Transform Origin
.transform-box {
transform-origin: left center;
transform: rotate(20deg);
}
The element now rotates around the center of its left edge rather than its center.
Original Layout Space
Transformations do not change the space reserved for an element in normal document flow. The transformed element may overlap nearby content or extend outside its original box.
Translate Functions
Translation functions move an element along one or more axes without changing its original layout position.
translate()
.example {
transform: translate(40px, 20px);
}
Moves the element 40 pixels horizontally and 20 pixels vertically.
translateX()
.example {
transform: translateX(40px);
}
Moves the element along the X axis.
translateY()
.example {
transform: translateY(20px);
}
Moves the element along the Y axis.
translateZ()
.example {
transform: perspective(500px) translateZ(60px);
}
Moves the element along the Z axis. Perspective can make the resulting depth change visible.
translate3d()
.example {
transform: perspective(500px) translate3d(30px, 20px, 60px);
}
Percentage Translation
.example {
transform: translate(-50%, -50%);
}
Percentage translations are relative to the element's transform reference box, rather than the dimensions of its parent.
Negative Translation
.example {
transform: translateX(-30px);
}
Negative values move the element in the opposite direction along the specified axis.
When to Use Translation
Use translation for visual offsets, centering techniques, hover effects, and movement animations.
Rotate Functions
Rotation functions turn an element around its transform origin.
rotate()
.example {
transform: rotate(45deg);
}
Rotates the element by 45 degrees in the 2D plane.
rotateX()
.example {
transform: perspective(500px) rotateX(45deg);
}
Rotates the element around the X axis.
rotateY()
.example {
transform: perspective(500px) rotateY(45deg);
}
Rotates the element around the Y axis.
rotateZ()
.example {
transform: rotateZ(45deg);
}
Rotates the element around the Z axis, producing a rotation in the 2D plane.
rotate3d()
.example {
transform: perspective(500px) rotate3d(1, 1, 0, 45deg);
}
Rotates the element around a three-dimensional axis defined by its first three arguments.
Negative Rotation
.example {
transform: rotate(-30deg);
}
Angle Units
.example {
transform: rotate(0.25turn);
}
.example {
transform: rotate(100grad);
}
.example {
transform: rotate(1.57rad);
}
CSS supports degrees, turns, gradians, and radians for rotation angles.
When to Use Rotation
Use rotation for icons, decorative elements, interactive controls, and animated visual effects.
Scale Functions
Scaling functions change an element's rendered size along one or more axes.
scale()
.example {
transform: scale(1.5);
}
Scales the element to 150% of its original rendered size.
scaleX()
.example {
transform: scaleX(1.5);
}
Scales the element horizontally.
scaleY()
.example {
transform: scaleY(0.75);
}
Scales the element vertically.
scaleZ()
.example {
transform: perspective(500px) scaleZ(1.5) translateZ(60px);
}
Scales the Z coordinate of subsequent transformations. It may have no visible effect without a 3D transformation that uses depth.
scale3d()
.example {
transform: scale3d(1.2, 1.5, 1);
}
Scaling Down
.example {
transform: scale(0.8);
}
Reduces the rendered size to 80%.
Negative Scaling
.example {
transform: scaleX(-1);
}
Flips the element horizontally.
Scaling and Layout
Scaling changes the visual size but does not change the element's original layout dimensions.
When to Use Scaling
Use scaling for hover effects, emphasis, image previews, and interface animations.
Skew Functions
Skew functions slant an element by shifting its coordinates along one or both axes.
skew()
.example {
transform: skew(20deg, 10deg);
}
skewX()
.example {
transform: skewX(20deg);
}
Slants the element horizontally.
skewY()
.example {
transform: skewY(15deg);
}
Slants the element vertically.
Negative Skew
.example {
transform: skewX(-15deg);
}
Skewed Background
.skewed-panel {
transform: skewX(-10deg);
}
Correcting Skewed Text
.skewed-panel {
transform: skewX(-10deg);
}
.skewed-panel .content {
transform: skewX(10deg);
}
Applying an opposite skew to the content can visually counteract the parent's skew in simple cases.
When to Use Skewing
Use skewing for angled panels, decorative backgrounds, and stylized interface elements.
Matrix Functions
Matrix functions represent transformations using numerical matrices. They can combine translation, rotation, scaling, and skewing into a single mathematical operation.
matrix()
.example {
transform: matrix(1, 0, 0, 1, 30, 20);
}
This example translates the element 30 pixels horizontally and 20 pixels vertically.
2D Matrix Parameters
.example {
transform: matrix(a, b, c, d, tx, ty);
}
The first four values control the linear transformation, while the final two values control translation. The letters shown above are placeholders, not literal CSS values.
Identity Matrix
.example {
transform: matrix(1, 0, 0, 1, 0, 0);
}
The identity matrix leaves the element visually unchanged.
matrix3d()
.example {
transform: matrix3d(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
This is the identity matrix for 3D transformations.
When to Use Matrix Functions
Matrix functions are commonly generated by animation systems, graphics tools, and JavaScript libraries. Named transform functions are generally easier to read and maintain in hand-written CSS.
3D Transforms
Three-dimensional transformations allow elements to move, rotate, and scale along the X, Y, and Z axes.
Rotating in 3D
.example {
transform: perspective(600px) rotateY(45deg);
}
Rotating Around the X Axis
.example {
transform: perspective(600px) rotateX(45deg);
}
Moving Along the Z Axis
.example {
transform: perspective(600px) translateZ(80px);
}
Combining 3D Transformations
.example {
transform: perspective(600px) rotateX(20deg) rotateY(30deg) translateZ(40px);
}
Perspective
The perspective() function introduces a perspective projection that makes 3D depth visible.
.example {
transform: perspective(800px) rotateY(40deg);
}
Preserving 3D Space
.scene {
perspective: 800px;
}
.object {
transform-style: preserve-3d;
}
The transform-style property can allow nested transformed elements to retain their 3D relationships, subject to flattening rules.
Hiding Back Faces
.card-face {
backface-visibility: hidden;
}
This can hide the reverse side of an element when it faces away from the viewer.
When to Use 3D Transforms
Use 3D transforms for card flips, rotating interfaces, layered visual effects, and perspective-based animations.
Combining Transforms
Multiple transform functions can be included in one transform declaration.
Translate and Rotate
.example {
transform: translateX(30px) rotate(20deg);
}
Rotate and Scale
.example {
transform: rotate(20deg) scale(1.2);
}
Translate, Rotate, and Scale
.example {
transform: translate(20px, 10px) rotate(15deg) scale(1.1);
}
Combining 2D and 3D Functions
.example {
transform: perspective(600px) translateX(20px) rotateY(30deg);
}
One Declaration
When multiple transformations are needed, place them in the same declaration.
.example {
transform: translateX(20px) rotate(15deg);
}
Avoid Overwriting Earlier Transforms
.example {
transform: translateX(20px);
transform: rotate(15deg);
}
In this example, the second declaration replaces the first because both declarations target the same property within the same rule.
Transform Order
The order of transform functions matters because transformations are not generally interchangeable.
Translate Then Rotate
.example {
transform: translateX(50px) rotate(45deg);
}
Rotate Then Translate
.example {
transform: rotate(45deg) translateX(50px);
}
These declarations produce different results because the translation follows different coordinate directions.
How Functions Are Applied
Transform functions are multiplied into a transformation matrix from left to right. For a point being transformed, the rightmost function effectively acts first.
Scaling and Translation
.first {
transform: scale(2) translateX(20px);
}
.second {
transform: translateX(20px) scale(2);
}
In the first declaration, the translation is affected by the scaling transformation. In the second, the translation is applied after scaling in the resulting coordinate transformation.
Important Consideration
Changing transform order can affect the element's final position, orientation, and size. Test combinations carefully when building complex effects.
Transform Origin
The transform-origin property determines the point around which transformations such as rotation and scaling occur.
Default Origin
.example {
transform-origin: center center;
transform: rotate(30deg);
}
Top-Left Origin
.example {
transform-origin: top left;
transform: rotate(30deg);
}
Bottom-Right Origin
.example {
transform-origin: bottom right;
transform: scale(1.2);
}
Percentage Origin
.example {
transform-origin: 25% 75%;
transform: rotate(20deg);
}
3D Transform Origin
.example {
transform-origin: center center 50px;
transform: rotateY(30deg);
}
When to Change the Origin
Change the transform origin when an element should rotate or scale around an edge, corner, hinge, or another specific point.
Transforms and Layout
Transforms change an element's visual rendering but generally do not change the layout space reserved for it.
Normal Document Flow
.box {
transform: translateX(50px);
}
The element appears 50 pixels to the right, but nearby elements are laid out as though the translation had not occurred.
Scaling Does Not Resize Layout Space
.box {
width: 150px;
transform: scale(1.5);
}
The element is visually enlarged, but its layout width remains 150 pixels.
Transforms and Overflow
Transformed elements may extend beyond their original boxes and affect the scrollable overflow area.
Containing Blocks
A non-none transform establishes a containing block for absolutely positioned and fixed-positioned descendants.
Stacking Context
A non-none transform also creates a stacking context, affecting how the element and its descendants are layered.
Fixed Descendants
.container {
transform: translateZ(0);
}
.fixed-child {
position: fixed;
top: 0;
}
The transformed ancestor can establish the containing block for the fixed-positioned child, rather than the viewport.
When to Use Layout Properties Instead
Use Flexbox, Grid, margins, padding, or positioning properties when an element's location should influence the layout of surrounding content.
Related CSS Properties
The transform property works with several other CSS properties that control transformations and animations.
transform-origin
.example {
transform-origin: top left;
}
Specifies the point around which transformations occur.
transform-box
.example {
transform-box: border-box;
}
Defines the reference box used by transformations.
transform-style
.example {
transform-style: preserve-3d;
}
Controls whether descendants are flattened or retain their 3D positioning.
perspective
.example {
perspective: 800px;
}
Applies perspective to transformed descendants.
perspective-origin
.example {
perspective-origin: center center;
}
Specifies the viewpoint origin for the perspective effect.
backface-visibility
.example {
backface-visibility: hidden;
}
Controls whether the reverse side of a transformed element is visible.
translate
.example {
translate: 20px 10px;
}
Provides an individual translation property.
rotate
.example {
rotate: 30deg;
}
Provides an individual rotation property.
scale
.example {
scale: 1.2;
}
Provides an individual scaling property.
transition
.example {
transition: transform 0.3s ease;
}
Allows changes to the transform property to animate smoothly.
will-change
.example {
will-change: transform;
}
Hints that the transform property may change. Use this selectively rather than applying it to large numbers of elements.
Inheritance
The transform property is not inherited. Child elements do not automatically receive the parent's transform value.
Parent Element Example
.parent {
transform: rotate(15deg);
}
Child Element
.child {
transform: none;
}
The child is still visually affected by the parent's transformed coordinate system, even though its own transform value is none.
Explicit Inheritance
.child {
transform: inherit;
}
Restore the Initial Value
.child {
transform: initial;
}
This restores none.
Using unset
.child {
transform: unset;
}
Because the property is not inherited, unset restores the initial value.
Important Consideration
Nested transformations combine visually. A child can appear rotated, scaled, or moved because of its ancestor's transformation even when the child has no explicit transform.
Practical Examples
Centered Element
.container {
position: relative;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Hover Scaling
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: scale(1.05);
}
Rotating an Icon
.icon {
transform: rotate(45deg);
}
Sliding an Element
.slide-in {
transform: translateX(30px);
}
Flipping an Image Horizontally
.flipped-image {
transform: scaleX(-1);
}
Angled Panel
.angled-panel {
transform: skewX(-10deg);
}
3D Card Rotation
.card {
transform: perspective(600px) rotateY(25deg);
}
Animated Rotation
.spinner {
animation: spin 2s linear infinite;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Reduced Motion Alternative
@media (prefers-reduced-motion: reduce) {
.spinner {
animation: none;
}
}
Responsive Transformation
.feature-card {
transform: scale(1.1);
}
@media (max-width: 600px) {
.feature-card {
transform: none;
}
}
transform Example
This example compares translation, rotation, scaling, and skewing using four boxes. Each box starts with the same dimensions but receives a different transformation.
HTML Example
<div class="transform-demo">
<div class="transform-sample">
<strong>translateX(20px)</strong>
<div class="transform-stage">
<div class="transform-box move">Move</div>
</div>
</div>
<div class="transform-sample">
<strong>rotate(20deg)</strong>
<div class="transform-stage">
<div class="transform-box turn">Rotate</div>
</div>
</div>
<div class="transform-sample">
<strong>scale(1.3)</strong>
<div class="transform-stage">
<div class="transform-box grow">Scale</div>
</div>
</div>
<div class="transform-sample">
<strong>skewX(20deg)</strong>
<div class="transform-stage">
<div class="transform-box slant">Skew</div>
</div>
</div>
</div>
CSS Example
.transform-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
gap: 16px;
}
.transform-sample strong {
display: block;
margin-bottom: 8px;
}
.transform-stage {
display: flex;
align-items: center;
justify-content: center;
min-height: 130px;
padding: 16px;
border: 1px dashed #94a3b8;
background: #f8fafc;
}
.transform-box {
display: flex;
align-items: center;
justify-content: center;
width: 90px;
height: 55px;
background: #dbeafe;
border: 1px solid #2563eb;
color: #1e40af;
font-weight: bold;
}
.move {
transform: translateX(20px);
}
.turn {
transform: rotate(20deg);
}
.grow {
transform: scale(1.3);
}
.slant {
transform: skewX(20deg);
}
Understanding the Results
Each example uses the same starting box dimensions, but the transform property changes how the box appears.
Translation
The first box moves horizontally without changing its size or orientation.
Rotation
The second box rotates around its center.
Scaling
The third box becomes visually larger while retaining its original layout dimensions.
Skewing
The fourth box is slanted horizontally.
Experiment With Multiple Functions
.move {
transform: translateX(20px) rotate(15deg) scale(1.1);
}
Combining functions produces a different result from applying any one function alone.
Accessibility Considerations
Transforms can improve visual presentation, but they should not interfere with readability, navigation, or user interaction.
Respect Reduced Motion Preferences
Large or continuous transform animations may cause discomfort for users sensitive to motion.
@media (prefers-reduced-motion: reduce) {
.animated-element {
animation: none;
transition: none;
}
}
Maintain Readable Text
Extreme scaling, rotation, or skewing can make text difficult to read.
Avoid Unexpected Overlap
Transformed elements can overlap nearby content because their original layout space remains unchanged.
Preserve Keyboard Navigation
Visual transformations do not change the document's keyboard focus order.
Keep Interactive Controls Visible
Large translations can move controls outside the visible viewport or container.
Avoid Excessive Motion
Use transform animations purposefully and avoid unnecessary movement that distracts from content.
Test at High Zoom Levels
Verify that transformed elements remain usable when users increase browser zoom or text size.
Provide Non-Animated Alternatives
Important information should remain available even when animations are disabled.
Common Mistakes
Using Invalid Angle Values
.example {
transform: rotate(45);
}
Nonzero rotation angles require units such as deg, rad, or turn.
Using Percentages for scale()
.example {
transform: scale(150%);
}
The scale() function accepts numerical scaling factors, such as 1.5.
Separating Functions With Commas
.example {
transform: rotate(20deg), scale(1.2);
}
Separate transform functions with spaces.
Overwriting Earlier Transform Declarations
.example {
transform: rotate(20deg);
transform: scale(1.2);
}
The second declaration replaces the first. Combine the functions in one declaration.
Expecting Transforms to Change Layout Space
Transforms change visual rendering but generally do not change the space reserved in normal document flow.
Ignoring Transform Order
Changing the order of transform functions can change the final result.
Forgetting transform-origin
Rotation and scaling occur around the transform origin, which may need adjustment for the intended effect.
Transforming Non-Replaced Inline Elements
Ordinary inline elements such as span are not transformable unless their display behavior is changed to a transformable box.
.inline-icon {
display: inline-block;
transform: rotate(20deg);
}
Confusing transform With Individual Transform Properties
The translate, rotate, and scale properties can be used independently and have a defined composition order relative to transform.
Ignoring Stacking Contexts
A non-none transform creates a stacking context, which can affect overlapping elements and z-index.
Unexpected Fixed Positioning
Transformed ancestors can establish containing blocks for fixed-positioned descendants.
Using Transforms for Ordinary Layout
Flexbox and Grid are generally better choices when element placement should affect surrounding content.
Overusing will-change
Applying will-change: transform to too many elements can increase resource usage.
Ignoring Reduced Motion Preferences
Transform animations should account for users who prefer reduced motion.
Important Considerations
Keep the following points in mind when using transform:
- The property changes an element's visual rendering.
- The initial value is
none. - The property is not inherited.
- Transforms can translate, rotate, scale, and skew elements.
- Both 2D and 3D transformations are supported.
- Multiple transform functions can be combined.
- Transform function order affects the final result.
- Functions are separated by spaces, not commas.
- Transforms generally do not change normal document flow.
- Transformed elements can overlap surrounding content.
- Transforms can affect scrollable overflow.
- A non-
nonetransform creates a stacking context. - A transformed element can establish a containing block for positioned descendants.
- The
transform-originproperty controls the transformation origin. - The
transform-boxproperty controls the reference box. - Percentage translations depend on the transform reference box.
- Scaling changes visual size without changing layout dimensions.
- Negative scaling factors can flip elements.
- Perspective can make 3D depth visible.
- The
transform-styleproperty controls 3D rendering of descendants. - The
backface-visibilityproperty controls visibility of reverse-facing surfaces. - Transforms can be animated using transitions or keyframes.
- Individual
translate,rotate, andscaleproperties are also available. - Non-replaced inline elements are not transformable.
- Use layout properties when surrounding content should respond to positional changes.
- Test transformed elements at different viewport sizes.
- Respect reduced motion preferences for animated transformations.
Browser Support
The CSS transform property is widely supported across modern browsers. Common 2D transformations and standard 3D transform functions are broadly available, although advanced 3D rendering behavior and newer individual transform properties may have additional compatibility considerations.
For current compatibility information, visit Can I Use? and search for the CSS transform property.
Basic 2D Transformation
.example {
transform: rotate(30deg);
}
Multiple Transform Functions
.example {
transform: translateX(20px) scale(1.2);
}
3D Transformation
.example {
transform: perspective(600px) rotateY(30deg);
}
Feature Detection
@supports (transform: rotate(30deg)) {
.example {
transform: rotate(30deg);
}
}
Testing Recommendations
Test complex transformations in the browsers and devices your website supports. Pay particular attention to 3D perspective, nested transforms, overflow, stacking contexts, and animated effects.
Specifications
The transform property is defined in the CSS Transforms specifications.
CSS Transforms Module Level 1 - The transform Property
These specifications define transform functions, transformation matrices, 3D transformations, and related properties.
For additional documentation and examples, consult MDN Web Docs.
