2D Transformations
By the end of this lesson you will be able to predict exactly where a transformed element ends up. You will use every 2D transform function, combine the individual translate, rotate and scale properties with transform, explain why order changes the result, place transform-origin precisely, and know what transforms do and do not change on the page.
CSS Transform gave you the overview. This lesson goes deeper into each idea, so read that one first if you have not.
The 2D coordinate system
Every element has its own invisible sheet of graph paper. The origin sits at the transform origin, which is the centre of the element by default. On this sheet:
- The x axis points to the right.
- The y axis points down, not up as in school maths. So
translateY(20px)moves an element down. - Positive angles turn clockwise.
rotate(90deg)turns the top of the element to face right.
Think of seats in a cinema hall. Row numbers grow as you walk towards the screen at the front, which is "down" on the seating chart. CSS counts the same way: y grows downwards, just like the page itself.
Every 2D transform function
| Function | What it does | Notes |
|---|---|---|
translate(x, y), translateX(x), translateY(y) | Moves | Percentages use the element's own size |
rotate(a) | Turns | Units: deg, turn, rad, grad |
scale(x, y), scaleX(x), scaleY(y) | Resizes | One value scales both axes; a negative value mirrors |
skew(ax, ay), skewX(a), skewY(a) | Slants | skewX(-15deg) leans forward like italic text |
matrix(a, b, c, d, e, f) | Any 2D transform | What the browser stores internally |
scaleX(-1) is a handy trick. It mirrors an element left to right, so one arrow icon can point both ways.
Step 1 shows each function on a small signboard and prints the computed value of each transform. The computed value is what the browser actually stores after it has worked out your CSS.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>2D functions</title>
<style>
body { font-family: system-ui, sans-serif; margin: 24px; }
.row { display: flex; flex-wrap: wrap; gap: 28px; }
.sign {
width: 110px;
padding: 14px 6px;
border-radius: 6px;
background: #b45309;
color: white;
font-weight: bold;
text-align: center;
}
.move { transform: translateY(-10px); }
.turn { transform: rotate(-0.02turn); }
.squash { transform: scaleY(0.7); }
.mirror { transform: scaleX(-1); }
.lean { transform: skewX(-15deg); }
code { display: block; margin-top: 6px; font-size: 0.7rem; color: #334155; }
</style>
</head>
<body>
<div class="row">
<div><div class="sign move">Sharma Chai</div><code></code></div>
<div><div class="sign turn">Sharma Chai</div><code></code></div>
<div><div class="sign squash">Sharma Chai</div><code></code></div>
<div><div class="sign mirror">Sharma Chai</div><code></code></div>
<div><div class="sign lean">Sharma Chai</div><code></code></div>
</div>
<script>
document.querySelectorAll('.sign').forEach(sign => {
sign.nextElementSibling.textContent = getComputedStyle(sign).transform;
});
</script>
</body>
</html>Under each sign you will see a matrix(). The mirrored one shows matrix(-1, 0, 0, 1, 0, 0). The first four numbers describe scaling, rotation and skew, and the last two are the movement in pixels. You will rarely write matrix() yourself, but you will see it in DevTools and in JavaScript, so it helps to recognise it.
Why order matters
A transform list is applied from left to right, and each function changes the graph paper for the functions after it. Picture giving directions to a friend at Jaipur railway station: "turn left, then walk 100 metres" ends somewhere very different from "walk 100 metres, then turn left". Transforms work the same way.
The overview lesson showed this with rotate and translate. Scale has the same effect, and it surprises people more. After scale(2), the graph paper itself is twice as big, so a later translateX(40px) covers 80 real pixels.
Step 2 makes this visible. Each dashed outline marks where the box would be without any transform.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Transform order</title>
<style>
body { font-family: system-ui, sans-serif; margin: 24px; }
.stage { position: relative; height: 110px; margin-bottom: 8px; border-bottom: 1px solid #e2e8f0; }
.ghost, .box { position: absolute; top: 35px; left: 40px; width: 40px; height: 40px; }
.ghost { border: 2px dashed #94a3b8; box-sizing: border-box; }
.box { background: rgb(37 99 235 / 0.8); }
.a { transform: translateX(40px) scale(2); }
.b { transform: scale(2) translateX(40px); }
.c { scale: 2; translate: 40px 0; }
.d { rotate: 30deg; transform: translateX(60px); background: rgb(219 39 119 / 0.8); }
p { margin: 0; font-size: 0.85rem; }
</style>
</head>
<body>
<p>A: translateX(40px) scale(2)</p>
<div class="stage"><div class="ghost"></div><div class="box a"></div></div>
<p>B: scale(2) translateX(40px)</p>
<div class="stage"><div class="ghost"></div><div class="box b"></div></div>
<p>C: scale: 2; translate: 40px 0;</p>
<div class="stage"><div class="ghost"></div><div class="box c"></div></div>
<p>D: rotate: 30deg; transform: translateX(60px);</p>
<div class="stage"><div class="ghost"></div><div class="box d"></div></div>
</body>
</html>What you should see:
- Box A moves 40px right, then doubles in size around its new centre.
- Box B doubles first, so its 40px move becomes 80px on screen. It sits further right than A.
- Box C matches A, even though
scaleis written beforetranslate. The next section explains why. - Box D moves down and to the right, because its
translateXruns along an x axis that is already turned by 30 degrees.
A helpful habit: write translate first, then rotate, then scale. That order matches what most people expect: "go there, turn, then resize in place".
Individual properties meet transform
The individual properties translate, rotate and scale have been Baseline since 2022. They take values without brackets, and each one can be changed or transitioned on its own.
The browser always combines them in one fixed order, whatever order you write them in:
translaterotatescale- then every function in
transform, from left to right.
So these two rules give the same result:
.badge-one {
scale: 1.2;
rotate: 10deg;
translate: 0 -8px;
}
.badge-two {
transform: translate(0, -8px) rotate(10deg) scale(1.2);
}That is what happened to box C. And in box D, rotate ran first, then transform added its move on the turned axis.
When should you use which? Use the individual properties for anything that changes on hover or in a transition, because changing one never resets the others. Use transform when you need skew(), when order must differ from translate-rotate-scale, or when you need a final adjustment after the others. There is no individual skew property.
transform-origin in depth
transform-origin moves the pin that rotation, scaling and skew happen around. Translation is not affected, because a move is the same wherever the pin is.
It takes one or two values (x then y), and an optional third length for 3D:
- Keywords:
left,center,right,top,bottom.transform-origin: topmeans the middle of the top edge. - Percentages of the element's own box:
0% 100%is the bottom left corner. - Lengths:
20px 10px, measured from the top left corner. Values outside the box are allowed, so the pin can sit outside the element.
One trap: for shapes inside an SVG, the default origin is the top left of the SVG canvas (0 0), not the shape's centre. Set it yourself when you rotate SVG parts.
Step 3 uses an origin below each element to fan out a paint shade card, the kind you see at a hardware shop in Lucknow.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shade card fan</title>
<style>
body { font-family: system-ui, sans-serif; margin: 24px; }
.fan { position: relative; width: 60px; height: 200px; margin: 40px auto; }
.strip {
position: absolute;
inset: 0;
border-radius: 8px;
border: 2px solid white;
transform-origin: 50% 90%; /* pin near the bottom */
rotate: 0deg;
transition: rotate 400ms ease-out;
}
.fan:hover .strip,
.fan:focus-visible .strip {
rotate: calc(var(--i) * 15deg - 45deg);
}
</style>
</head>
<body>
<div class="fan" tabindex="0" aria-label="Paint shade card">
<div class="strip" style="--i: 0; background: #7c2d12"></div>
<div class="strip" style="--i: 1; background: #c2410c"></div>
<div class="strip" style="--i: 2; background: #ea580c"></div>
<div class="strip" style="--i: 3; background: #f59e0b"></div>
<div class="strip" style="--i: 4; background: #84cc16"></div>
<div class="strip" style="--i: 5; background: #0d9488"></div>
<div class="strip" style="--i: 6; background: #1d4ed8"></div>
</div>
<p>Hover over the card or press Tab to open the fan.</p>
</body>
</html>All seven strips turn around the same point near their bottom, so they spread like a hand fan from -45 to 45 degrees. Change transform-origin to 50% 50% and run again: the strips now spin around their middles and the fan falls apart.
Transforms do not change layout
A transform changes how an element is painted, not the space it takes. In practice that means:
- Neighbours never move. A scaled card overlaps the cards beside it instead of pushing them.
- Clicks follow the drawing. You click a transformed button where you see it, not where its layout box is.
- Scroll areas can grow. A box translated past the edge of a scrolling container can add scrollbars, because transforms count towards the scrollable area.
- A transform creates a stacking context, and it becomes the containing block for
position: fixedandabsolutedescendants. See Z-Index & Stacking Context. - Plain inline boxes ignore transforms. A
<span>needsdisplay: inline-blockfirst.
JavaScript sees the difference too. Step 4 compares two ways to measure a scaled box.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Layout vs paint</title>
<style>
body { font-family: system-ui, sans-serif; margin: 24px; }
.row { display: flex; gap: 8px; }
.tile { width: 100px; padding: 20px 0; background: #e2e8f0; text-align: center; }
.tile.big { scale: 1.5; background: rgb(22 163 74 / 0.85); color: white; }
</style>
</head>
<body>
<div class="row">
<div class="tile">Idli</div>
<div class="tile big">Dosa</div>
<div class="tile">Vada</div>
</div>
<p id="out"></p>
<script>
const big = document.querySelector('.big');
document.querySelector('#out').textContent =
'offsetWidth: ' + big.offsetWidth +
', getBoundingClientRect().width: ' + big.getBoundingClientRect().width;
</script>
</body>
</html>offsetWidth: 100, getBoundingClientRect().width: 150
The Dosa tile covers parts of its neighbours, yet the gaps in the row stay the same. offsetWidth reports the layout size and ignores the transform. getBoundingClientRect() reports the painted box on screen.
Common mistakes
- Expecting
scaleto push neighbours away. It does not. If neighbours must move, changewidthor use a layout technique instead. - Writing
scaleaftertranslateintransformand expecting the move to grow. Only functions to the right ofscale()are scaled. - Two
transformrules on hover and normal states that list different functions. The hover rule replaces the whole list. Use individual properties for the part that changes. - Forgetting
transform-originon SVG shapes. They rotate around the canvas corner, not their own centre.
Interview questions
1. In what order are translate, rotate, scale and transform applied when all are set?
The browser applies translate, then rotate, then scale, then the functions in transform from left to right. The order you write the declarations in does not matter. This fixed order makes the individual properties predictable to combine.
2. Why does scale(2) translateX(50px) move an element further than translateX(50px) scale(2)?
Each function works in the coordinate system left by the previous ones. After scale(2) every unit is doubled, so the 50px move becomes 100px on screen. In the other order the move happens first at normal size, then the element grows in place.
3. Does transform affect the layout of surrounding elements?
No. The element keeps its original layout box and neighbours do not move. It does affect painting, hit testing and the scrollable overflow area, and it creates a stacking context and a containing block for positioned descendants.
4. What does getComputedStyle(el).transform return?
It returns a matrix() (or matrix3d() for 3D) that combines all the functions in transform, or none. It does not include the individual translate, rotate and scale properties, which are reported separately under their own names.
5. When would you choose the transform property over the individual properties?
When you need skew(), which has no individual property, or a function order other than translate, rotate, scale. For hover effects and transitions the individual properties are usually better, because changing one does not reset the others.
Key points
- In CSS the y axis points down and positive angles turn clockwise.
transformfunctions apply left to right, and each one changes the coordinate system for the next.- Individual properties always combine as translate, then rotate, then scale, then
transform. scaleX(-1)mirrors,skewX(-15deg)leans like italics, and the computed value oftransformis amatrix().transform-originmoves the pin for rotate, scale and skew; it can sit outside the element.- Transforms change painting, clicks and scroll areas, but never the layout of neighbours.
Exercise
Make a product card for a blue pottery plate from Jaipur. Add a "20% off" ribbon across the top right corner, made with rotate: 45deg and a suitable transform-origin or translate, and clip it with overflow: hidden on the card. Add "Previous" and "Next" buttons that share one arrow <span>, mirrored with scale for one direction. On hover or focus the card should grow to 1.04 and lift by 4px using individual properties, without disturbing the ribbon.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pottery card</title>
<style>
body { font-family: system-ui, sans-serif; margin: 32px; }
.card {
position: relative; overflow: hidden; width: 220px; padding: 20px;
border-radius: 10px; background: #eff6ff; box-shadow: 0 2px 6px rgb(0 0 0 / 0.15);
transition: scale 200ms ease-out, translate 200ms ease-out;
}
.card:hover, .card:focus-visible { scale: 1.04; translate: 0 -4px; }
.ribbon {
position: absolute; top: 0; right: 0; width: 140px;
padding: 4px 0; background: #dc2626; color: white; text-align: center;
transform-origin: top left;
translate: 50% 0;
rotate: 45deg;
}
.arrow { display: inline-block; }
.prev .arrow { scale: -1 1; }
</style>
</head>
<body>
<div class="card" tabindex="0">
<div class="ribbon">20% off</div>
<h2>Blue pottery plate</h2>
<p>Handmade in Jaipur. ₹1,120</p>
</div>
<p>
<button class="prev" type="button"><span class="arrow" aria-hidden="true">➤</span> Previous</button>
<button class="next" type="button">Next <span class="arrow" aria-hidden="true">➤</span></button>
</p>
</body>
</html>The ribbon starts at the top right, moves right by half its width, then turns 45 degrees around its top left corner so it crosses the corner; overflow: hidden trims the ends. Hover changes only scale and translate on the card, so the ribbon's own rotate is untouched. scale: -1 1 mirrors the arrow for "Previous".
Quick Quiz
1. An element has transform: scale(3) translateX(10px). How far does it move on screen?
The translate comes after the scale, so it is measured in the tripled coordinate system.
2. An element has scale: 2; translate: 40px 0; written in that order. Which is it equal to?
Individual properties always apply as translate, then rotate, then scale, whatever the source order.
3. A card is scaled to 1.5. What happens to the cards next to it?
Transforms change painting only, so the layout of neighbours stays the same.
4. Which value mirrors an element horizontally?
A negative horizontal scale flips the element left to right; rotate: 180deg would also turn it upside down.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
