So far, every visual change we have made to an element (a bigger width, a new margin, a different position) has also affected the layout: neighbouring elements moved to make room. Sometimes that's exactly what we want. But for a button that lifts slightly on hover, an icon that spins, or a card that flips over, pushing the rest of the page around would be a disaster.
The transform property lets you move, rotate, scale and skew an element visually, without changing the space it occupies in the layout. Combined with Transitions from the previous chapter, it powers most of the smooth motion you see on modern websites.
What is a Transform?
A transform changes how an element is drawn (its position, rotation, size or slant) by applying a geometric transformation to its coordinate system. It does not change the element's box in the normal flow.
Think of the layout as a seating plan in an exam hall. Each student (element) has a fixed seat. A transform is the student leaning, turning or standing up: they look different, but their seat is still reserved and nobody else sits in it.
.badge {
transform: rotate(-10deg);
}The badge appears tilted 10 degrees anticlockwise, but every element around it stays exactly where it was.
Transforms Don't Affect Layout
This is the single most important fact about transforms, so let's see it clearly:
<div class="row">
<div>1</div>
<div class="moved">2</div>
<div>3</div>
</div>.row {
display: flex;
gap: 10px;
}
.row div {
width: 80px;
height: 50px;
background: orange;
}
.row .moved {
background: purple;
transform: translate(40px, 30px);
}Output: box 2 is drawn 40px to the right and 30px lower, overlapping box 3. Boxes 1 and 3 do not move, and the gap where box 2 "should" be is still there.
Without transform: With transform: translate(40px, 30px)
[ 1 ] [ 2 ] [ 3 ] [ 1 ] [ ] [ 3 ] ← box 2's original space stays empty
[ 2 ] ← box 2 drawn here, overlapping box 3Compare with position: relative; left: 40px; top: 30px; from Positioning: that also keeps the original space, but changing left/top during an animation forces layout work on every frame, while a transform does not. That's why animations use transforms.
Which elements can be transformed? Almost all of them, except non-replaced inline elements like a plain
<span>or<a>with the defaultdisplay: inline. A transform on them is silently ignored. Give themdisplay: inline-block(or block, flex...) first.
2D Transform Functions
| Function | What it does | Example |
|---|---|---|
translate(x, y) | Moves the element | translate(20px, -10px) |
translateX(x), translateY(y) | Moves along one axis | translateY(-4px) |
rotate(angle) | Rotates around the transform origin | rotate(45deg) |
scale(sx, sy) | Resizes (1 = normal size) | scale(1.1) |
scaleX(sx), scaleY(sy) | Resizes along one axis | scaleX(-1) (mirror) |
skew(ax, ay) | Slants the element | skewX(20deg) |
matrix(a, b, c, d, e, f) | Any 2D transform as a matrix | Usually generated, not hand-written |
translate()
.box {
transform: translate(50px, 20px); /* 50px right, 20px down */
}In CSS the y-axis points down, so a positive y moves the element down and a negative y moves it up. A single value, translate(50px), means x only.
The most useful detail: percentages in translate() are relative to the element's own size, not its parent's. translateX(100%) moves an element by exactly its own width. That gives the classic centring trick:
.hero {
position: relative;
height: 300px;
}
.hero-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}Step by step:
top: 50%; left: 50%puts the element's top-left corner at the centre of.hero(these percentages are of the parent).translate(-50%, -50%)shifts the element back by half its own width and height.Now the element's centre sits on the parent's centre, whatever size the text is.
Today, flexbox or grid (place-items: center) is usually simpler for centring, but you will still meet this pattern everywhere, especially for overlays and modals.
rotate()
.icon {
transform: rotate(45deg);
}Positive angles rotate clockwise (because the y-axis points down). Angle units: deg (360 in a circle), turn (1 in a circle), rad (2π in a circle) and grad (400 in a circle). So rotate(0.25turn) = rotate(90deg).
scale()
.card:hover {
transform: scale(1.05); /* 5% bigger in both directions */
}
.flip-horizontal {
transform: scaleX(-1); /* mirror image */
}One number scales both axes equally; two numbers scale x and y separately. scale(1) means no change, scale(0.5) means half size, and a negative value flips the element. A 200px wide box with scale(1.5) is drawn 300px wide, but still takes up 200px in the layout.
Scaling also scales everything inside: text, borders and shadows. Scaling text up a lot can make it look slightly blurry, because it may have been drawn at the original size first.
skew()
.ribbon {
transform: skewX(-15deg);
}skewX() slants vertical edges, turning a rectangle into a parallelogram. It is used for decorative shapes such as slanted ribbons and section dividers.
transform-origin: The Pivot Point
Rotation and scaling happen around a fixed point called the transform origin. By default it is the centre of the element (50% 50%).
.door {
transform-origin: left center; /* hinge on the left edge */
transform: rotate(-30deg);
}
.dropdown {
transform-origin: top; /* grow downward from the top edge */
transform: scaleY(0);
}With the default origin, rotate(45deg) spins the element around its own centre, like a wheel on an axle. With transform-origin: top left, the top-left corner stays pinned and the rest of the element swings around it, like a door on a hinge.
The value can be keywords (top, left, center...), lengths (20px 10px) or percentages of the element's size. translate() is not affected by the origin, because moving doesn't need a pivot.
Combining Transforms: Order Matters!
You can list several functions, separated by spaces:
transform: translate(150px) rotate(90deg);Is that the same as rotate(90deg) translate(150px)? No. Each function transforms the coordinate system for the ones after it.
/* both boxes start at the same place: 100px × 50px */
.a { transform: translate(150px) rotate(90deg); }
.b { transform: rotate(90deg) translate(150px); }Box A: first moves 150px to the right, then rotates 90° in place. Its centre ends up 150px to the right of the start.
Box B: first rotates 90°, which also rotates its x-axis to point downward. Then
translate(150px)moves 150px along that new x-axis, so its centre ends up 150px below the start.
Box A: translate → rotate Box B: rotate → translate
[start]────150px───►▮ [start]
│
150px
│
▼
▮We checked this in Chrome by placing the two boxes side by side. Box A's centre moved from (150, 105) to (300, 105): 150px to the right. Box B's centre moved from (500, 105) to (500, 255): 150px down. Both ended up rotated by 90°.
Rule of thumb: read the functions left to right, and imagine each one moving the element's own coordinate grid (its axes and origin) before the next function runs.
A common bug: overriding instead of adding
.icon { transform: translate(-50%, -50%); }
.icon:hover { transform: rotate(20deg); } /* ❌ loses the translate! */transform is one property, so the hover rule replaces the whole list and the icon jumps off-centre. You must repeat the parts you want to keep:
.icon:hover { transform: translate(-50%, -50%) rotate(20deg); }Individual Transform Properties
To avoid that bug, modern CSS has separate translate, rotate and scale properties (note: properties, not functions). They have been supported in all major browsers since 2022.
.icon {
translate: -50% -50%;
}
.icon:hover {
rotate: 20deg; /* the translate stays as it was */
scale: 1.2;
}Notice the syntax: the values are separated by spaces and there are no brackets: translate: 10px 20px; not translate: translate(10px, 20px);.
These properties can also be transitioned and animated independently, for example a different duration for scale and rotate.
Their order is fixed: the browser always applies translate, then rotate, then scale, and then anything in the transform property. So the following two are equivalent:
.x { translate: 50px; rotate: 45deg; scale: 2; }
.y { transform: translate(50px) rotate(45deg) scale(2); }If you need a different order, use the transform property.
The Maths Behind It: Transformation Matrices
This section is useful for computer graphics courses and for understanding what DevTools shows you.
Every 2D transform can be written as one 3×3 matrix. CSS shows it as matrix(a, b, c, d, e, f), which stands for:
| a c e | | x | | a·x + c·y + e |
| b d f | × | y | = | b·x + d·y + f |
| 0 0 1 | | 1 | | 1 |The third row and column let a translation (e, f) be included in a matrix multiplication. These are called homogeneous coordinates.
| Transform | Matrix |
|---|---|
translate(tx, ty) | matrix(1, 0, 0, 1, tx, ty) |
scale(sx, sy) | matrix(sx, 0, 0, sy, 0, 0) |
rotate(θ) | matrix(cos θ, sin θ, −sin θ, cos θ, 0, 0) |
skewX(α) | matrix(1, 0, tan α, 1, 0, 0) |
The browser reports this matrix as the computed value of transform. In Chrome:
el.style.transform = "rotate(30deg)";
console.log(getComputedStyle(el).transform);
el.style.transform = "translate(10px, 20px) scale(2)";
console.log(getComputedStyle(el).transform);Output:
matrix(0.866025, 0.5, -0.5, 0.866025, 0, 0)
matrix(2, 0, 0, 2, 10, 20)Check the first one: cos 30° ≈ 0.866 and sin 30° = 0.5. ✔
Worked example: rotating a point
Where does the point (50, 0) go under rotate(90deg) (relative to the transform origin)?
cos 90° = 0, sin 90° = 1, so the matrix is
matrix(0, 1, −1, 0, 0, 0).x′ = a·x + c·y + e = 0·50 + (−1)·0 + 0 = 0
y′ = b·x + d·y + f = 1·50 + 0·0 + 0 = 50
The point moves from "50 to the right" to "50 below". On screen, where y grows downward, that is a clockwise turn, which confirms that positive angles rotate clockwise in CSS.
Combining transforms means multiplying matrices. Since matrix multiplication is not commutative (A × B ≠ B × A in general), the order of transform functions matters, which is exactly what we saw with Box A and Box B.
3D Transforms
Elements can also rotate and move in three dimensions. The z-axis points out of the screen towards you.
| Function | Meaning |
|---|---|
rotateX(angle) | Tilt forward/backward around the horizontal axis |
rotateY(angle) | Turn left/right around the vertical axis (like a door or a page) |
rotateZ(angle) | Same as the 2D rotate() |
translateZ(z) | Move towards (+) or away from (−) the viewer |
translate3d(), scale3d(), rotate3d(), matrix3d() | Full 3D versions |
Perspective
Without perspective, a 3D rotation looks flat: rotateY(60deg) just makes the element narrower. Perspective adds depth: nearer parts look bigger, farther parts smaller.
/* Option 1: on the parent, shared by all 3D children */
.scene {
perspective: 800px;
}
/* Option 2: as a function, on the element itself */
.tile {
transform: perspective(800px) rotateY(60deg);
}The value is the distance between the viewer and the screen. A small value (like 300px) means you are very close, so the 3D effect is dramatic. A large value (1000px or more) gives a subtle effect. In our test, a square with rotateX(45deg) became a strongly tapered trapezoid with perspective(300px) and only a slight one with perspective(1000px). When you use the function form, perspective() must come first in the list.
Project: a flip card
A flashcard that shows a question and flips over on hover to reveal the answer:
<div class="card">
<div class="card-inner">
<div class="card-front">What is CSS?</div>
<div class="card-back">Cascading Style Sheets</div>
</div>
</div>.card {
width: 200px;
height: 120px;
perspective: 800px; /* 1. depth for the 3D scene */
}
.card-inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 600ms ease;
transform-style: preserve-3d; /* 2. children live in the same 3D space */
}
.card:hover .card-inner {
transform: rotateY(180deg); /* 3. flip the whole card */
}
.card-front,
.card-back {
position: absolute;
inset: 0; /* both faces stacked on top of each other */
display: grid;
place-items: center;
border-radius: 12px;
backface-visibility: hidden; /* 4. hide a face when it's turned away */
}
.card-front {
background: #2563eb;
color: white;
}
.card-back {
background: #f59e0b;
transform: rotateY(180deg); /* 5. the back starts already flipped */
}Output: the card shows the blue front "What is CSS?". On hover it turns like a page; at 60° you see the front narrowed and in perspective, and at 180° the orange back "Cascading Style Sheets" faces you.
Why each numbered line is needed:
perspectiveon the outer card gives depth to the rotation.transform-style: preserve-3d: by default, children of a transformed element are flattened into its plane. This keeps the two faces as real 3D objects.Rotating the inner wrapper rotates both faces together.
backface-visibility: hidden: when a face is turned away from you, it disappears instead of showing mirror-image text.The back starts rotated 180°, so when the card turns 180°, the back has turned 360° and faces you.
Side Effects You Must Know
Any transform other than none (and likewise the individual translate, rotate, scale properties) has two side effects that cause confusing bugs:
It creates a new stacking context. The element and its children are layered as a group, which can change how
z-indexbehaves (see z-index & Stacking Context).It becomes the containing block for
position: fixeddescendants. A "fixed" child is then positioned and scrolls relative to the transformed ancestor, not the viewport.
.wrapper {
transform: translate(0); /* looks harmless... */
}
.wrapper .modal {
position: fixed;
top: 0;
left: 0; /* ...but this is now the wrapper's top-left, not the screen's */
}In our test, the "fixed" element appeared at the top-left corner of the transformed wrapper rather than the window. If a fixed modal or header suddenly behaves strangely, check its ancestors for transform (and also filter, perspective and will-change: transform, which have the same effect).
Transforms + Transitions
Transforms are the best partners for transitions, because the browser can animate them without redoing layout:
.card {
transition: transform 200ms ease-out, box-shadow 200ms ease-out;
}
.card:hover {
transform: translateY(-6px) scale(1.02);
box-shadow: 0 12px 24px rgb(0 0 0 / 0.15);
}Output: the card rises slightly and grows by 2%, while its shadow deepens, which gives the feeling it has lifted off the page.
When transitioning between two transform lists, the browser interpolates function by function if both lists have the same functions in the same order. If they don't match, it falls back to interpolating the matrices, which can give odd paths (for example, a rotation that doesn't take the direction you expect). Keep the same function list in both states.
Common Mistakes
Transforming an inline element. A plain
<span>ignorestransform; make itinline-block.Expecting neighbours to move. Transforms don't change layout; use margins or sizing if you need space.
Overriding the whole transform on hover and losing an earlier
translate(-50%, -50%). Repeat it, or use the individualtranslate/rotate/scaleproperties.Getting the order wrong.
rotate() translate()moves along the rotated axis.Writing
translate: translate(10px)or commas in the individual property. It'stranslate: 10px 20px.Forgetting
preserve-3dorbackface-visibilityin 3D effects, giving flat or mirrored results.Surprise
position: fixedbehaviour inside a transformed ancestor.
Interview / Exam Questions
1. What is the difference between transform: translate() and position: relative with top/left?
Both move the element visually and keep its original space in the flow. But top/left are layout properties, so animating them recalculates layout every frame, while translate() is applied at the compositing stage and is much cheaper to animate. Also, percentages in translate() refer to the element's own size, while percentages in top/left refer to the containing block.
2. How does translate(-50%, -50%) help centre an element?
With position: absolute; top: 50%; left: 50%, the element's top-left corner is at the parent's centre. translate(-50%, -50%) moves it back by half of its own width and height, so its centre lands on the parent's centre, regardless of its size.
3. Are transform: rotate(45deg) translate(100px) and transform: translate(100px) rotate(45deg) the same?
No. In the first, the rotation turns the element's axes, and then it moves 100px along the rotated (diagonal) x-axis. In the second, it moves 100px straight right and then rotates in place. Transforms compose like matrix multiplication, which is not commutative.
4. Write the matrix for rotate(θ). Where does (1, 0) go for θ = 90°?
matrix(cos θ, sin θ, −sin θ, cos θ, 0, 0), i.e. x′ = x·cos θ − y·sin θ and y′ = x·sin θ + y·cos θ. For θ = 90°, (1, 0) → (0, 1), which is downward on screen, so positive angles appear clockwise.
5. What does transform-origin do? Does it affect translate()?
It sets the fixed point around which rotation, scaling and skewing happen (default: the element's centre). It doesn't change the result of a pure translation.
6. Why might a position: fixed element scroll with the page instead of staying fixed?
One of its ancestors has a transform (or filter, perspective and some other properties). That ancestor becomes the containing block for fixed descendants, so the element is fixed relative to the ancestor rather than the viewport.
7. What do perspective and backface-visibility do in a card flip?
perspective sets how far the viewer is from the screen, creating depth (smaller value = stronger effect). backface-visibility: hidden hides a face when it is rotated away from the viewer, so only one side of the card is visible at a time.
Summary
transformmoves, rotates, scales or skews an element visually without affecting layout.Main functions:
translate(),rotate(),scale(),skew(),matrix(). Percentages intranslate()refer to the element's own size.transform-originsets the pivot (default: centre).The order of functions matters; each one transforms the coordinate system for the next. Mathematically it is matrix multiplication.
The individual
translate,rotate,scaleproperties avoid overriding bugs and are applied in that fixed order.3D needs
perspective, and oftentransform-style: preserve-3dandbackface-visibility: hidden.Transforms create a stacking context and a containing block for fixed descendants.
Transforms plus transitions = smooth, cheap motion.
Transitions only move between two states. In Animations & Keyframes we'll learn to create multi-step animations that can run on their own, loop forever and combine transforms in sequence.