2D transforms and origin
Definition and purpose
A transform changes an element’s rendered coordinate system without changing its reserved normal-flow size. Translation moves it, scale changes its apparent size, rotation turns it and skew shears it. Transformed boxes can overlap nearby content.
Scale from a corner
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a, #b { width: 100px; height: 40px; background: lightblue; } #a { transform: scale(2); transform-origin: left top; } </style> </head> <body> <div id="a">A</div> <div id="b">B</div> </body></html>A looks twice as wide, while B still starts at y=40.
Syntax and mental model
transform: translateX(20px) scale(2) composes matrices in the written order; for a point in fixed parent coordinates the rightmost function acts first. Reversing these functions changes the result. transform-origin: left top makes the top-left corner the pivot. A computed matrix is a serialization of the combined transform, not the original function text.
Function order
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a, #b { width: 100px; height: 40px; background: lightblue; } #a { transform: translateX(20px) scale(2); transform-origin: left top; } #b { transform: scale(2) translateX(20px); transform-origin: left top; } </style> </head> <body> <div id="a">A</div> <div id="b">B</div> </body></html>The two matrices translate the boxes by different amounts.
Common mistakes
Do not treat translate as margin: a transformed item leaves its normal-flow space behind. Percentages in translation refer to the transform reference box rather than the parent width.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Top layer and transformed ancestors, Color mixing and relative colors. Next lesson: 3D transforms and perspective.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
2D transforms and origin — knowledge check
Five questions on 2d transforms and origin.
Does transform scale change normal-flow reservation?
Which function translates along the horizontal axis?
What does transform-origin specify?
Can reversing scale and translate change the result?
What does translateX(50%) reference?