CSS transforms
The CSS transforms module defines how elements styled with CSS can be transformed in two-dimensional or three-dimensional space.
The CSS transforms module defines how elements styled with CSS can be transformed in two-dimensional or three-dimensional space.
CSS transforms in action
Use the sliders in the example below to modify the translation, rotation, scale, and skew CSS transform properties of the cube in 3D space. As you move the cube through 3D space, notice the way it interacts with the element labeled z:0px, which is located at the 3D position (0, 0, 0).
```html hidden live-sample___transforms
z:0px
```css hidden live-sample___transforms
#allTransformFieldset {
border: none;
padding: 0;
margin-bottom: 4px;
accent-color: blue; /* or any color */
font-family:
"Inter", "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans",
"Droid Sans", "Helvetica Neue", sans-serif;
}
#allTransformFieldset > legend {
margin-bottom: 4px;
}
fieldset {
margin: 0;
}
legend {
font-weight: bold;
padding: 0;
}
#fieldsetSection {
display: flex;
flex-wrap: wrap;
align-items: start;
gap: 8px;
}
#outputSection {
width: 100%;
min-height: 400px;
background: linear-gradient(skyblue, khaki);
display: flex;
justify-content: center;
align-items: center;
position: relative;
overflow: clip;
}
#outputContainer {
position: absolute;
width: 100%;
height: 100%;
perspective: 200px;
display: flex;
justify-content: center;
align-items: center;
transform-style: preserve-3d;
pointer-events: none;
}
#z0 {
width: 50px;
height: 50px;
background: linear-gradient(
to right bottom,
rgb(223 223 223),
rgb(190 190 190)
);
transform: translateZ(0px);
position: absolute;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
color: black;
border-radius: 50%;
outline: 1px solid rgb(0 0 0 / 0.35);
pointer-events: all;
}
#perspectiveDot {
width: 4px;
height: 4px;
border-radius: 50%;
background-color: rgb(240 0 0 / 0.5);
transform: translate3d(-2px, -2px, 0px);
position: absolute;
}
#cube {
width: 100px;
height: 100px;
transform-style: preserve-3d;
transition: all 0.075s ease-out;
position: absolute;
pointer-events: all;
}
.face {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
position: absolute;
backface-visibility: inherit;
font-size: 60px;
color: white;
}
.front {
background: rgb(90 90 90 / 0.7);
transform: translateZ(50px);
}
.back {
background: rgb(0 210 0 / 0.7);
transform: rotateY(180deg) translateZ(50px);
}
.right {
background: rgb(210 0 0 / 0.7);
transform: rotateY(90deg) translateZ(50px);
}
.left {
background: rgb(0 0 210 / 0.7);
transform: rotateY(-90deg) translateZ(50px);
}
.top {
background: rgb(210 210 0 / 0.7);
transform: rotateX(90deg) translateZ(50px);
}
.bottom {
background: rgb(210 0 210 / 0.7);
transform: rotateX(-90deg) translateZ(50px);
}
.transformFieldset {
margin: 0;
}
.controlsContainer {
display: flex;
flex-direction: column;
align-items: start;
gap: 4px;
}
.controlsContainer {
width: 100%;
}
.controlsContainer > div {
display: flex;
flex-direction: row;
align-items: center;
gap: 4px;
}
.controlsContainer > div > label {
display: flex;
flex-direction: row;
align-items: center;
gap: 4px;
}
button {
font-size: 18px;
border-radius: 50%;
border: #cccccc solid 1px;
padding: 0;
width: 26px;
height: 26px;
margin-left: 4px;
}
input[type="range"] {
width: 172px;
}
output {
width: 3em;
}
```js hidden live-sample___transforms allTransformFieldset .querySelectorAll("input[type='range']") .forEach((rangeInput) => { // Event listeners for when the range inputs change rangeInput.addEventListener("input", (el) => { updateTransform(); });
// Reset the relevant transform setting when the range input is double clicked
rangeInput.addEventListener("dblclick", (el) => {
resetInput(el.target);
updateTransform();
});
});
// Event listeners for when checkbox inputs change allTransformFieldset .querySelectorAll("input[type='checkbox']") .forEach((checkboxInput) => { checkboxInput.addEventListener("input", (el) => { updateTransform(); }); });
// "Reset All" button event listener resetAllButton.addEventListener("click", () => { allTransformFieldset.querySelectorAll("input").forEach((input) => { resetInput(input); }); updateTransform(); });
// Section reset button event listeners allTransformFieldset .querySelectorAll(".resetSectionButton") .forEach((resetSectionButton) => { resetSectionButton.addEventListener("click", (el) => { let allRanges = el.target.parentElement.parentElement.querySelectorAll( "input[type='range']", ); allRanges.forEach((range) => { resetInput(range); });
let allCheckboxes =
el.target.parentElement.parentElement.querySelectorAll(
"input[type='checkbox']",
);
allCheckboxes.forEach((check) => {
resetInput(check);
});
updateTransform();
});
});
function resetInput(inputEl) {
if (!inputEl) {
console.warn(inputEl \${inputEl}` is falsy!`);
console.trace();
return;
}
const defaultValue = inputEl.getAttribute("data-default"); if (inputEl.getAttribute("type") === "checkbox") { inputEl.checked = defaultValue === "checked"; } else { inputEl.value = defaultValue || "0"; } }
function updateOutputs() {
translateXOutput.value = ${translateXRange.value}px;
translateYOutput.value = ${translateYRange.value}px;
translateZOutput.value = ${translateZRange.value}px;
rotateXOutput.value = ${rotateXRange.value}°;
rotateYOutput.value = ${rotateYRange.value}°;
rotateZOutput.value = ${rotateZRange.value}°;
scaleXOutput.value = ${scaleXRange.value}x;
scaleYOutput.value = ${scaleYRange.value}x;
scaleZOutput.value = ${scaleZRange.value}x;
skewXOutput.value = ${skewXRange.value}°;
skewYOutput.value = ${skewYRange.value}°;
perspectiveOutput.value = ${perspectiveRange.value}px;
perspectiveOriginXOutput.value = ${perspectiveOriginXRange.value}%;
perspectiveOriginYOutput.value = ${perspectiveOriginYRange.value}%;
}
function updateTransform() { updateOutputs();
cube.style.transform = translate3d(${translateXRange.value}px,
${translateYRange.value}px,
${translateZRange.value}px)
rotateX(${rotateXRange.value}deg)
rotateY(${rotateYRange.value}deg)
rotateZ(${rotateZRange.value}deg)
scale3d(${scaleXRange.value},
${scaleYRange.value},
${scaleZRange.value})
skewX(${skewXRange.value}deg)
skewY(${skewYRange.value}deg);
cube.style.backfaceVisibility = ${backfaceVisibilityCheckbox.checked ? "visible" : "hidden"};
outputContainer.style.perspective = ${perspectiveRange.value}px;
outputContainer.style.perspectiveOrigin = ${perspectiveOriginXRange.value}% ${perspectiveOriginYRange.value}%;
perspectiveDot.style.top = ${perspectiveOriginYRange.value}%;
perspectiveDot.style.left = ${perspectiveOriginXRange.value}%;
}
updateTransform();
```
You can also use the perspective slider to modify the perspective property of the cube's container, which determines the distance between you and the z=0 plane.
The perspective-origin sliders determine where you, the viewer, are looking into the 3D space for purposes of determining the view's vanishing point. This vanishing point is indicated by a small red dot. You can imagine modifying these sliders as physically moving your head up, down, left, and right to see different parts of the cube without moving the cube itself.
The backface-visibility checkbox determines whether the cube's back faces are set to visible or hidden.
The cube in the above example is comprised of six <div> elements, all of which are styled with CSS to create the cube's faces. The cube is not drawn using a 2D or 3D canvas context, so you can inspect the faces of the cube with your browser's developer tools as you would inspect any other DOM element. Try using your browser's developer tools element picker to inspect different faces of the cube as you transform its position and rotation.
[!NOTE] The order in which transformations, including 3D rotations, are applied affects the resultant transformation. In the above example, transforms are translated, scaled, rotated, then skewed. The rotations are applied in the order X → Y → Z.
Reference
Properties
- backface-visibility
- perspective
- perspective-origin
- rotate
- scale
- transform
- transform-box
- transform-origin
- transform-style
- translate
Functions
- matrix()
- matrix3d()
- perspective()
- rotate()
- rotate3d()
- rotateX()
- rotateY()
- rotateZ()
- scale()
- scale3d()
- scaleX()
- scaleY()
- scaleZ()
- skew()
- skewX()
- skewY()
- translate()
- translate3d()
- translateX()
- translateY()
- translateZ()
Data types
- <transform-function>
Guides
- Using CSS transforms
- : Step-by-step tutorial about how to transform elements styled with CSS.
- Coordinate systems
- : Describes the way pixel locations are defined in the CSS object model.
- Performance fundamentals: Use CSS transforms
- : An overview of web performance fundamentals, including how CSS transforms can improve performance.
- Matrix math for the web
- : Describes how object transformations can be represented by mathematical matrices.
Related concepts
- CSS Properties:
- animation
- background-position
- clip
- clip-path
- contain
- content-visibility
- isolation
- mask
- mask-border-source
- mask-image
- mix-blend-mode
- opacity
- overflow
- transition
- visibility
- Data types:
- angle
- length-percentage
- length
- number
- percentage
- position
- Glossary terms:
- Interpolation
- Stacking context
- SVG concepts:
<animate>element<animateTransform>element<set>elementtransformelement
Specifications
See also
- Basic SVG transformations tutorial
- CSS animations module
- CSS transitions module