CSS Transforms
CSS Transforms
CSS transforms visually move, rotate or resize an element without reflowing its neighboring boxes.
What you’ll learn
- Apply translate, scale and rotate.
- Distinguish visual transformations from document flow.
- Set a transform origin deliberately.
Understand the concept
transform: translateY(-6px) shifts an element upward visually; its original layout space is still reserved. rotate(5deg) changes orientation; scale(1.05) enlarges the rendered shape.
Transforms run from right to left in a transform-function list and may create a new stacking context. For centered motion, transform-origin: center is the usual default.
What the browser evaluates
A transform paints the element in a new position while nearby elements remain laid out as if it had not moved.
Translate a label
Move a label upward without changing its neighbors.
HTML
<div class="transform-row"><span>Normal</span><span class="moved">Moved</span><span>Normal</span></div>CSS
.transform-row { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding-block: 15px; } .transform-row span { padding: 12px; background: #dbeafe; border-radius: 6px; } .moved { transform: translateY(-12px); }Rendered output
Observe: The middle badge rises, but the Flexbox spacing remains unchanged.
Rotate and scale a tile
Apply two transform functions to one shape.
HTML
<div class="transform-tile">Featured</div>CSS
.transform-tile { margin: 20px; width: 110px; padding: 20px 8px; text-align: center; background: #ccfbf1; border: 2px solid #0f766e; border-radius: 8px; transform: rotate(-5deg) scale(1.05); transform-origin: center; }Rendered output
Observe: The tile rotates and enlarges while its original box still occupies layout space.
Explain the result
A transform paints the element in a new position while nearby elements remain laid out as if it had not moved.
Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.
Common mistakes to avoid
Transforms can visually overlap nearby items because layout is unchanged.
Do not use transform:scale() to replace accessible font sizing.
Try it yourself
Change translateY to translateX(20px), then compare the reserved gap.
Before changing the code, predict what will change. Then verify by checking the rendered result.
Check your understanding
What you should remember
Use transforms for visual adjustments and motion; use layout properties for real structural changes.
Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.
Reaching the recap and completing the quizzes are separate milestones.